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.