Download presentation
Presentation is loading. Please wait.
Published byElinor Welch Modified over 9 years ago
1
Exception Handling Unit-6
2
Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following: A user has entered invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications, or the JVM has run out of memory. Examples – Divide by zero errors – Accessing the elements of an array beyond its range – Invalid input – Hard disk crash – Opening a non-existent file – Heap memory exhausted
3
categories of exceptions Checked exceptions: Exceptions which occur at compile time. e.g.: ClassNotFoundException, NoSuchMethodException, NoSuchFieldException etc. Unchecked exceptions(Runtime exceptions): Exceptions which occur at run time. eg: ArrayIndexOutOfBoundsException, ArithmeticException, NumberFormatException etc. Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
4
Exception Handling In java, exception is handled using five keywords try, catch,throw,throws and finally. Before understanding the use of these keywords lets us define some basic terms- (1)Exception: Exception is a representation of error condition. (2)Throwing an Exception: When we want to detect an abnormal condition then it is called throwing an exception. (3)Catching an Exception: The Exception which is thrown must be caught. this process is called catching of exception. (4)Handling an Exception: The Exception must be processed properly this process is called handling of exception.
5
list of Java Unchecked( Runtime)Exception Java defines several exception classes inside the standard package java.lang.
6
list of Java Checked Exceptions
7
Catching Exceptions Using Try & Catch A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: o A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.
8
Example(1)
9
Example(2) Class ExceptionDemo { public static void main(String args[]) { try { int a,b; A try block which includes the lines for which exception must be raised. a=5; b=a/0; } catch(ArithmeticException e) { System.out.println(“Divide by Zero\n”); Exception is caught by catch block. A try-catch pair must Exit. } System.out.println(“Executed catch statement”); } OUTPUT Divide by Zero Executed Catch statement
10
Multiple catch Blocks The syntax for multiple catch blocks looks like the following: The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes down to the second catch statement. This continues until the exception either is caught or falls through all catches, in which case the current method stops execution and the exception is thrown down to the previous method on the call stack.
11
Program1:Multiple catch Blocks output In this example we have used two catch clause catching the exception ArrayIndexOutOfBoundsException and ArithmeticException in which the statements that may raise exception are kept under the try block. When the program is executed, an exception will be raised. Now that time the first catch block is skipped and the second catch block handles the error.
12
Program2:Multiple catch Blocks OUTPUT
13
Handling the Unreachable Code Problem The multiple catch blocks can generate unreachable code error i.e. if the first catch block contains the Exception class object then the subsequent catch blocks are never executed. This is known as Unreachable code problem. To avoid this, the last catch block in multiple catch blocks must contain the generic class object that is called the Exception class. This exception class being the super class of all the exception classes and is capable of catching any types of exception. The generic Exception class can also be used with multiple catch blocks.
14
Program3:Handling the Unreachable Code Problem OUTPUT: In this example we didn't specify that which one exception may occur during the execution of program but here we are trying to access the value of an array that is out of bound still we don't need to worry about handle the exception because we have used the Exception class that is responsible to handle any type of exception.
15
Exception Handling Using throw keyword Basically the exceptions thrown by java runtime syatems. But it is not all the time possible for java runtime system to throw the exception. This burden may come on programmers' shoulders. That means sometime we have to explicitly the throw of exception. The syntax of declaring and creating an exception using throw is: throw instanceof_Exception for Example: throw ArithmeticException;
16
Program: throw keyword Output: Caught inside demoproc. Recaught: java.lang.NullPointerException: demo
17
Exception Handling Using throws keyword The throws keyword in java programming language is applicable to a method to indicate that the method raises particular type of exception while being processed. Syntax: type method-name parameter-list) throws exception-list { // body of method }
18
Program: throws keyword Output: Inside throwOne. Caught java.lang.IllegalAccessException: demo
19
Exception Handling Using finally keyword Sometime because of execution of try block the execution gets break off. And this may cause that some important code which comes after throwing off an exception may not get executed. The finally block provide the assurance of execution of some important code that must be executed after the try block. A finally block of code always executes, whether or not an exception has occurred.
20
Syntax & Program of finally keyword OUTPUT:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.