260 likes | 397 Views
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.
E N D
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 • Tells the calling routine “something bad happened, and this method failed”
Exceptions in Java publicvoid makeFile() throws FileNotFoundException
Why Exception Handling? • What could you do instead?
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(); }
Who throws an exception? public Object pop() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException(); } // pop and return the top // element of this stack... }
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.
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
Checked and Unchecked • Runtime Exceptions and Errors are unchecked • , … • All other exceptions are checked
Example - Unchecked public void doSomething(Object x) { String str = x.toString(); // NullPointerException // … }
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); // … }
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); } }
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();”
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 … }
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 … }
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(); • }
Multiple Throws public void myMethod() throws MyException1, MyException2 myMethod may throw either of these exceptions
Multiple catches try { … } catch (MyException1 me1) { … } catch (MyException2 me2) { … }
Multiple catches try { … } catch (MyException1 me1) { … } catch (MyException2 me2) { … }
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?
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?
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 */ } }