0% found this document useful (0 votes)
13 views

Lecture 16 Exception Handling

The document discusses nested try-catch blocks in Java. It explains that try blocks can be nested within each other and each try block will have its own context on the exception stack. If an inner exception is not caught, the next outer block will be checked. Finally blocks will execute after try-catch regardless of exceptions. Exceptions can also be explicitly thrown using the throw keyword.

Uploaded by

Amitesh Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 16 Exception Handling

The document discusses nested try-catch blocks in Java. It explains that try blocks can be nested within each other and each try block will have its own context on the exception stack. If an inner exception is not caught, the next outer block will be checked. Finally blocks will execute after try-catch regardless of exceptions. Exceptions can also be explicitly thrown using the throw keyword.

Uploaded by

Amitesh Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lecture 26

Prof. Anita Agrawal


BITS Pilani, Goa
▪ The try block within a try block is known as nested try block.
▪ Sometimes a situation may arise where a part of a block may cause one
error and the entire block itself may cause another error. In such cases,
exception handlers have to be nested.
▪ 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 next try statement’s catch handlers are inspected for a match
▪ This continues until one of the catch statements succeed
▪ If no catch statement matches
▪ Then, the Java run-time system will handle the exception
Aita Agrawal (CS F213) 4/17/2023 7:41 AM 2
//Main try block
try {
statement 1;
statement 2;
//try-catch block inside another try block
try {
statement 3;
statement 4;
//try-catch block inside nested try block
try {
statement 5;
statement 6;
}
catch(Exception e2) {
//Exception Message
}
}
catch(Exception e1) {
//Exception Message
}

}
//Catch of Main(parent) try block
catch(Exception e3) {
//Exception Message
Aita Agrawal (CS F213) } 4/17/2023 7:41 AM 3
▪ Java finally block is a block that is used to execute important code such as
closing connection, stream etc.
▪ 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
▪ Useful for closing file handles and freeing up any other reserved resources
▪ The finally clause is optional
▪ However, each try statement requires at least one catch or a finally block

Aita Agrawal (CS F213) 4


▪ We are only catching exceptions that are thrown by the
Java run-time system
▪ It is possible for your program to throw an exception
explicitly
▪ The throw keyword in Java is used to explicitly throw an
exception from a method or any block of code.
▪ We can throw either checked or unchecked exception.
▪ The throw keyword is mainly used to throw custom
exceptions.
▪ The general form of throw is
throw ThrowableInstance
Aita Agrawal (CS F213) 4/17/2023 7:41 AM 5
▪ ThrowableInstance must be an object of type Throwable or a
subclass of Throwable
▪ Primitive types and object of String and Object cannot be used as
exceptions.
▪ The flow of execution stops immediately after the throw statement
▪ The nearest enclosing try block is inspected to see
▪ Whether a catch statement that matches the type of exception is
available
▪ If it does, control is transferred to that statement

Aita Agrawal (CS F213) 4/17/2023 7:41 AM 6


▪ 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
▪ Prints the stack trace

Aita Agrawal (CS F213) 4/17/2023 7:41 AM 7

You might also like