Introduction to the specific use of Java multiple catch statements

Source: Internet
Author: User

In some cases, multiple exceptions may be caused by a single code snippet. In dealing with this situation, you can define two or more catch clauses, each capturing one type of exception. When an exception is thrown, each catch clause is checked sequentially, and the first clause that matches the exception type is executed. When a catch statement is executed, the other clauses are bypassed, and execution proceeds from the code after the Try/catch block. The following example designs two different types of exceptions:
Demonstrate multiple catch statements.
Class Multicatch {
public static void Main (String args[]) {
try {
int a = Args.length;
System.out.println ("a =" + a);
int b = 42/a;
int c[] = {1};
C[42] = 99;
} catch (ArithmeticException e) {
System.out.println ("Divide by 0:" + E);
} catch (ArrayIndexOutOfBoundsException e) {
SYSTEM.OUT.PRINTLN ("Array index OOB:" + e);
}
System.out.println ("After Try/catch blocks.");
}
}

The program runs without command-line arguments, causing the exception to be removed by 0, because a is 0. If you provide a command-line argument, it will survive the difficulty of setting a to a value greater than 0. However, it will cause the Arrayindexoutof boundsexception exception because the length of the integer array c is 1, and the program attempts to assign a value to c[42].

The following is the output of the program running under two different scenarios:
C:\>java Multicatch
A = 0
Divide by 0:java.lang.arithmeticexception:/by zero after try/catch blocks.
C:\>java Multicatch Testarg
A = 1
Array index oob:java.lang.ArrayIndexOutOfBoundsException after try/catch blocks.

When you use multiple catch statements, it is important to remember that the exception subclasses must be used before any of their parent classes. This is because a catch statement that uses the parent class captures the exception of that type and all its subclass types. This way, if the child class is behind the parent class, the subclass will never arrive. Also, code that cannot be reached in Java is an error. For example, consider the following program:
/* This program contains an error.
A subclass must come before its superclass in a series of catch statements. If not,unreachable code would be created and acompile-time error would result.
*/
Class Supersubcatch {
public static void Main (String args[]) {
try {
int a = 0;
int b = 42/a;
} catch (Exception e) {
System.out.println ("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception. */
catch (ArithmeticException e) {//Error-unreachable
System.out.println ("This is never reached.");
}
}
}

If you try to compile the program, you receive an error message stating that the second catch statement will not arrive because the exception is already caught. Because ArithmeticException is a subclass of exception, the first catch statement handles all the exception-oriented errors, including ArithmeticException. This means that the second catch statement will never be executed. Reverses the order of two catch statements in order to modify a program.

More relevant Knowledge

Introduction to the specific use of Java multiple catch statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.