2016-06-30
1 Abnormal chain
Two or more different exceptions appear in the same program, and nested throws occur, which we call the exception chain.
Packagecom.java1995;/*** Abnormal chain *@authorAdministrator **/ Public classExceptionchaintest { Public Static voidMain (string[] args) {Calculator C=NewCalculator (); Try{C.chufa (1, 0); }Catch(numbercalculateexception e) {e.printstacktrace (); System.out.println ("Cause of error" +e); }; }}classcalculator{/*** Division *@return * @throwsnumbercalculateexception*/ Public intChufa (intIintJthrowsnumbercalculateexception{if(j==0) {numbercalculateexception e=NewNumbercalculateexception ("Calculation Error"); Negativenumberexception E1=NewNegativenumberexception ("Divisor cannot be 0"); E.initcause (E1); Throwe; } return0; };}classNegativenumberexceptionextendsException { Publicnegativenumberexception (String msg) {Super(msg); }}classNumbercalculateexceptionextendsException { Publicnumbercalculateexception (String msg) {Super(msg); }}
Resources
[1] Java Easy Start Classic tutorial "full version"
Java section 50th, the exception chain in Java