References: Java, The Complete Reference, 4 Edition Thinking in Java, 4th Edition
The document discusses Java exception handling. It covers topics like try/catch blocks, throwing exceptions, exception types, and finally blocks. Key points include that exceptions represent runtime errors, try/catch blocks allow handling exceptions, and finally blocks ensure code is executed whether an exception occurs or not.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
63 views23 pages
References: Java, The Complete Reference, 4 Edition Thinking in Java, 4th Edition
The document discusses Java exception handling. It covers topics like try/catch blocks, throwing exceptions, exception types, and finally blocks. Key points include that exceptions represent runtime errors, try/catch blocks allow handling exceptions, and finally blocks ensure code is executed whether an exception occurs or not.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23
References:
•Java, The Complete Reference, 4th Edition
•Thinking in Java, 4th Edition
Ms. Nagasundari Department of ISE, PESIT 1
Fundamentals Types Using Try and Catch Multiple Catch clauses Nested Try Throw Throws Finally Built in Exceptions Chained Exceptions Ms. Nagasundari Department of ISE, PESIT 2 An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a runtime error. A Java exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error.
Ms. Nagasundari Department of ISE, PESIT 3
Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment. Manually generated exceptions are typically used to report some error condition to the caller of a method. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally
Ms. Nagasundari Department of ISE, PESIT 4
This is the general form of an exception- handling block: try { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionType2 } // ... finally { // block of code to be executed before try block ends } Ms. Nagasundari Department of ISE, PESIT 5 Ms. Nagasundari Department of ISE, PESIT 6 A try and its catch statement form a unit. The scope of the catch clause is restricted to those statements specified by the immediately preceding try statement. A catch statement cannot catch an exception thrown by another try statement. The statements that are protected by try must be surrounded by curly braces. You cannot use try on a single statement.
Ms. Nagasundari Department of ISE, PESIT 7
Benefits of using user defined Exception handler: It allows you to fix the error. It prevents the program from automatically terminating. Ex: class Exc_han { try { // monitor a block of code. } catch (Exception) {error } } } Ms. Nagasundari Department of ISE, PESIT 8 In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others are bypassed, and execution continues after the try/catch block. Ms. Nagasundari Department of ISE, PESIT 9 Ex: class MultiCatch { try { // Statements } catch(Exception) { //ERROR } catch(Exception ) { //ERROR } } Ms. Nagasundari Department of ISE, PESIT 10 The try statement can be nested. That is, a try statement can be inside the block of another try. Each time a try statement is entered, the context of that exception is pushed on the stack. If an inner try statement does not have a catch handler for a particular exception, the stack is unwound and the next try statement's catch handlers are inspected for a match. This continues until one of the catch statements succeeds, or until all of the nested try statements are exhausted. If no catch statement matches, then the Java run-time system will handle the exception.
Ms. Nagasundari Department of ISE, PESIT 11
Ex: class NestTry { try { // Statements try { // nested try block } } Catch(Exception) { //ERROR } } Catch(Exception) { //ERROR } } Ms. Nagasundari Department of ISE, PESIT 12 It is possible for your program to throw an exception explicitly, using the throw statement. The general form of throw is shown here: Throw ThrowableInstance; ThrowableInstance must be an object of type Throwable or a subclass of Throwable. There are two ways you can obtain a Throwable object: Using a parameter into a catch clause Creating one with the new operator
Ms. Nagasundari Department of ISE, PESIT 13
The flow of execution stops immediately after the throw statement, any subsequent statements are not executed. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of the exception. If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception handler halts the program and prints the stack trace.
Ms. Nagasundari Department of ISE, PESIT 14
Ex: class ThrowDemo { static void demoproc() { try { throw new NullPointerException("demo"); } catch(NullPointerException e) { System.out.println("Caught inside demoproc."); throw e; // rethrow the exception } }
Ms. Nagasundari Department of ISE, PESIT 15
If a method is capable of causing an exception that it does not handle, it must specify this behaviour so that callers of the method can guard themselves against that exception. We do this by including a throws clause in the method's declaration. A throws clause lists the types of exceptions that a method might throw. This is necessary for all exceptions, except those of type Error or Runtime Exception, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result.
Ms. Nagasundari Department of ISE, PESIT 16
This is the general form of a method declaration that includes a throws clause: type method(parameter-list) throws exception-list { // body of method }
Here, exception-list is a comma-separated list
of the exceptions that a method can throw.
Ms. Nagasundari Department of ISE, PESIT 17
Ex: class ThrowsDemo { static void throwOne() throws IllegalAccessException { System.out.println("Inside throwOne."); Throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwOne(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } Ms. Nagasundari Department of ISE, PESIT 18 } Depending upon how the method is coded, it is even possible for an exception to cause the method to return prematurely. This could be a problem in some methods. The finally keyword is designed to address this
contingency. Finally creates a block of code that will be executed
after a try/catch block has completed and before
the code following the try/catch block. The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will
execute even if no catch statement matches the
exception. Ms. Nagasundari Department of ISE, PESIT 19 This can be useful for closing file handles and freeing up any other resources that might have been allocated at the beginning of a method with the intent of disposing of them before returning.
The finally clause is optional. However, each
try statement requires at least one catch or a finally clause.
try { procA(); } catch (Exception e) { System.out.println("Exception caught"); } } Ms. Nagasundari Department of ISE, PESIT 21 Inside the standard package java.lang, Java defines several exception classes.
The most general of these exceptions are
subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available.
Java defines several other types of exceptions
that relate to its various class libraries. Ms. Nagasundari Department of ISE, PESIT 22 Ms. Nagasundari Department of ISE, PESIT 23