Unreachable Catch Blocks:
Program:
\Java Example program on unreachable catch block
Output:
- The block of statements to which the control would never reach under any case can be called as unreachable blocks.
- Unreachable blocks are not supported by java.
- Thus catch block mentioned with the reference of "Exception" class should and must be always last catch block. Because Exception is super class of all exceptions.
Program:
- package com.instanceofjava;
- public class ExcpetionDemo {
- public static void main(String agrs[])
- {
- try
- {
- //statements
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- catch(ArithmeticException e)//unreachable block.. not supported by java. leads to error
- System.out.println(e);
- }
- }
\Java Example program on unreachable catch block
- package com.instanceofjava;
- public class ExcpetionDemo {
- public static void main(String agrs[])
- {
- try {
- System.out.println("Excpetion handling interview questions Java unreachable block");
- } catch (IllegalThreadStateException e) {
- e.printStackTrace();
- }catch (IllegalArgumentException e) {
- e.printStackTrace();
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Output:
- Excpetion handling interview questions Java unreachable block
- package com.instanceofjava;
- public class ExcpetionDemo {
- public static void main(String agrs[])
- {
- try {
- System.out.println("Excpetion handling interview questions Java unreachable block");
- }
- catch (Exception e) {
- e.printStackTrace();
- }catch (IllegalThreadStateException e) {//Error: Unreachable catch block for
- //IllegalThreadStateException. It is already handled by the catch block for Exception
- e.printStackTrace();
- }catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- }
- }
Bagikan
Unreachable Blocks in java
4/
5
Oleh
Kris Kimcil