Exception syntax try { ... code with possible exception ... } catch (ExceptionClassName e) { ... code to run if exception is thrown ... } finally { ... code run before finished with exception handling if not interrupted ... }
Exception examples you can have multiple catch clauses (for each type of possible exception) examples will be illustrated using posted demonstration programs TestException1.java (CIS 160): a non-compiling exception demonstration TestException2.java (CIS 160): a non-compiling exception demonstration TestException3.java (CIS 160): specifying but not catching an exception TestException4.java (CIS 160): catching an exception demonstration TestException5.java (CIS 160): displaying a special exception message TestException0.java: unchecked exceptions don't have to be specified TestException1.java: catching an unchecked exception TestException2.java: will not compile, exceptions not declared as thrown and not caught TestException3.java: declaring that exceptions are thrown TestException4.java: treating FileNotFoundException as an IOException TestException5.java: multiple catch blocks and "finally" processing TestException6.java: creating and throwing your own exceptions TestException7.java: creating and using exceptions with custom error messages TestException8.java: using try-with-resources TestException9.java: experimenting with finally TestException10.java: more experimentation with finally TestException11.java: even more experimentation with finally