Open In App

Is it Necessary that each Try block must be followed by a Catch block in Java?

Last Updated : 08 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Have you ever wondered if it is necessary to write a catch statement after try , or what will happen if we don't write catch statement after each try rather we write a single catch for all try statement. Well in this post we are going to discuss just about that, but before that let us first discuss about the try and catch statement and its implementation.

Introduction:

The try...catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if it throws an exception, the code in the catch the block will be executed. The code in the finally block will always be executed before the control flow exits the entire construct.

Syntax:

try {
tryStatements
} catch (exceptionVar) {
catchStatements
} finally {
finallyStatements
}
  • tryStatements: The statements to be executed.
  • catchStatements: A statement that is executed if an exception is thrown in the try block.
  • exceptionVar: An optional identifier to hold the caught exception for the associated catch block. If the catch block does not use the exception's value, you can omit the exceptionVar and its surrounding parentheses.
  • finallyStatements: Statements that are executed before control flow exits the try...catch...finally construct. These statements execute regardless of whether an exception was thrown or caught.

Is it Necessary that each Try block must be followed by a Catch?

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. Behaviour when each try block must be followed by a catch block?
    2. Behaviour when each try block must be followed by a finally block?
    3. Behaviour when 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

Conclusion:

It is not mandatory that each try block must be followed by a catch block in Java.

Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.

And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg