COE318 Software Systems Lecture Notes: Week 10 1 of 5 COE318 Lecture Notes Week 10 (Nov 7, 2011) Topics More about exceptions References Head First Java: Chapter 11 (Risky Behavior) The Java Tutorial: Read the Chapter on Exceptions. (This also includes several questions/answers and examples. More about Exceptions (The basics) An Exception is thrown when a method (or constructor) encounters a situation it cannot deal with. Some examples of this kind of situation include: Trying to divide by zero. Trying to use a null object. Encountering an invalid parameter, Trying to access an array with an illegal index. Trying to open a non-existent file or URL. Incorrectly casting an object to an illegal type. When a method (or constructor) throws an exception, it aborts execution (and a constructor does not create a new object). The recipient of the Exception is the method that invoked the method that threw the exception. The receiving method can either duck or catch the exception. If the exception is ducked (ignored), it is propagated to the method that called that method. If nothing catches the exception, the entire program is terminated. A block of statements which may cause result in an exception can be placed in a try block and and specific exceptions can be caught in catch blocks.
COE318 Software Systems Lecture Notes: Week 10 2 of 5 Example of basic usage public class A { public void method(a a) { int i = 5; int j; j = i / 1; // j = i / 0; System.out.println("A"); // throw new MyException(); public class B extends A { @Override public void method(a a) { a.method(a); int i = Integer.parseInt("123"); System.out.println("B"); public class C extends B { private static boolean CATCH = false; public void method(b b) { Object obj = (Object) b; // String s = (String) obj; if (!CATCH) { b.method(new A()); else { b.method(new A());
COE318 Software Systems Lecture Notes: Week 10 3 of 5 catch (NullPointerException ex) { System.err.println("Caught C: " + ex); catch (Exception ex) { System.err.println("Caught C: " + ex); System.out.println("methodC works!"); public class Main { private static boolean CATCH = false; public static void main(string[] args) { C c = new C(); if (!CATCH) { c.method(c); else { c.method(c); catch (ArithmeticException ex) { System.err.println("Caught (in main): " + ex); finally { System.out.println("Finally in main"); System.out.println("Main finished successfuly"); public class MyException extends Exception { /** * Creates a new instance of <code>myexception</code> * without detail message. */ public MyException() {
COE318 Software Systems Lecture Notes: Week 10 4 of 5 /** * Constructs an instance of <code>myexception</code> * with the specified detail message. * @param msg the detail message. */ public MyException(String msg) { super(msg); Checked vs. Unchecked Exceptions Exceptions are divided into two broad categories: unchecked (or runtime) exceptions: ordinary ones and Errors checked exceptions Unchecked exceptions include (among others): IllegalArgumentException NullPointerException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException Unchecked exceptions are often (but not always) the result of a programming error (bug). Unchecked exceptions do not have to be declared or caught. Checked exceptions must be declared. The compiler insists! A method that may throw a checked exception must explicitly declare that it does with the throws clause. For example: void methodc() throws MyException {...throw(new MyException(); Any method that invokes methodc must either: catch it; or re-throw it For example, if methodb invokes methodc, we must have one of the two alternatives:
COE318 Software Systems Lecture Notes: Week 10 5 of 5 //Alternative 1: catch it void methodb() {. methodc(); catch (MyException ex) {. //Alternative 2: re-throw it void methodb throws MyException { methodc(); Custom Exceptions You can make your own exceptions. These should be checked exceptions. (i.e. extend Exception.)