GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java Multiple Catch Blocks
Selected Reading
Java Multiple Catch BlocksMultiple Catch Blocks in JavaMultiple catch blocks in Java are used to catch/handle multiple exceptions that may be thrown from a particular code section. A try block can have multiple catch blocks to handle multiple exceptions. Syntax: Multiple Catch Blockstry {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}
The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes down to the second catch statement. This continues until the exception either is caught or falls through all catches, in which case the current method stops execution and the exception is thrown down to the previous method on the call stack. Points to Remember while using Multiple Catch Blocks
Example of Java Multiple Catch BlocksHere is code segment showing how to use multiple try/catch statements. In this example, we're creating an error by dividing a value by 0. As it is not an ArrayIndexOutOfBoundsException, next catch block handles the exception and prints the details. package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
try {
int a[] = new int[2];
int b = 0;
int c = 1/b;
System.out.println("Access element three :" + a[3]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException thrown :" + e);
}catch (Exception e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
OutputException thrown :java.lang.ArithmeticException: / by zero Out of the block Handling Multiple Exceptions Using Multiple Catch BlocksIn this code segment, we're showing how to use another example of multiple try/catch statements. In this example, we're creating an error by dividing a value by 0 and handling it using ArithmeticException. Examplepackage com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
try {
int a[] = new int[2];
int b = 0;
int c = 1/b;
System.out.println("Access element three :" + a[3]);
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException thrown :" + e);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException thrown :" + e);
}catch (Exception e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
OutputArithmeticException thrown :java.lang.ArithmeticException: / by zero Out of the block Handling Multiple Exceptions Within A Single Catch BlockSince Java 7, you can handle more than one exception using a single catch block, this feature simplifies the code. Here is how you would do it − Syntaxcatch (IOException|FileNotFoundException ex) {
logger.log(ex);
throw ex;
ExampleHere is code segment showing how to use multiple catch in a single statement. In this example, we're creating an error by dividing a value by 0. In single statement, we're handling ArrayIndexOutOfBoundsException and ArithmeticException. package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
try {
int a[] = new int[2];
int b = 0;
int c = 1/b;
System.out.println("Access element three :" + a[3]);
}
catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
OutputException thrown :java.lang.ArithmeticException: / by zero Out of the block Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |