throws和throw:
throws:用來聲明一個方法可能產生的所有異常,不做任何處理而是將異常往上傳,誰調用我我就拋給誰。
- 用在方法聲明后面,跟的是異常類名
- 可以跟多個異常類名,用逗號隔開
- 表示拋出異常,由該方法的調用者來處理
- throws表示出現異常的一種可能性,并不一定會發生這些異常
throw:則是用來拋出一個具體的異常類型。
- 用在方法體內,跟的是異常對象名
- 只能拋出一個異常對象名
- 表示拋出異常,由方法體內的語句處理
- throw則是拋出了異常,執行throw則一定拋出了某種異常
分別介紹:
throws在方法后邊聲明異常,其實就是自己不想對異常做出任何的處理,告訴別人自己可能出現的異常,交給別人處理;
注意:方法名后面跟上 throws Exception 證明這個方法里的語句可能會發生異常,注意是可能!在別處如果調用這個方法時,就必須也拋出異常或者用try catch 處理。 throws是可以單獨使用的。
eg:(代碼示例01)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Test { public static void main(String[] args) throws Exception { Test test = new Test(); /*** 調用的方法里拋出了異常,依然要調用的2種方式 * 1、繼續聲明異常(此代碼塊兒為本方式) * 2、用try catch 代碼塊包住 test.compute() */ test.compute(); } public void compute() throws Exception{ System.out.println( "我可能發生異常" ); System.out.println( "3/0的值為" + 3 / 0 ); } } |
eg:(代碼示例02)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Test { public static void main(String[] args){ Test test = new Test(); /*** 調用的方法里拋出了異常,依然要調用的2種方式 * 1、拋出異常 * 2、用try catch 代碼塊包住 test.compute()進行捕獲并解決異常(此代碼塊兒為此方式) */ try { test.compute(); } catch (Exception e) { e.printStackTrace(); System.err.println( "除數不能為0" ); } } public void compute() throws Exception{ System.out.println( "我可能發生異常" ); System.out.println( "3/0的值為" + 3 / 0 ); } } |
throw:就是自己處理一個異常,有兩種方式要么是自己捕獲異常try...catch代碼塊,要么是拋出一個異常(throws 異常)
eg(代碼示例01):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package Exception005.usuallyWrong.usuallyWrong01; import java.util.Scanner; /** * 方式1:方法后未加throws Exception,在代碼塊兒中使用try-catch進行捕獲異常,在if選擇結構中加入throw,實現了手動異常,方式2:調用方法時繼續聲明該異常 */ public class ByoneselfThrow { String name; String sex; int age; public void byoneself(){ Scanner input= new Scanner(System.in); System.out.println( "請輸入你的姓名:" ); name=input.next(); System.out.println( "請輸入你的年齡:" ); age=input.nextInt(); System.out.println( "請輸入你的性別:" ); sex=input.next(); try { if ( "男" .equals(sex)|| "女" .equals(sex)){ System.out.println( "我的名字叫" +name+ ",年齡為" +age+ ",性別為" +sex); } else { throw new Exception( "性別只能是男/女!" ); } } catch (Exception e){ e.printStackTrace(); } } } class Test{ public static void main(String[] args) { ByoneselfThrow center= new ByoneselfThrow(); center.byoneself(); } } |
eg(代碼示例02):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package Exception005.usuallyWrong.usuallyWrong01; import java.util.Scanner; /** * 方式1:方法后加throws Exception(聲明異常),在if選擇結構中加入throw(手動拋出異常),在調用方法時使用try-catch進行捕獲并解決異常,實現了手動異常 * 方式2:調用方法時繼續聲明該異常 */ public class ByoneselfThrow { String name; String sex; int age; public void byoneself() throws Exception{ Scanner input= new Scanner(System.in); System.out.println( "請輸入你的姓名:" ); name=input.next(); System.out.println( "請輸入你的年齡:" ); age=input.nextInt(); System.out.println( "請輸入你的性別:" ); sex=input.next(); if ( "男" .equals(sex)|| "女" .equals(sex)){ System.out.println( "我的名字叫" +name+ ",年齡為" +age+ ",性別為" +sex); } else { throw new Exception( "性別只能是男/女!" ); } } } class Test{ public static void main(String[] args) { ByoneselfThrow center= new ByoneselfThrow(); try { center.byoneself(); } catch (Exception e) { e.printStackTrace(); } } } |
eg(代碼示例03):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.xinkaipu.Exception; public class TestThrow { public static void main(String[] args) { try { //調用帶throws聲明的方法,必須顯式捕獲該異常 //否則,必須在main方法中再次聲明拋出 throwChecked(- 3 ); } catch (Exception e) { System.out.println(e.getMessage()); } //調用拋出Runtime異常的方法既可以顯式捕獲該異常, //也可不理會該異常 throwRuntime( 3 ); } public static void throwChecked( int a) throws Exception { if (a > 0 ) { //自行拋出Exception異常 //該代碼必須處于try塊里,或處于帶throws聲明的方法中 throw new Exception( "a的值大于0,不符合要求" ); } } public static void throwRuntime( int a) { if (a > 0 ) { //自行拋出RuntimeException異常,既可以顯式捕獲該異常 //也可完全不理會該異常,把該異常交給該方法調用者處理 throw new RuntimeException( "a的值大于0,不符合要求" ); } } } |
總結:
throws可以單獨使用,throw不可以,必須搭配try catch,或者throws,若程序執行到throw exception 語句,則后面的語句不會再執行。
以上就是如何區分JAVA中的throws和throw的詳細內容,更多關于JAVA中的throws和throw的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/liangbaolong/p/12883613.html