- 相關(guān)推薦
JAVA中throws和throw有什么區(qū)別
一直對(duì)java中的throws和throw不太理解。你知道JAVA中throws和throw有什么區(qū)別嗎?下面是小編為大家?guī)淼年P(guān)于JAVA中throws和throw有什么區(qū)別的知識(shí),歡迎閱讀。
throw:(針對(duì)對(duì)象的做法)
拋出一個(gè)異常,可以是系統(tǒng)定義的,也可以是自己定義的。下面舉兩個(gè)例子:
拋出Java中的一個(gè)系統(tǒng)異常:
public class One {
public void yichang(){
NumberFormatException e = new NumberFormatException();
throw e;
}
public static void main(String[] args){
One test = new One();
try{
test.yichang();
}catch(NumberFormatException e){
System.out.println(e.getMessage());
}
}
}
拋出一個(gè)自定義的異常:
public class People {
public static int check(String strage) throws MyException{
int age = Integer.parseInt(strage);
if(age < 0){
throw new MyException("年齡不能為負(fù)數(shù)!");
}
return age;
}
public static void main(String[] args){
try{
int myage = check("-101");
System.out.println(myage);
}catch(NumberFormatException e){
System.out.println("數(shù)據(jù)格式錯(cuò)誤");
System.out.println("原因:" + e.getMessage());
}catch(MyException e){
System.out.println("數(shù)據(jù)邏輯錯(cuò)誤");
System.out.println("原因:" + e.getMessage());
}
}
}
public class MyException extends Exception{
private static final long serialVersionUID = 1L;
private String name;
public MyException(String name){
this.name = name;
}
public String getMessage(){
return this.name;
}
}
throws:(針對(duì)一個(gè)方法拋出的異常)
拋出一個(gè)異常,可以是系統(tǒng)定義的,也可以是自己定義的。
拋出java中的一個(gè)系統(tǒng)異常:
public class One {
public void yichang() throws NumberFormatException{
int a = Integer.parseInt("10L");
}
public static void main(String[] args){
One test = new One();
try{
test.yichang();
}catch(NumberFormatException e){
System.out.println(e.getMessage());
}
}
}
拋出一個(gè)自定義異常:
public class People {
public static int check(String strage) throws MyException{
int age = Integer.parseInt(strage);
if(age < 0){
throw new MyException("年齡不能為負(fù)數(shù)!");
}
return age;
}
public static void main(String[] args){
try{
int myage = check("-101");
System.out.println(myage);
}catch(NumberFormatException e){
System.out.println("數(shù)據(jù)格式錯(cuò)誤");
System.out.println("原因:" + e.getMessage());
}catch(MyException e){
System.out.println("數(shù)據(jù)邏輯錯(cuò)誤");
System.out.println("原因:" + e.getMessage());
}
}
}
public class MyException extends Exception{
private static final long serialVersionUID = 1L;
private String name;
public MyException(String name){
this.name = name;
}
public String getMessage(){
return this.name;
}
}
那么下面我要說究竟什么時(shí)候用哪種:
如果是系統(tǒng)異常的話可以什么都不用做,也可以針對(duì)方法拋出一個(gè)異常,因?yàn)橄到y(tǒng)異常是可以被系統(tǒng)自動(dòng)捕獲的,所以這個(gè)異常究竟是要在方法內(nèi)部解決還是交給上層函數(shù)去解決其實(shí)效果是一樣的。但是我查了很多資料,即使會(huì)拋出異常能被系統(tǒng)所捕獲的話還是建議針對(duì)方法寫一個(gè)throws,因?yàn)檫@樣在完成一個(gè)大型任務(wù)的時(shí)候可以讓別的程序員知道這里會(huì)出現(xiàn)什么異常。
如果是自己定義的異常,則必須要用throws拋出該方法可能拋出的異常,否則編譯會(huì)報(bào)錯(cuò)
【JAVA中throws和throw有什么區(qū)別】相關(guān)文章:
Java編程中throw和throws子句的使用方法08-26
Java和Python有什么區(qū)別09-06
java和C++有什么區(qū)別09-05
java SE和EE有什么區(qū)別08-01
java知識(shí):JDK和JRE有什么區(qū)別08-08
Java面試實(shí)例int和Integer有什么區(qū)別?06-09
WLAN和WIFI有什么區(qū)別06-26
警校和軍校有什么區(qū)別10-07
Unix和Linux有什么區(qū)別07-24