Catch All Exceptions in Java

How to catch all exceptions in Java? In Java, We can catch all exceptions with a single catch block using the “java.lang.Exception” parameter. We should use this catch block only for stopping abnormal terminations irrespective of the exception thrown from its corresponding try statement. 

Note:- We can’t handle Error type exceptions either by using catch block or throws keyword. Example:- OutOfMemoryError, StackOverflowError, VirtualMachineError, NoClassDefFoundError and e.t.c. Because these types of exceptions are caused due to the lack of system resources. Errors are not recoverable. If OutOfMemoryError occurs, being a programmer we can’t do anything & the program will be terminated abnormally. System admin or server admin is responsible to increase heap memory.

Program to demonstrate how to catch all exceptions in Java

class Division{
  public static void main(String[] args) {
    try{
      int a = Integer.parseInt(args[0]);
      int b = Integer.parseInt(args[1]);
      int c = a/b;
      System.out.println("Result: " + c);
    } catch(Exception e){
      System.out.println("Exception in the program");
    }
  }
}

Executing Division class without passing any value:- Since we are not passing any value therefore ArrayIndexOutOfBoundsException will be raised which will be handled by the catch block.

> java Division
Exception in the program

Executing Division class by passing string:- Inside program parseInt() is converting the passed value to integer number. If the passed value is a string then it will throw a NumberFormatException which will be handled by the catch block.

> java Division A
Exception in the program

Executing Division class by passing only one value:- We are calling args[1] but passed only one value therefore ArrayIndexOutOfBoundsException will be raised but handled by the catch block.

> java Division 10
Exception in the program

Executing Division class by passing only one integer value and one string:- The second value is not a number therefore NumberFormatException will be raised but handled by the catch block.

> java Division 10 a
Exception in the program

Passing only the second value as 0:- The second number is zero so, an ArithmeticException exception will occur.

> java Division 10 0
Exception in the program

Passing valid values to get the output message:-

> java Division 10 2
Result: 5

Based on the above all test-cases we can conclude that due to the “java.lang.Exception” parameter it can handle all exceptions, which was raised during the program execution. No need to write multiple catch blocks for different exceptions.

But we can also observe that there are some cons. Whenever an exception is raised, the catch block will be executed and for different wrong values, we are not able to figure out what is the problem in the program. 

For example:- Our program gives the message “Exception in the program” when we pass the second number as a string (NumberFormatException), and it again gives the same output when the second number was zero (ArithmeticException). So we will not find the actual reason for the exception.

Hence, It is always recommended to write a catch block with an Exception parameter even though we are writing multiple catch blocks. 

We must always define Exception parameter catch block in the below two situations,

  • As a backup catch of all catch blocks, or
  • To stop abnormal terminations irrespective of the exception raised in the program

Coding standards for handling exceptions:- We can use multiple catch blocks for handling known exceptions, but the last catch block should have a java.lang.Exception parameter which will be used to handle the unknown exception which may be raised in the program. The last catch block will be treated as a backup catch block, and program execution will be terminated normally. 

Sample program to catch All Exceptions in Java

class Division{
  public static void main(String[] args) {
    try{
      int a = Integer.parseInt(args[0]);
      int b = Integer.parseInt(args[1]);
      int c = a/b;
      System.out.println("Result: " + c);
    } catch(ArrayIndexOutOfBoundsException aiobe){
      System.out.println("Enter 2 integer values");
    } catch(NumberFormatException nfe){
      System.out.println("Passed value should be integer");
    } catch(ArithmeticException ae){
      System.out.println("2nd Number is 0, pass other value");
    } catch(Exception e){
      // for backup
      System.out.println("Exception in the program");
    }
  }
}

Output for the different input values:-

>java Division
Enter 2 integer values

>java Division A
Passed value should be integer

>java Division 10
Enter 2 integer values

>java Division 10 A
Passed value should be integer

>java Division 10 0
2nd Number is 0, pass other value

>java Division 10 2
Result: 5

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *