1 / 19

Exceptions and Exception Handling (continued)

Exceptions and Exception Handling (continued). Carl Alphonce CSE116. Exceptions and flow-of-control. When an exception is thrown, the regular flow of control is interrupted. Invocation records are popped from runtime stack until an exception handler is found.

heba
Télécharger la présentation

Exceptions and Exception Handling (continued)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exceptions andException Handling(continued) Carl Alphonce CSE116

  2. Exceptions and flow-of-control • When an exception is thrown, the regular flow of control is interrupted. • Invocation records are popped from runtime stack until an exception handler is found. • Because of this, code in a method after a “throw” is not executed if the throw occurs. Intermediate Java

  3. Example public int someMethod() { stmt1; stmt2; if (x<=0) { throw new Exception(); } stmt3; return x; } Intermediate Java

  4. Example (if x > 0) public int someMethod() { stmt1; stmt2; if (x<=0) { throw new Exception(); } stmt3; return x; } Intermediate Java

  5. Example (if x <= 0) public int someMethod() { stmt1; stmt2; if (x<=0) { throw new Exception(); } stmt3; return x; } Intermediate Java

  6. Catching an exception • If you want to catch an exception, you must indicate: • from which segment of code you are prepared to handle an exception • which exception(s) you are going to handle • You can also: • include a “cleanup” case to release resources acquired, regardless of whether an exception was thrown Intermediate Java

  7. The try block • To indicate the segment of code from which you are prepared to handle an exception, place it in a try block: stmt1; try { stmt2; stmt3; } stmt4; Intermediate Java

  8. A catch block • A catch block is an exception handler for a specific type of exception: try { // code that may throw exception } catch (Exception e) { // code to handle exception } Intermediate Java

  9. Multiple catch blocks • Can place many catch blocks (exception handlers) after a try block: try { // code that may throw exception } catch (IndexOutOfBoundsException e) { // code to handle an index out of bounds exception } catch (MalformedURLException e) { // code to handle a malformed URL exception } catch (Exception e) { // code to handle a general exception } Intermediate Java

  10. Recall there is an exception hierarchy: • Here’s part of the hierarchy: • Exception • IOException • FileNotFoundException • MalformedURLException • RuntimeException • IndexOutOfBoundsException • NullPointerException Intermediate Java

  11. Catch block ordering: specific to general • Catch blocks are tried in the order they are written: try { // code that may throw exception } catch (IndexOutOfBoundsException e) { // code to handle index out of bounds exception } catch (RuntimeException e) { // code to handle runtime exception } catch (Exception e) { // code to handle any other exception } Intermediate Java

  12. Now consider a slightly different part of the hierarchy: • Here’s part of the hierarchy: • Exception • IOException • FileNotFoundException • MalformedURLException • RuntimeException • IndexOutOfBoundsException • NullPointerException Intermediate Java

  13. Catch block ordering: general to specific? • Catch blocks are tried in the order they are written: try { // code that may throw exception } catch (Exception e) { // code to handle any exception } catch (RuntimeException e) { // code to handle a runtime exception // this is never reached! } catch (IndexOutOfBoundsException e) { // code to handle index out of bounds exception // this is never reached! } Intermediate Java

  14. Finally clause • Optional • Used to release resources back to OS • Shared resources are often acquired inside a try block (e.g. a file is opened for writing) • These resources must be released (e.g. the file must be closed so some other piece of code can use it): • if an exception is NOT thrown in the try block • if an exception IS thrown in the try block Intermediate Java

  15. Flow of control:no exception is thrown // some code try { // code that may throw exception } catch (IndexOutOfBoundsException e) { // code to handle index out of bounds exception } catch (RuntimeException e) { // code to handle runtime exception } catch (MalformedURLException e) { // code to handle malformed URL exception } finally { // cleanup code } // more code Intermediate Java

  16. Flow of control:handled exception (e.g. RuntimeException) is thrown // some code try { // code that throws a RuntimeException } catch (IndexOutOfBoundsException e) { // code to handle index out of bounds exception } catch (RuntimeException e) { // code to handle runtime exception } catch (MalformedURLException e) { // code to handle malformed URL exception } finally { // cleanup code } // more code Intermediate Java

  17. Flow of control:unhandled exception (e.g. FileNotFoundException) is thrown // some code try { // code that throws a RuntimeException } catch (IndexOutOfBoundsException e) { // code to handle index out of bounds exception } catch (RuntimeException e) { // code to handle runtime exception } catch (MalformedURLException e) { // code to handle malformed URL exception } finally { // cleanup code } // more code Intermediate Java

  18. Defining your own exception classes • Since exceptions are just objects derived from the type java.util.Exception, you can define your own. • There are *many* predefined exceptions – one will likely meet your needs. • To define your own: Intermediate Java

  19. An example of an exception class public class IllegalStateException extends RuntimeException { public IllegalStateException() { super(); } public IllegalStateException(String s) { super(s); } public IllegalStateException(String message, Throwable cause) { super(message, cause); } public IllegalStateException(Throwable cause) { super(cause); } } Intermediate Java

More Related