Handling Multiple Exceptions in java

Handling Multiple Exceptions in java

In Java programming, programmers can contain multiple system-defined or user-generated errors or exceptions within a program source code block. Therefore, Java users can easily handle such program situations by implementing the multi-catch feature, introduced in Java version 7, or multiple catch blocks. Java provides users with several mechanisms to handle any type of program exception in a try-catch program statement.

Handling Multiple Exceptions in java

Multiple Catch Blocks/Before Java 7 version.

Prior to Java version 7, Java developers had to use the multiple catch block function to handle or manage individual types of program exception errors. Each user-defined catch block handled or managed a specific program exception. When an exception was thrown in a user-generated program, the program would move to the first matching catch block.

Example of Java Multiple Catch Blocks.

public class MultipleCatchIllustration {

public static void main(String[] args) {

try {

int[] array = new int[7];

System.out.println(array[10]); // here This will throw an ArrayIndexOutOfBoundsException error in program

int output = 20 / 0; // here This will throw an ArithmeticException

}

catch (ArrayIndexOutOfBoundsException e) {

System.out.println(“here Array Index Out of Bounds Exception generated”);

}

catch (ArithmeticException e) {

System.out.println(“here it caught Arithmetic program Exception”);

}

}

}

In the above example.

  • Here, Java users have two different types of catch blocks defined to handle individual program exceptions.

Java Multi-Catch Features/In Java 7 and later versions.

The multi-catch block feature was built into the Java programming language in all newer Java versions from version 7 onwards, which provides Java developers with the ability to catch or handle multiple program error exceptions within a single program catch block. A multi-catch block in Java is defined to define different error exception types. It is created using the | operator (pipe) in a Java multi-catch program.

Syntax of Java Multi-Catch.

catch (ExceptionType1 | ExceptionType2 | ExceptionType3 | ExceptionType4 e) {

// Here you can handle a user-defined program exception

}

Java Multi-Catch Example.

public class MultiCatchIllustration {

public static void main(String[] args) {

try {

int[] array = new int[9];

System.out.println(array[13]); // here we define an ArrayIndexOutOfBoundsException in program

int output = 30 / 0; // define ArithmeticException here

}

catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {

System.out.println(“here multi-catch program Exception caught – ” + e);

}

}

}

Result – here multi-catch program Exception caught – java.lang.ArrayIndexOutOfBoundsException: Index 13 out of bounds for length 9

In the above example.

  • In the above program, both ArrayIndexOutOfBoundsException and ArithmeticException are caught in a single catch block using the multi-catch syntax.
  • In this program, the variable e is used to access the exception object, regardless of its type.

Special features of the Java multi-catch block.

  • Java users can catch multiple program exception errors in a single program using the multi-catch block.
  • The exceptions used in the multi-catch block should not be in a parent-child relationship. As such, Java users cannot catch both ProgramException and IOException simultaneously, because IOException is defined as a subclass of Exception.
  • The exception variable (e) in the catch block can only be used in one catch block. Because it is implicitly final, it cannot be modified in the program.

Using the finally block in Java.

Whether or not an exception is thrown in the current program or another program, the finally block is always executed after the try-catch block, unless the program exits prematurely (e.g., by calling the System.exit() function method or using a FatalError). This block is mostly used in Java to clean up resources, such as closing a file stream, a database connection, or releasing other resources.

Java finally block example.

public class FinallyBlockIllustration {

public static void main(String[] args) {

try {

int[] array = new int[11];

System.out.println(array[14]); // here define ArrayIndexOutOfBoundsException

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(“here Array Index Out of Bounds Exception caused in final block”);

} finally {

System.out.println(“here Final block ran successfully”);

}

}

}

In the above example.

  • In this program, the catch block catches the user-defined ArrayIndexOutOfBoundsException error.
  • Similarly, the finally block is executed regardless of whether or not a final block exception is thrown in the current program.

Nested Try-Catch Blocks in Java.

Java users can create or define multiple nested multi-catch exception blocks by nesting try-catch blocks within each other in a program. The nested multi-catch block feature can be helpful for Java users when they need to deal with or handle multiple individual program exceptions in individual portions of a complex program operation.

Example of Nested Try-Catch Blocks.

public class NestedTryCatchBlockIllustration {

public static void main(String[] args) {

try {

int[] array = new int[7];

try {

System.out.println(array[9]); // ArrayIndexOutOfBoundsException is defined here

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(“Internal catch: Array Index Out of Bounds nested block exception caught at runtime”);

}

int result = 30 / 0; // ArithmeticException is defined here

} catch (ArithmeticException e) {

System.out.println(“External catch: Arithmetic nested block exception caught”);

}

}

}

In the above example.

  • The internal try-catch block handles the ArrayIndexOutOfBoundsException generated in this program.
  • Similarly, the outer try-catch block handles or manages the ArithmeticException in the program.

Features of the Java multi-catch exception error block.

  • Multiple catch blocks – Used to handle multiple individual exceptions, with a specific exception handling method for each program exception defined in a Java program.
  • Multi-catch (Java 7 and later) – It helps Java users to catch and handle multiple program exceptions within a single catch block.
  • Finally block – The use of the finally block in a Java program ensures that a block of the current program source code continues to run, whether or not a program exception is thrown.
  • Rethrowing an exception – It provides the ability to handle an exception error in a block in the current program and pass it on for further program exception handling.

Leave a Reply