Java basics: exception capture sequence, java capture sequence
Reprinted with the source: jiq •'s technical Blog
Public voidtestException () {int a [] = {1, 2, 3}; int q = 0; try {for (int I = 0; I <. length; I ++) {a [I]/= q ;}} catch (ArithmeticException h) {System. out. print ("ArithmeticException \ n"); // execute} catch (Exception e) {System. out. print ("Exception \ n"); // no execution, the Exception must be placed after the ArithmeticException/*** with a wider range not only must be put behind the Exception *, but will not be executed (blocked by the earlier range * with fewer exceptions ), so what's the significance ??? */} Finally {System. out. print ("finally \ n") ;}// <span style = "color: # 3333ff;"> output </span> ArithmeticExceptionfinally
* Point 1: Although ArithmeticException inherits from Exception, an ArithmeticException occurs.
* When the Exception is captured, only the Exception that actually occurred will be captured, not because the Exception is its parent class.
* Execute the catch clause of Exception.
* Point 2: however, if you try to put the catch statement with a wider range of exceptions in front of the catch statement
* The error "Unreachablecatch block for ArithmeticException.
* Itis already handled by the catch block for Exception"
* An exception with a larger scope (parent class) must be placed behind it. If there is no inheritance relationship, such as ClassNotFoundException,
* There is no sequential relationship between catch clauses of ArithmeticException.