2. Draw the hierarchy of exception.
3. Difference between Checked and Unchecked Exception.
4.What is the order of catch blocks when catching more than one exception?
case 1: Exception catch block defined before NumberFormatException catch block will throw an unreachable catch block error. Because it is already handled by Exception catch block.
public class MultipleCatchBlock {
public static void main(String[] args) {
try {
int a=Integer.parseInt("One");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
catch(NumberFormatException e) {
System.out.println(e.getMessage());
}
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for NumberFormatException. It is already handled by the catch block for Exception at Batch4.MultipleCatchBlock.main(MultipleCatchBlock.java:11)
case 2: NumberFormatException catch block defined before Exception catch block handles the specified exception and terminate the program normally without showing any error.
public class MultipleCatchBlock {
public static void main(String[] args) {
try {
int a=Integer.parseInt("One");
}
catch(NumberFormatException e) {
System.out.println(e.getMessage());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Output:
java.lang.NumberFormatException: For input string: "One"
5. How many different ways to print the Exception message?
There are three different way to print exception information in different format as follow:
- printStackTrace() → prints Exception package and class: Name of the exception and its path.
- toString() → prints Exception package class: Name of the exception
- getmessage() → prints only Exception name
public class ExceptionDemo {
public static void main(String[] args) {
try {
System.out.println(25/0);
}
catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
}
System.out.println("Learn with Durga");
}
}
Output:
java.lang.ArithmeticException: / by zero
at Batch4.ExceptionDemo.main(ExceptionDemo.java:6)
/ by zero
java.lang.ArithmeticException: / by zero
Learn with Durga
Note: default exception handler internally uses printStackTrace() method to print exception information to the console.
6. Describe behavior of following exception?
RuntimeException - unchecked
Error -unchecked
IOException - fully checked
Exception - partially checked
InterruptedException - fully checked
CloneNotSupportedException - checked
Throwable - partially checked
ArithmeticException - unchecked
ConcurrentModificationException - unchecked
NullPointerException - unchecked
FileNotFoundException - fully checked
7. Difference between throw and throws.
8. Which are the valid declaration of try catch and finally block as in the following options?
9. What is finally block?
finally block is used at the end of exception handling try-catch block and it terminated the exception thrown by the try block even it is handled/not handled or checked/unchecked. finally block will not executed if there is no try block mentioned in the program.


No comments:
Post a Comment