Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.

Similar presentations


Presentation on theme: "Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling."— Presentation transcript:

1 Exception and Exception Handling

2 Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling the parseInt method with an argument that cannot be parsed into an integer Dividing a non-zero integer by zero Accessing an array outside of its boundary

3 Types of Programming Errors Syntax errors – non-compliance of rule Logic errors – incorrect logic Runtime errors – require computer to carry out impossible tasks

4 Exception Handling When a method encounters an abnormal condition that it cannot handle, it may throw an exception. Throwing an exception is like tossing a ball out of a window hoping that someone on the outside will catch it.

5 Response to an Exception Ignore, and do nothing Let the user fix the problem The programmer sets control mechanism within the program to catch the exception, and deal with it.

6 Procedure for Handling Exception try { } catch(ExceptionObject e) { } : finally { } try { } catch(ExceptionObject e) { } : finally { }

7 The try block encloses the code that may generate an exception. The throw clause specifies what exception object is to be thrown. The catch block handles the exception. If no exception is thrown, this clause is bypassed. The finally block always gets executed

8 Understanding Runtime Errors 1.import javax.swing.JOptionPane; 2.class Exception 3.{ 4.public static void main(String[] arg) 5.{ 6.boolean done = false; 7. 8.while (!done) 9.{ 10.String s = JOptionPane.showInputDialog("Enter an integer"); 11. 12.int i = Integer.parseInt(s); 13.if (i == 4) 14.done = true; 15.} 16.} 17.}

9 Exception Classes Throwable Error Exception IOExceptionRuntimeException

10 The Super Class Throwable Throwable() Constructs a new Throwable with no detail message. Throwable(String message) With detailed message

11 Frequently used Methods getMessage() Returns the detail message of this Throwable object toString() Returns a short description of this Throwable object. printStackTrace() Displays a back trace to the console.

12 Exception and Its Associated Packages

13 Checked and Unchecked Exceptions Checked Exceptions – are verified by the compiler during compilation time Unchecked Exceptions - are not verified during compilation time

14 How to Handle Unchecked Exception Unchecked exceptions can be the result of poor programming logic. Problem more than likely can be solved without exception.

15 Without using Exception 1.class WithoutUsingException 2.{ 3.public static void main(String [ ] arg) 4.{ 5.int denom = 0; 6. 7.if (denom != 0) 8.{ 9.int x = 20/denom; 10.} 11.} 12.}

16 UncheckedException 1.class UncheckedException { 2.public static void main(String [ ] arg) { 3.int denom = 0; 4. try{ 5.int x = 20/denom; 6.} 7.catch(ArithmeticException e) 8.{ } 9.} 10.}

17 Handling Multiple Unrelated Exceptions When there are multiple unrelated exceptions to be caught, separate catch blocks are required The order in which the catch blocks are arranged does not matter. (See class Division)

18 Handling Multiple Related Exceptions When exception objects are related by inheritance, the catch blocks should be arranged in reverse order of how the classes were inherited (See class ExceptionOrder)

19 Alternate Handling of Multiple Exceptions try { } catch(E1 | E2 | E3 …. e) { } (See class ExceptionOrder again)

20 Manually Throwing an Exception


Download ppt "Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling."

Similar presentations


Ads by Google