Java know how much (47) use of 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:
1 //demonstrate multiple catch statements.2 classMulticatch {3      Public Static voidMain (String args[]) {4         Try {5             intA =args.length;6System.out.println ("a =" +a);7             intb = 42/A;8             intC[] = {1 };9C[42] = 99;Ten}Catch(ArithmeticException e) { OneSystem.out.println ("Divide by 0:" +e); A}Catch(arrayindexoutofboundsexception e) { -SYSTEM.OUT.PRINTLN ("Array index OOB:" +e); -         } theSystem.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 in two different situations:
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:
1 /*This program contains an error.2 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.3 */4 classSupersubcatch {5      Public Static voidMain (String args[]) {6         Try {7             intA = 0;8             intb = 42/A;9}Catch(Exception e) {TenSystem.out.println ("Generic Exception catch.")); One         } A         /*This catch is never reached because - ArithmeticException is a subclass of Exception.*/ -         Catch(ArithmeticException e) {//error-unreachable theSystem.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.

Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

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.