1 / 26

Exceptions

Exceptions. Situation. To reduce the cost of writing programs, reuse software Class libraries, utility routines Things someone else wrote Your code calls a routine that you didn’t write They couldn’t foresee your situation, but they know when their code will fail. Exceptions.

ajaxe
Download Presentation

Exceptions

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

  2. Situation • To reduce the cost of writing programs, reuse software • Class libraries, utility routines • Things someone else wrote • Your code calls a routine that you didn’t write • They couldn’t foresee your situation, but they know when their code will fail

  3. Exceptions • Tells the calling routine “something bad happened, and this method failed”

  4. Exceptions in Java publicvoid makeFile() throws FileNotFoundException

  5. Why Exception Handling? • What could you do instead?

  6. Exception Handling // Client code Stack jobStack = new Stack(); // … try { Job work = (Job) jobStack.pop(); // normal flow of control, e.g., work.doIt(); } catch (StackEmptyException e) { // exceptional flow of control … e.printStackTrace(); }

  7. Options for dealing with exceptions

  8. Who throws an exception? public Object pop() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException(); } // pop and return the top // element of this stack... }

  9. RuntimeException Throwable Exception Error user exceptions JVM errors JVM exceptions JVM exceptions Exception Hierarchy • Exceptions are modeled as objects of exception classes. • Exception classes are organized in a class hierarchy, called an exception hierarchy.

  10. Exception Hierarchy (partial)

  11. Checked and Unchecked • The compiler checks for exception handlers • If you call a method that throws an exception, it must either throw an exception or handle the exception, unless it is a RuntimeException

  12. Checked and Unchecked • Runtime Exceptions and Errors are unchecked • , … • All other exceptions are checked

  13. Example - Unchecked public void doSomething(Object x) { String str = x.toString(); // NullPointerException // … }

  14. Example - Checked // Incorrect code public void readFile(String n) { // can throw java.io.FileNotFoundException FileInputStream f = new FileInputStream(n); // … } // Correct code: propagate exceptions public void readFile(String n) throws java.io.FileNotFoundException { FileInputStream f = new FileInputStream(n); // … }

  15. Example - Checked (Cont.) // Correct code: catch and handle exceptions public void readFile(String n) { try { FileOutputStream f = new FileOutputStream(n); // … } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(-1); } }

  16. Why?

  17. Summary • A method can throw an exception when something fails at runtime • An exception is an object of type Exception • The compiler ignores exceptions of type RuntimeException • All other exceptions are checked by the compiler for handlers • To throw, “throw new MyException();”

  18. Defining Your Own Exceptions • Same as defining regular classes public class StackEmptyException extends Exception { public StackEmptyException() { } public StackEmptyException(String msg) { super(msg); } // other fields and methods here … }

  19. Throwing Exceptions • Use the throw statement public class Stack { /** Pops and returns the top element of this stack. */ public Object pop() throws StackEmptyException { if (isEmpty()) { throw new StackEmptyException(“Sorry, stack is empty.”); } // the rest of code … } // other fields and methods here … }

  20. Handling Exceptions • Use the try-catch-[finally] statement • Stack jobs = …; • try { • // normal case … • Job work = (Job) jobs.pop(); • work.doIt(); • } catch (StackEmptyException e) { • // handle exceptions • fireMe(); • } finally { • // finalization • goVacation(); • }

  21. Multiple Throws public void myMethod() throws MyException1, MyException2 myMethod may throw either of these exceptions

  22. Multiple catches try { … } catch (MyException1 me1) { … } catch (MyException2 me2) { … }

  23. Multiple catches try { … } catch (MyException1 me1) { … } catch (MyException2 me2) { … }

  24. What happens? M = new MyStack(); try { m.pop(); } catch (MyStackEmptyException e) { System.out.println(“MSEE”); } catch (MyStackException e) { System.out.println(“MSEE”); } What if a MyStackEmptyException is thrown? What if a MyStackFullException is thrown?

  25. What happens? Changed here M = new MyStack(); try { m.pop(); } catch (Exception e) { System.out.println(“MSEE”); } catch (MyStackException e) { System.out.println(“MSEE”); } What if a MyStackEmptyException is thrown? What if a MyStackFullException is thrown?

  26. Exception Rules • You cannot have a catch or finally without a try • You cannot put code between the try and catch try { x.doSomething(); } int x = 10; catch (Exception e) { …} • A try must be followed by either a catch or a finally (or both) try { x.doSomething(); } finally { /* cleanup */ } • A try without a catch (only a finally) must still declare the exception void foo() throws BarException { try { x.doSomething(); } finally { /* cleanup */ } }

More Related