9.2.6 寮傚父嫻佺▼鐨勮繍琛岃繃紼?/h4>
錛?錛塮inally璇彞涓嶈鎵ц鐨勫敮涓鎯呭喌鏄厛鎵ц浜嗙敤浜庣粓姝㈢▼搴忕殑System.exit()鏂規硶銆?br />
錛?錛塺eturn璇彞鐢ㄤ簬閫鍑烘湰鏂規硶銆?br />
錛?錛塮inally浠g爜鍧楄櫧鐒跺湪return璇彞涔嬪墠琚墽琛岋紝浣唂inally浠g爜鍧椾笉鑳介氳繃閲嶆柊緇欏彉閲忚祴鍊兼潵鏀瑰彉return璇彞鐨勮繑鍥炲箋?br />
錛?錛夊緩璁笉瑕佸湪finally浠g爜鍧椾腑浣跨敤return璇彞錛屽洜涓哄畠浼氬鑷翠互涓嬩袱縐嶆綔鍦ㄧ殑閿欒
A錛氳鐩杢ry鎴朿atch浠g爜鍧楃殑return璇彞
public class SpecialException extends Exception {
public SpecialException() {
}
public SpecialException(String msg) {
super(msg);
}
}
public class FinallyReturn {
/**
* @param args
*/
public static void main(String[] args) {
FinallyReturn fr = new FinallyReturn();
System.out.println(fr.methodB(1));// 鎵撳嵃100
System.out.println(fr.methodB(2));// 鎵撳嵃100
}
public int methodA(int money) throws SpecialException {
if (--money <= 0) {
throw new SpecialException("Out of money");
}
return money;
}
@SuppressWarnings("finally")
public int methodB(int money) {
try {
return methodA(money);// 鍙兘鎶涘嚭寮傚父
} catch (SpecialException e) {
return -100;
} finally {
return 100;// 浼氳鐩杢ry鍜宑atch浠g爜鍧楃殑return璇彞
}
}
}
B錛氫涪澶卞紓甯?br />
public class ExLoss {
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(new ExLoss().methodB(1));// 鎵撳嵃100
System.out.println("No Exception");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public int methodA(int money) throws SpecialException {
if (--money <= 0) {
throw new SpecialException("Out of money");
}
return money;
}
@SuppressWarnings("finally")
public int methodB(int money) {
try {
return methodA(money);// 鍙兘鎶涘嚭寮傚父
} catch (SpecialException e) {
throw new Exception("Wrong");
} finally {
return 100;// 浼氫涪澶眂atch浠g爜鍧椾腑鐨勫紓甯?/span>
}
}
}
9.3 Java寮傚父綾?/h3>
鎵鏈夊紓甯哥被鐨勭鍏堢被涓簀ava.lang.Throwable綾伙紝瀹冪殑瀹炰緥琛ㄧず鍏蜂綋鐨勫紓甯稿璞★紝鍙互閫氳繃throw璇彞鎶涘嚭銆?br />
Throwable綾繪彁渚涗簡璁塊棶寮傚父淇℃伅鐨勪竴浜涙柟娉曪紝甯哥敤鐨勬柟娉曞寘鎷細
- getMessage() --榪斿洖String綾誨瀷鐨勫紓甯鎬俊鎭?
- printStachTrace()--鎵撳嵃璺熻釜鏂規硶璋冪敤鏍堣岃幏寰楃殑璇︾粏寮傚父淇℃伅銆傚湪紼嬪簭璋冭瘯闃舵錛屾鏂規硶鍙敤浜庤窡韙敊璇?
public class ExTrace {
/**
* @param args
*/
public static void main(String[] args) {
try {
new ExTrace().methodB(1);
} catch (Exception e) {
System.out.println("--- Output of main() ---");
e.printStackTrace();
}
}
public void methodA(int money) throws SpecialException {
if (--money <= 0) {
throw new SpecialException("Out of money");
}
}
public void methodB(int money) throws Exception {
try {
methodA(money);
} catch (SpecialException e) {
System.out.println("--- Output of methodB() ---");
System.out.println(e.getMessage());
throw new Exception("Wrong");
}
}
}
鎵撳嵃緇撴灉錛?br />
--- Output of methodB() ---
Out of money
--- Output of main() ---
java.lang.Exception: Wrong
at chapter09.d0903.ExTrace.methodB(ExTrace.java:45)
at chapter09.d0903.ExTrace.main(ExTrace.java:26)
Throwable綾繪湁涓や釜鐩存帴瀛愮被錛?br />
- Error綾?-琛ㄧず浠呴潬紼嬪簭鏈韓鏃犳硶鎭㈠鐨勪弗閲嶉敊璇紝濡傚唴瀛樹笉瓚崇瓑銆?
- Exception綾?-琛ㄧず紼嬪簭鏈韓鍙互澶勭悊鐨勫紓甯搞?
9.3.1 榪愯鏃跺紓甯?/h4>
RuntimeException綾誨強鍏跺瓙綾婚兘琚О涓鴻繍琛屾椂寮傚父錛岃繖縐嶅紓甯哥殑鐗圭偣鏄疛ava緙栬瘧鍣ㄤ笉浼氭鏌ュ畠錛屼細緙栬瘧閫氳繃錛屼絾榪愯鏃跺鏋滄潯浠舵垚绔嬪氨浼氬嚭鐜板紓甯搞?br />
渚嬪褰撲互涓媎ivided()鏂規硶鐨勫弬鏁癰涓?錛屾墽琛?#8220;a/b”鎿嶄綔鏃朵細鍑虹幇ArrithmeticException寮傚父錛屽畠灞炰簬榪愯鏃跺紓甯革紝Java緙栬瘧鍣ㄤ笉浼氭鏌ュ畠銆?br />
public int divide2(int a, int b) {
return a / b;// 褰撳弬鏁頒負0錛屾姏鍑篈rrithmeticException
}
涓嬮潰鐨勭▼搴忎腑鐨処llegalArgumentException涔熸槸榪愯鏃跺紓甯革紝divided()鏂規硶鍗蟲病鏈夋崟鑾峰畠錛屼篃娌℃湁澹版槑鎶涘嚭瀹冦?br />
public class WithRuntimeEx {
/**
* @param args
*/
public static void main(String[] args) {
new WithRuntimeEx().divide(1, 0);
System.out.println("End");
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("闄ゆ暩涓嶈兘鐐?");
}
return a / b;
}
}
鐢變簬紼嬪簭浠g爜涓嶄細澶勭悊榪愯鏃跺紓甯革紝鍥犳褰撶▼搴忓湪榪愯鏃跺嚭鐜頒簡榪欑寮傚父鏃訛紝灝變細瀵艱嚧紼嬪簭寮傚父緇堟銆備互涓婄▼搴忕殑鎵撳嵃緇撴灉涓猴細
Exception in thread "main" java.lang.IllegalArgumentException: 闄ゆ暩涓嶈兘鐐?
at chapter09.d0903.WithRuntimeEx.divide(WithRuntimeEx.java:29)
at chapter09.d0903.WithRuntimeEx.main(WithRuntimeEx.java:23)
9.3.2 鍙楁鏌ュ紓甯?/h4>
闄や簡RuntimeException鍙婂叾瀛愮被浠ュ錛屽叾浠栫殑Exception綾誨強鍏跺瓙綾婚兘灞炰簬鍙楁鏌ュ紓甯革紙Checked Exception錛夈傝繖縐嶅紓甯歌涔坈atch璇彞鎹曡幏錛岃涔坱hrows瀛愬彞澹版槑鎶涘嚭錛屽惁鍒欑紪璇戝嚭閿欍?br />
9.3.3 鍖哄垎榪愯鏃跺紓甯稿拰鍙楁鏌ュ紓甯?/h4>
鍙楁鏌ュ紓甯歌〃紺虹▼搴忓彲浠ュ鐞嗙殑寮傚父銆?br />
榪愯鏃跺紓甯歌〃紺烘棤娉曡紼嬪簭鎭㈠榪愯鐨勫紓甯革紝瀵艱嚧榪欑寮傚父鐨勫師鍥犻氬父鏄敱浜庢墽琛屼簡閿欒鎿嶄綔銆備竴鏃﹀嚭鐜頒簡閿欒鎿嶄綔錛屽緩璁粓姝㈢▼搴忥紝鍥犳Java緙栬瘧鍣ㄤ笉媯鏌ヨ繖縐嶅紓甯搞?br />
9.3.4 鍖哄垎榪愯鏃跺紓甯稿拰閿欒
Error綾誨強鍏跺瓙綾昏〃紺虹▼搴忔湰韜棤娉曚慨澶嶇殑閿欒錛屽畠鍜岃繍琛屾椂寮傚父鐨勭浉鍚屼箣澶勬槸錛欽ava緙栬瘧鍣ㄩ兘涓嶄細媯鏌ュ畠浠紝褰撶▼搴忚繍琛屾椂鍑虹幇瀹冧滑錛岄兘浼氱粓姝㈢▼搴忋?br />
涓よ呯殑涓嶅悓涔嬪鏄細Error綾誨強鍏跺瓙綾昏〃紺虹殑閿欒閫氬父鏄敱Java铏氭嫙鏈烘姏鍑恒?br />
鑰孯untimeException綾昏〃紺虹▼搴忎唬鐮佷腑鐨勯敊璇紝瀹冩槸鍙墿灞曠殑錛岀敤鎴峰彲浠ユ牴鎹壒瀹氱殑闂棰嗗煙鏉ュ垱寤虹浉鍏崇殑榪愯鏃跺紓甯哥被銆?br />
9.4 鐢ㄦ埛瀹氫箟寮傚父
9.4.1 寮傚父杞瘧鍜屽紓甯擱摼
public class BaseException extends Exception {
protected Throwable cause = null;
public BaseException() {
}
public BaseException(String msg) {
super(msg);
}
public BaseException(Throwable cause) {
this.cause = cause;
}
public BaseException(String msg, Throwable cause) {
super(msg);
this.cause = cause;
}
public Throwable initCause(Throwable cause) {
this.cause = cause;
return this;
}
public Throwable getCause() {
return cause;
}
public void printStackTrace() {
printStackTrace(System.err);
}
public void printStackTrace(PrintStream outStream) {
printStackTrace(new PrintStream(outStream));
}
public void printStackTrace(PrintWriter writer) {
super.printStackTrace(writer);
if (getCause() != null) {
getCause().printStackTrace(writer);
}
writer.flush();
}
}
9.4.2 澶勭悊澶氭牱鍖栧紓甯?/h4>
public class MultiBaseException extends Exception {
protected Throwable cause = null;
private List<Throwable> exceptions = new ArrayList<Throwable>();
public MultiBaseException() {
}
public MultiBaseException(Throwable cause) {
this.cause = cause;
}
public MultiBaseException(String msg, Throwable cause) {
super(msg);
this.cause = cause;
}
public List getException() {
return exceptions;
}
public void addException(MultiBaseException ex) {
exceptions.add(ex);
}
public Throwable initCause(Throwable cause) {
this.cause = cause;
return this;
}
public Throwable getCause() {
return cause;
}
public void printStackTrace() {
printStackTrace(System.err);
}
public void printStackTrace(PrintStream outStream) {
printStackTrace(new PrintStream(outStream));
}
public void printStackTrace(PrintWriter writer) {
super.printStackTrace(writer);
if (getCause() != null) {
getCause().printStackTrace(writer);
}
writer.flush();
}
}
9.5 寮傚父澶勭悊鍘熷垯
9.5.1 寮傚父鍙兘鐢ㄤ簬闈炴甯告儏鍐?/h4>
9.5.2 涓哄紓甯告彁渚涜鏄庢枃妗?/h4>
9.5.3 灝藉彲鑳藉湴閬垮厤寮傚父
9.5.4 淇濇寔寮傚父鐨勫師瀛愭?/h4>
9.5.5 閬垮厤榪囦簬搴炲ぇ鐨則ry浠g爜鍧?/h4>
9.5.6 鍦╟atch瀛愬彞涓寚瀹氬叿浣撶殑寮傚父綾誨瀷
9.5.7 涓嶈鍦╟atch浠g爜鍧椾腑蹇界暐琚崟鑾風殑寮傚父錛屽彲浠ュ鐞嗗紓甯搞侀噸鏂版姏鍑哄紓甯搞佽繘琛屽紓甯歌漿璇?/h4>

]]>