| Line 344: |
Line 344: |
| | see: [https://github.com/rafahsolis/javaTutorial/blob/master/AnonymousClass.java AnonymousClass.java] | | see: [https://github.com/rafahsolis/javaTutorial/blob/master/AnonymousClass.java AnonymousClass.java] |
| | == Exceptions == | | == Exceptions == |
| | + | [http://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html Class Exception] |
| | + | === trows === |
| | <source lang="java"> | | <source lang="java"> |
| | package tutorialJava; | | package tutorialJava; |
| Line 353: |
Line 355: |
| | public class HandlingExceptions { | | public class HandlingExceptions { |
| | | | |
| | + | public static void main(String[] args) trows FileNotFoundException { |
| | + | |
| | + | File file = new File("test.txt"); |
| | + | |
| | + | FileReader fr = new FileReader(file); |
| | + | } |
| | + | |
| | + | }</source> |
| | + | |
| | + | === try/catch === |
| | + | <source lang="java"> |
| | + | import java.io.IOException; |
| | + | import java.text.ParseException; |
| | + | |
| | + | public class MultipleExceptions { |
| | public static void main(String[] args) { | | public static void main(String[] args) { |
| | + | Test test = new Test(); |
| | + | |
| | + | //Option 1 |
| | + | try { |
| | + | test.run(); |
| | + | } catch (IOException e) { |
| | + | // e.printStackTrace(); |
| | + | System.out.println("File not found."); |
| | + | } catch (ParseException e) { |
| | + | // e.printStackTrace(); |
| | + | System.out.println("Parse error."); |
| | + | } |
| | | | |
| − | File file = new File("test.txt"); | + | //Option 2 |
| | + | try { |
| | + | test.run(); |
| | + | } catch (IOException | ParseException e) { |
| | + | // TODO Auto-generated catch block |
| | + | e.printStackTrace(); |
| | + | } |
| | | | |
| | + | //Option 3 |
| | try { | | try { |
| − | FileReader fr = new FileReader(file); | + | test.run(); |
| − | } catch (FileNotFoundException e) { | + | } catch (Exception e) { |
| − | //e.printStackTrace(); | + | // TODO Auto-generated catch block |
| − | System.out.println("File not found: " + file.toString());
| + | e.printStackTrace(); |
| | } | | } |
| | + | |
| | + | //Option 4 |
| | + | // add throws to main declaration... |
| | } | | } |
| − |
| |
| | }</source> | | }</source> |
| | | | |