Download presentation
Presentation is loading. Please wait.
1
Lab 1 Exception Handling
2
Lab 1: Exception Handling
Introduction Exceptions in Java Types of Exceptions Handling Exceptions Causes of Exceptions
3
Introduction Exception handling is a mechanism for dealing with runtime errors. Syntactically-correct programs do not mean they are error-free during runtime. For example, a program trying to read data from a non-existent file. In languages such as C, the programmer is responsible for error checking but is not rigidly enforced by the language.
4
Java provides support for exception handling.
The programmer is not forced to check whether f is null. For example (C), FILE *f = fopen("list.dat", "r"); fscanf(f, "%d", &qty); Java provides support for exception handling.
5
Exceptions in Java In Java, exceptions are represented as objects.
An exception object representing a particular exception contains data about that exception. This makes it possible to obtain clues as to why it occurred and hopefully diagnose the cause of the problem.
6
When an exception occurs, an exception object is created and "thrown" to the calling method.
The calling method may choose to handle ("catch") the exception and/or "throw" the exception to its calling method, and so on.
7
EXCEPTION OCCURS Method C throws the exception object Method C : Exception Method B throws the exception object calls Method B calls Method B catches the exception object and handles the exception Method A Method A catches the exception object and handles the exception
8
Each method involved in the calling chain must:
provide error handling code (if the method wants to) for each type of exception object thrown by the methods it calls. rethrow the exception objects it receives to its calling method.
9
Types of Exceptions java.lang.Throwable java.lang.Error
java.lang.Exception
10
java.lang.Error Exceptions
These are serious errors which should be handled by the Java system. Examples of exception objects in this category: AWTError - serious error occurs in the AWT NoClassDefFoundError OutOfMemoryError
11
java.lang.RuntimeException Exceptions
java.lang.RuntimeException is a subclass of java.lang.Exception. It usually represents fairly serious errors which are difficult to recover from. They are automatically handled by the Java system although the programmer may handle them himself if there is a need. Examples: NullPointerException, SecurityException
12
Handling Exception try and catch statements throws keyword
Throwing an exception finally statement
13
try and catch statements
… x.method(); } catch (ExceptionType e) { Calling a method that may throw an ExceptionType exception If the exception occurs, it will be caught here and statements in this block will be executed.
14
throws keyword A method may rethrow an exception without catching the exception. Example: public void method() throws IOException { … }
15
Throwing an Exception A method may catch an exception and rethrow it to its caller. Example: public void op() throws IOException { try { x.method(); } catch (IOException e) { System.out.println("Error"); throw e;
16
finally statement This statement is optional. Statements in the finally block will be executed whether or not an exception occurs. try { … } catch (ExceptionType e) { } finally { }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.