Sadegh Aliakbary Sharif University of Technology Fall 2010
Agenda Exception Handling Fall 2010Sharif University of Technology2
Review Fall 2010Sharif University of Technology3
Benefits of Exception Handling Framework Separating Error-Handling code from “regular” business logic code Propagating errors up the call stack Grouping and differentiating error types Fall 2010Sharif University of Technology4
Separating Error-Handling Code Consider pseudocode method It reads an entire file into memory readFile { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } Fall 2010Sharif University of Technology5
Traditional Programming Fall 2010Sharif University of Technology6
With Exception Handling Framework Fall 2010Sharif University of Technology7
Note You should still write code for Detecting, Reporting and Handling exceptions Exception handling framework is not responsible for these jobs! It only helps you organize the work more effectively Fall 2010Sharif University of Technology8
Propagating Errors Up the Call Stack Traditional approach Each method should explicitly forward the exception Use a special return code Using return type for reporting exceptions Smells bad! New approach Automatic Beautiful! Fall 2010Sharif University of Technology9
Grouping and Differentiating Error Types All exceptions thrown within a program are objects The grouping or categorizing of exceptions is a natural outcome of the class hierarchy Fall 2010Sharif University of Technology10
Fall 2010Sharif University of Technology11
Example2 class MultipleCatch { public static void main(String args[]) { try { int den = Integer.parseInt(args[0]); System.out.println(3/den); } catch (ArithmeticException exc) { System.out.println(“Divisor was 0.”); } catch (ArrayIndexOutOfBoundsException exc2) { System.out.println(“Missing argument.”); } System.out.println(“After exception.”); } Fall 2010Sharif University of Technology12
Nested Tries class NestedTryDemo { public static void main(String args[]){ try { int a = Integer.parseInt(args[0]); try { int b = Integer.parseInt(args[1]); System.out.println(a/b); } catch (ArithmeticException e) { System.out.println(“Div by zero error!"); } } catch (ArrayIndexOutOfBoundsException) { System.out.println(“Need 2 parameters!"); } Fall 2010Sharif University of Technology13
Bad Use of Exceptions Don’t Use Exception instead of If-else Use exceptions for exceptions! Fall 2010Sharif University of Technology14
Writing Your Own Exceptions your classes should extend Exception class Only Exception Subclasses could be thrown and caught Steps to follow Create a class that extends the RuntimeException or the Exception class Customize the class Members and constructors may be added to the class Fall 2010Sharif University of Technology15
Example class HateStringExp extends Exception { /* some code */ } String input = "invalid input"; try { if (input.equals("invalid input")) { throw new HateStringExp(); } System.out.println("Accept string."); } catch (HateStringExp e) { System.out.println("Hate string!”); } Fall 2010Sharif University of Technology16
Finally try { //.. } catch (ExceptionType e) { //… }... } finally { } Contains the code for cleaning up after a try or a catch Fall 2010Sharif University of Technology17
Finally (2) Block of code is always executed Despite of different scenarios: Normal completion Forced exit occurs using a return, a continue or a break statement Caught exception thrown Exception was thrown and caught in the method Uncaught exception thrown Exception thrown was not specified in any catch block in the method Fall 2010Sharif University of Technology18
Fall 2010Sharif University of Technology19
Unchecked Exceptions private static void function(String[] args) { int den = Integer.parseInt(args[0]); System.out.println(3 / den); } public static void main(String[] args) { function(args); } function() may throw exceptions But it has not declared it with throws keyword Why? Fall 2010Sharif University of Technology20
Checked and Unchecked Exceptions Checked exception Java compiler checks the program should catch or list the occurring exception If not, compiler error will occur Unchecked exceptions Not subject to compile-time checking for exception handling Built-in unchecked exception classes Error RuntimeException Their subclasses Fall 2010Sharif University of Technology21
Exception Class Hierarchy Fall 2010Sharif University of Technology22
Exception Classes and Hierarchy Multiple catches should be ordered from subclass to superclass class MultipleCatchError { public static void main(String args[]){ try { int a = Integer.parseInt(args [0]); int b = Integer.parseInt(args [1]); System.out.println(a/b); } catch (ArrayIndexOutOfBoundsException e) { //.. } catch (Exception ex) { //.. } Fall 2010Sharif University of Technology23
Further Reading Assertions assert name!=null; Fall 2010Sharif University of Technology24
Fall 2010Sharif University of Technology25
References Fall 2010Sharif University of Technology26