1、throw是當前方法不處理這個異常,由它的上一級進行處理。並且拋出異常后將停止執行代碼。
package myProject; public class ExceptionTest { //測試throw public void testThrow() { try { int a=1/0; }catch(Exception e){ System.out.println("1"); throw new RuntimeException(); }finally { System.out.println("2"); } System.out.println("3"); } public static void main(String[] args) { ExceptionTest t=new ExceptionTest(); t.testThrow(); } }
輸出結果為:
1 Exception in thread "main" 2 java.lang.RuntimeException at myProject.ExceptionTest.testThrow(ExceptionTest.java:11) at myProject.ExceptionTest.main(ExceptionTest.java:20)
可見,沒有打印3,即throw拋出異常后,會執行finally塊的代碼,但不會再執行后邊的代碼。調用這種方法時,可以用try catch捕獲並處理這個異常,並用finally塊達到輸出3的目的,見如下代碼:
package myProject; public class ExceptionTest { //測試throw public void testThrow() { try { int a=1/0; }catch(Exception e){ System.out.println("1"); throw new RuntimeException(); }finally { System.out.println("2"); } System.out.println("3"); } public void test1() { try { testThrow(); }catch(Exception e) { e.printStackTrace(); }finally{
System.out.println("3");
} } public static void main(String[] args) { ExceptionTest t=new ExceptionTest(); t.test1(); } }
輸出結果為:
1
2
java.lang.RuntimeException
3
at myProject.ExceptionTest.testThrow(ExceptionTest.java:11)
at myProject.ExceptionTest.test1(ExceptionTest.java:19)
at myProject.ExceptionTest.main(ExceptionTest.java:28)
2、try catch 是直接處理異常,執行完finally塊后,接着執行代碼。
package myProject; public class ExceptionTest { //測試try catch public void testCatch() { try { int a=1/0; }catch(Exception e){ System.out.println("1"); }finally { System.out.println("2"); } System.out.println("3"); } public void test1() { try { testCatch(); }catch(Exception e) { System.out.println("4"); e.printStackTrace(); }finally { System.out.println("5"); } } public static void main(String[] args) { ExceptionTest t=new ExceptionTest(); t.test1(); } }
輸出結果如下:
1 2 3 5
可見,由於testCatch()已經用try catch處理了異常,那么在test1()方法中的catch塊將不會執行,也就不會輸出4
3、throws寫在方法參數的后邊,聲明了該方法有可能拋出異常。如果這個方法的確有可能會拋出一個異常,那么編輯器會強制你加上這個throws,見如下代碼
package myProject; public class ExceptionTest { //測試try catch public void testCatch() throws Exception{ int a=1/0; } public void test() { try { testCatch(); }catch(Exception e) { System.out.println("1"); }finally { System.out.println("2"); } } public static void main(String[] args) { ExceptionTest t=new ExceptionTest(); t.test(); } }
輸出結果如下:
1 2
可見,try catch可以捕獲有帶throws的方法的異常。
4、try catch 可以捕獲try catch捕獲的異常
見如下代碼:ExceptionTest2類
package myProject; public class ExceptionTest2 { public void trycatch() { try { int i=1/0; }catch(Exception e){ System.out.println("ExceptionTest2-----catch"); e.printStackTrace(); } } }
ExceptionTest類:
package myProject; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class ExceptionTest { private static final Logger logger=LogManager.getLogger(); private ExceptionTest2 t; public void test() { try { t.trycatch(); }catch(Exception e){ System.out.println("ExceptionTest----catch"); e.printStackTrace(); } } public static void main(String[] args) { ExceptionTest test= new ExceptionTest(); test.test(); } }
ExceptionTest類中的test()方法調用ExceptionTest2類trycatch()方法,所以test()會捕獲trycatch()捕獲的異常
輸出結果為:
ExceptionTest----catch java.lang.NullPointerException at myProject.ExceptionTest.test(ExceptionTest.java:11) at myProject.ExceptionTest.main(ExceptionTest.java:21)