Nested try block in Java:
Java allows the use of try blocks inside of other try blocks. The term “nested try block” describes it. The context of the exception is pushed onto the stack with each sentence we enter in a try block.
For instance, the outer try block can handle the ArithemeticException whereas the inside try block can handle the ArrayIndexOutOfBoundsException (division by zero).
The purpose of nested try blocks:
There are situations when a block may have a portion that causes one mistake while the complete block itself causes a different error. Exception handlers must be nested in such circumstances.
Syntax:
//main try block
try
{
statement 1;
statement 2;
//try catch block within another try block
try
{
statement 3;
statement 4;
//try catch block within nested try block
try
{
statement 5;
statement 6;
}
catch(Exception e2)
{
//exception message
}
}
catch(Exception e1)
{
//exception message
}
}
//catch block of parent (outer) try block
catch(Exception e3)
{
//exception message
}
....
Example:
public class NestedTryBlock {
public static void main(String args[])
{
// outer (main) try block
try {
//inner try block 1
try {
// inner try block 2
try {
int arr[] = { 1, 2, 3, 4 };
//printing the array element out of its bounds
System.out.println(arr[10]);
}
// to handles ArithmeticException
catch (ArithmeticException e) {
System.out.println("Arithmetic exception");
System.out.println(" inner try block 2");
}
}
// to handle ArithmeticException
catch (ArithmeticException e) {
System.out.println("Arithmetic exception");
System.out.println("inner try block 1");
}
}
// to handle ArrayIndexOutOfBoundsException
catch (ArrayIndexOutOfBoundsException e4) {
System.out.print(e4);
System.out.println(" outer (main) try block");
}
catch (Exception e5) {
System.out.print("Exception");
System.out.println(" handled in main try-block");
}
}
}
Output:
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 4 outer (main) try block
The try-with-resources statement:
A try statement that declares one or more resources is known as a try-with-resources statement in Java. A resource is an object that needs to be closed once the program is finished. Each resource is closed after the statement execution thanks to the try-with-resources statement.
Any object that complies with Java.lang can be passed.
You can pass any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable.
Syntax:
try(resource-specification(there can be more than one resource))
{
//use the resource
}
catch()
{
// handler code
}
Example:
import java.io.*;
class Demo
{
public static void main(String[] args) throws FileNotFoundException
{
BufferedReader br = new BufferedReader(new FileReader("d:\\myfile.txt"));
try(br) // resource is declared outside the try
{
String str;
while((str = br.readLine()) != null)
{
System.out.println(str);
}
}
catch(IOException ie)
{
System.out.println("I/O Exception "+ie);
}
}
}
Note: also read about the Java Multi-catch block
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment