Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2010.

Similar presentations

Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2010."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2010

2 Agenda Exception Handling Fall 2010Sharif University of Technology2

3 Review Fall 2010Sharif University of Technology3

4 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

5 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

6 Traditional Programming Fall 2010Sharif University of Technology6

7 With Exception Handling Framework Fall 2010Sharif University of Technology7

8 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

9 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

10 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

11 Fall 2010Sharif University of Technology11

12 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

13 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

14 Bad Use of Exceptions Don’t Use Exception instead of If-else Use exceptions for exceptions! Fall 2010Sharif University of Technology14

15 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

16 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

17 Finally try { //.. } catch (ExceptionType e) { //… }... } finally { } Contains the code for cleaning up after a try or a catch Fall 2010Sharif University of Technology17

18 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

19 Fall 2010Sharif University of Technology19

20 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

21 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

22 Exception Class Hierarchy Fall 2010Sharif University of Technology22

23 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

24 Further Reading Assertions assert name!=null; Fall 2010Sharif University of Technology24

25 Fall 2010Sharif University of Technology25

26 References Fall 2010Sharif University of Technology26

Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2010."

Similar presentations

Ads by Google