<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            JAVA中throws和throw有什么區別

            時間:2025-11-26 22:09:20 java語言

            JAVA中throws和throw有什么區別

              一直對java中的throws和throw不太理解。你知道JAVA中throws和throw有什么區別嗎?下面是小編為大家帶來的關于JAVA中throws和throw有什么區別的知識,歡迎閱讀。

              throw:(針對對象的做法)

              拋出一個異常,可以是系統定義的,也可以是自己定義的。下面舉兩個例子:

              拋出Java中的一個系統異常:

              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());

              }

              }

              }

              拋出一個自定義的異常:

              public class People {

              public static int check(String strage) throws MyException{

              int age = Integer.parseInt(strage);

              if(age < 0){

              throw new MyException("年齡不能為負數!");

              }

              return age;

              }

              public static void main(String[] args){

              try{

              int myage = check("-101");

              System.out.println(myage);

              }catch(NumberFormatException e){

              System.out.println("數據格式錯誤");

              System.out.println("原因:" + e.getMessage());

              }catch(MyException e){

              System.out.println("數據邏輯錯誤");

              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:(針對一個方法拋出的異常)

              拋出一個異常,可以是系統定義的,也可以是自己定義的。

              拋出java中的一個系統異常:

              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());

              }

              }

              }

              拋出一個自定義異常:

              public class People {

              public static int check(String strage) throws MyException{

              int age = Integer.parseInt(strage);

              if(age < 0){

              throw new MyException("年齡不能為負數!");

              }

              return age;

              }

              public static void main(String[] args){

              try{

              int myage = check("-101");

              System.out.println(myage);

              }catch(NumberFormatException e){

              System.out.println("數據格式錯誤");

              System.out.println("原因:" + e.getMessage());

              }catch(MyException e){

              System.out.println("數據邏輯錯誤");

              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,因為這樣在完成一個大型任務的時候可以讓別的程序員知道這里會出現什么異常。

              如果是自己定義的異常,則必須要用throws拋出該方法可能拋出的異常,否則編譯會報錯


            【JAVA中throws和throw有什么區別】相關文章:

            Java編程中throw和throws子句的使用方法02-25

            Java和Python有什么區別02-21

            java和C++有什么區別03-18

            java SE和EE有什么區別02-22

            java知識:JDK和JRE有什么區別11-20

            Java面試實例int和Integer有什么區別?10-25

            雅思和托福有什么區別12-20

            Unix和Linux有什么區別01-19

            Wifi和Wlan有什么區別?12-05

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看