One Try block and two catch Blocks

Source: Internet
Author: User

A program contains one try block and two catch blocks. Both catch clauses can catch exceptions from one try block. Will the program result change if the order of the two catch clauses is different?

There are two catch blocks after a try block, which is normal because the try block contains many statements and may throw different exceptions, only multiple catch blocks are used to capture different exceptions. If the two exceptions have an inheritance relationship, you should place the subclass exceptions before your class exceptions to capture them.

 

Java code

// If both XxException and YyException can capture ZzException try {1M ..... // XxException and ZzException 2N may be thrown ..... // 3 p ..... // may throw YyException and ZzException} catch ([color = # FF0000] XxException [/color] e1) {....... // execute code A} catch ([color = # FF0000] YyException [/color] e2) {B .......} // If ZzException occurs on 1, code A is not executed directly on 2 and 3.

 

Java code
// If both xxexception and yyexception can capture zzexception try {1m ..... // xxexception and zzexception 2n may be thrown ..... // 3 p ..... // may throw yyexception and zzexception} catch ([color = # ff0000] yyexception [/color] E2) {B .......} catch ([color = # ff0000] xxexception [/color] E1) {.......} // code A is still executed after the swapping order

However, if you add the first code to throw

Java code
// If both xxexception and yyexception can capture zzexception try {1m ..... // xxexception and zzexception 2n may be thrown ..... // 3 p ..... // may throw yyexception and zzexception} catch (xxexception E1) {....... [color = # 0000ff] Throw e1 [/color] // The system will continue to throw an exception and execute code a} catch (yyexception E2) {// The system will continue to be caught, execute B code B .......} // If zzexception occurs on 1, code A is not executed directly on 2 and 3.

 

 

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.