Home » Java programming language

Is it necessary that each try block must be followed by a catch block in Java?

Here, we are going to learn that: Is it necessary that each try block must be followed by a catch block in Java programming language?
Submitted by Preeti Jain, on September 20, 2019

The question is that "Is it necessary that each try block must be followed by a catch block in Java?"

The answer is "No, it is not mandatory that each try block must be followed by a catch block in Java."

  • After try block, we can use either "catch" block or "finally" block.
  • Generally, thrown exceptions should be declared in the thrown clause of the method.
  • To understand the try-catch block, we will discuss three cases:
    1. What will happen, if each try block must be followed by a catch block?
    2. What will happen, if each try block must be followed by a finally block?
    3. What will happen, if each try block must be followed by both catch and finally block?

In the few steps, we will explore each of the above cases one by one with the help of an example,

1) Each try block is followed by a catch block

Example:

// Java program to demonstrate the example of
// try-catch block hierarchy 

public class TryCatchBlock {
    public static void main(String[] args) {

        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

Output

java.lang.ArithmeticException: / by zero
        at TryCatchBlock.main(TryCatchBlock.java:8)

2) Each try block is followed by a finally block

Example:

// Java program to demonstrate the example of
// try-finally block hierarchy 	

public class TryFinallyBlock {
    public static void main(String[] args) {
        
        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
        
    }
}

Output

Code which must be executed : Whether Exception throw or not throw

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at TryFinallyBlock.main(TryFinallyBlock.java:11)

3) Each try block is followed by both catch and finally block

Example:

// Java program to demonstrate the example of
// try-catch-finally block hierarchy 

public class TryCatchFinallyBlock {
    public static void main(String[] args) {
        int i1 = 10;
        int i2 = 0;
        
        try {
            int result = i1 / i2;
        
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            int result = i1 + i2;
            System.out.println("The addition of i1,i2 is" + " " + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
    }
}

Output

The addition of i1,i2 is 10
Code which must be executed : Whether Exception throw or not throw

The combination of try, catch and finally given below are valid and we have seen with the help of an example given above,

  • try-catch block
  • try-catch-finally block
  • try-finally block


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.