Sr.Java Web Developer Question:

Download Job Interview Questions and Answers PDF

Please explain can we write multiple catch blocks under single try block?

Sr.Java Web Developer Interview Question
Sr.Java Web Developer Interview Question

Answer:

Yes we can have multiple catch blocks under single try block but the approach should be from specific to general. Let’s understand this with a programmatic example.

public class Example {
public static void main(String args[]) {
try {
int a[]= new int[10];
a[10]= 10/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic exception in first catch block");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out of bounds in second catch block");
}
catch(Exception e)
{
System.out.println("Any exception in third catch block");
}
}

Download Sr.Java Web Developer Interview Questions And Answers PDF

Previous QuestionNext Question
Do you know how to create a custom Exception?What is String toString()?