You are reading the article Multiple Catch Block In Java updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Multiple Catch Block In Java
Introduction to Multiple Catch Block in JavaWe can handle exceptions inside our code by inserting try-catch blocks in java. Here in this article, we will see how we can handle multiple exceptions for a single try block by using multiple catch blocks or multiple arguments for a catch block.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
SyntaxSyntax 1: simple try-catch block
try { } { }Syntax 2: try-catch block with multiple catch block
try { } { } { } { }Syntax 3: try-catch block with multiple catch statements as argument of the catch block
try { } { } How Catch Block Work in Java?At first, we need to check on the try block before moving to catch block.
1. Try BlockIt contains the code snippet which may result in exceptions. So, whenever you suspect that your code might result in an exception, put that code snippet inside a try block. At least one catch block is necessary for a try block.
2. Catch BlockIt contains the code snippet which handles the exception that might occur for the code which is written inside its corresponding try block. Now how to deal with multiple exceptions, if any?
You can take two ways:
Use Multiple Catch for a Try Block: Here you can handle multiple exceptions under multiple catch block for a corresponding try block. Program flow will automatically come to the catch block one by one and then, the corresponding catch block is found as per the exception and will be handled accordingly.
Use Multiple Catch Statements in Arguments of A Catch Block: This feature came from Java 7. Here you can handle multiple exceptions under a single catch block (separated by pipe sign) for a corresponding try block. Program flow will automatically come to the catch block and then, the corresponding exception inside the arguments of catch block is found for the try block and will be handled accordingly.
ConstructorWe also can write try-catch blocks inside a constructor and it will work as expected. In the below example, we will write code inside try constructor: try block will generate arithmetic exception which is handled in a catch block.
public class MultipleCatchBlockInConstructor { MultipleCatchBlockInConstructor() { try { int num=6/0; } catch(ArrayIndexOutOfBoundsException excp) { System.out.println("Exception is : "+excp); } catch(ArithmeticException excp) { System.out.println("Exception is : "+ excp); } } public static void main(String[] args) { MultipleCatchBlockInConstructor mc=new MultipleCatchBlockInConstructor(); } }Output:
Examples of Multiple Catch Block in JavaIn this section, we will discuss many code examples. You should try this code by yourself and tally the outputs with the given one.
Example #1Code:
public class MultipleCatchBlockInJava { public static void main(String[] args) { try{ System.out.println("start of try block"); int num = 6/0; } catch(ArithmeticException e) { System.out.println("divide by zero exception "); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception occurs"); } catch(Exception e) { System.out.println("Any other excpetion"); } System.out.println("end of try-catch block"); } }Output:
Example #2In this example, we have illustrated about out-of-bound exception handing for an array by try and catch block.
Code:
public class MultipleCatchBlockInJava { public static void main(String[] args) { try{ System.out.println("start of try block"); int arr[]=new int[6]; System.out.println(arr[12]); } catch(ArithmeticException e) { System.out.println("divide by zero exception"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array IndexOutOfBounds Exception occurs"); } catch(Exception e) { System.out.println("Any other exception"); } System.out.println("end of try-catch block"); } }Output:
Example #3In this example, we will see how the null pointer is handled by a catch block. Also, note that there are multiple scenarios of exceptions in the try block, but once program flow reaches to first exception generating statement (here, Nullpointer exception), it will immediately move out of try block and searches the exception handler in a catch block. The other exception generating statement (here, Array index out of bound exception) inside try block is ignored.
public class MultipleCatchBlockInJava { public static void main(String[] args) { try{ System.out.println("start of try block"); String str=null; System.out.println(str.length()); int arr[]=new int[6]; System.out.println(arr[12]); } catch(ArithmeticException e) { System.out.println("divide by zero exception"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array IndexOutOfBounds Exception occurs"); } catch(NullPointerException e) { System.out.println("Null pointer Exception occurs"); } System.out.println("end of try-catch block"); } }Output:
Example #4In this example, we will check how exception will be handled by multiple arguments of a single catch block.
Code:
public class MultipleCatchBlockInJava { public static void main(String[] args) { try{ System.out.println("start of try block"); String str=null; System.out.println(str.length()); int arr[]=new int[6]; System.out.println(arr[10]); } { System.out.println("Code is throwing " + e); } System.out.println("end of try-catch block"); } }Output:
ConclusionThis concludes our learning for multiple catch blocks from in the java programming perspective. To summarize: Whenever you need to handle multiple exceptions, you can write those inside multiple catch blocks. Alternatively, for java 7 and above you can write multiple exceptions in arguments single catch block and program flow will automatically pick this. Also, it is important that you should write codes by yourself for better understanding.
Recommended ArticlesThis is a guide to Multiple Catch Block in Java. Here we discuss the introduction and constructors of multiple catch block in java along with different examples and code implementation. You may also look at the following articles to learn more –
You're reading Multiple Catch Block In Java
Update the detailed information about Multiple Catch Block In Java on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!