try catch finally用法

來源:互聯網
上載者:User

1.catch不到拋出的異常,則結束此函數的執行,拋出異常位置後面的代碼都不執行

public class Test {</p><p>/**<br /> * @param args<br /> */<br />public static void main(String[] args) {<br />// TODO Auto-generated method stub<br />System.out.println("--- main() start --->");</p><p>hello();</p><p>testSeq();</p><p>System.out.println("--- main() end <---");<br />}</p><p>public static void hello() {<br />try {<br />method();<br />} catch (NullPointerException e) {<br />e.printStackTrace();<br />System.out.println("NullPointerException occured.");<br />} finally {<br />System.out.println("finally...");<br />}</p><p>System.out.println("after hello");<br />}</p><p>public static void method() {<br />//throw new NullPointerException();<br />throw new ArithmeticException();<br />}</p><p>public static String testSeq() {<br />String result = "";<br />int i = 0;</p><p>try {<br />i = 7 / i;<br />System.out.println("in try...");<br />} catch (NullPointerException e) {<br />e.printStackTrace();<br />System.out.println("In catch...");<br />} finally {<br />System.out.println("In finally....");<br />}<br />System.out.println("after...");</p><p>return result;<br />}<br />}

 

輸出結果:

--- main() start --->
finally...
Exception in thread "main" java.lang.ArithmeticException
 at com.min.Test.method(Test.java:35)
 at com.min.Test.hello(Test.java:22)
 at com.min.Test.main(Test.java:13)

 

2.catch到拋出的異常,則繼續執行此函數,拋出異常位置後面的代碼也執行

package com.min;</p><p>public class Test {</p><p>/**<br /> * @param args<br /> */<br />public static void main(String[] args) {<br />// TODO Auto-generated method stub<br />System.out.println("--- main() start --->");</p><p>hello();</p><p>testSeq();</p><p>System.out.println("--- main() end <---");<br />}</p><p>public static void hello() {<br />try {<br />method();<br />} catch (NullPointerException e) {<br />e.printStackTrace();<br />System.out.println("NullPointerException occured.");<br />} finally {<br />System.out.println("finally...");<br />}</p><p>System.out.println("after hello");<br />}</p><p>public static void method() {<br />throw new NullPointerException();<br />//throw new ArithmeticException();<br />}</p><p>public static String testSeq() {<br />String result = "";<br />int i = 0;</p><p>try {<br />i = 7 / i;<br />System.out.println("in try...");<br />} catch (NullPointerException e) {<br />e.printStackTrace();<br />System.out.println("In catch...");<br />} finally {<br />System.out.println("In finally....");<br />}<br />System.out.println("after...");</p><p>return result;<br />}<br />}

輸出結果:

--- main() start --->
java.lang.NullPointerException
 at com.min.Test.method(Test.java:34)
 at com.min.Test.hello(Test.java:22)
 at com.min.Test.main(Test.java:13)
NullPointerException occured.
finally...
after hello
In finally....
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at com.min.Test.testSeq(Test.java:43)
 at com.min.Test.main(Test.java:15)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.