異常

Throwable 類

Throwable t = new Throwable();
System.out.println(t.toString());
//全限定名 全類名
——>java. lang. Throwable
Throwable t = new Throwable();
System.out.println(t.getMessage());
——> null
//getMessage()
Throwable t = new Throwable("懶癌");
System.out.println(t.getMessage());
——> 懶癌
//printStackTrace()
public class Test{
  public static void main(String[] args) {
    test();
  }
public static void test(){
  Throwable t = new Throwable ("懶癌");
  t.printStackTrace();
  }
}
java.lang.Throwable: cancer
    at com.TurtleGraph.Test.test(Test.java:16)
    at com.TurtleGraph.Test.main(Test.java:13)
image.png

已知直接子類:

  • Error: Error 是 Throwable 的子類,用于指示合理的應(yīng)用程序不應(yīng)該試圖捕獲的嚴(yán)重問題。大多數(shù)這樣的錯(cuò)誤都是異常條件。雖然 ThreadDeath 錯(cuò)誤是一個(gè)“正規(guī)”的條件,但它也是 Error 的子類,因?yàn)榇蠖鄶?shù)應(yīng)用程序都不應(yīng)該試圖捕獲它。
    Java虛擬機(jī)無法解決的嚴(yán)重問題。
    Error是jvm或硬件出現(xiàn)的錯(cuò)誤,一般不用編寫代碼取處理
    如:JVM系統(tǒng)內(nèi)部錯(cuò)誤、資源耗盡等嚴(yán)重情況。一般不編寫針對性的代碼進(jìn)行處理。
  • Exception: 其它因編程錯(cuò)誤或偶然的外在因素導(dǎo)致的一般性問題,可以使用針對性的代碼進(jìn)行處理。
    Exception 類及其子類是 Throwable 的一種形式,它指出了合理的應(yīng)用程序想要捕獲的條件。
    子類:
    運(yùn)行時(shí)異常,不用顯式處理
    檢查性異常,顯式處理,不是語法錯(cuò)誤
//檢查性異常
public static void main(String[] args){
  try{
      Class.froName("");
  }catch(Exception e){

  }
}

Exception是需要代碼去進(jìn)行處理
例如:
1 空指針訪問
2 試圖讀取不存在的文件
3 網(wǎng)絡(luò)連接中斷

public static void main(String[] args){
    test(2,0);  
  }
public static void test(int a, int b){
    //當(dāng)系統(tǒng)直行道a/b時(shí),發(fā)現(xiàn)b是0; 由于數(shù)學(xué)上要求出書不能為0,生成異常對象,調(diào)用異常對象的printStackTrace方法
    int c = a/b;
    System.out.println(c);
  }
——> ArithmeticException 數(shù)學(xué)異常

捕獲異常:

public static void main(String[] args){
    test(2,0);  
  }
public static void test(int a, int b){
    //當(dāng)系統(tǒng)直行道a/b時(shí),發(fā)現(xiàn)b是0; 由于數(shù)學(xué)上要求出書不能為0,生成異常對象
    //把可能出現(xiàn)異常的代碼放進(jìn)try里
    try{
      int c = a/b;
      
    }catch(Exception e){
      System.out.println("除數(shù)不能為0");
    }
  System.out.println("hello world");
  }
——> 除數(shù)不能為0
    hello world

捕獲異常需要注意的細(xì)節(jié):
1.try塊中的異常經(jīng)過處理,try塊后的代碼可以正常運(yùn)行
2.如果try塊里出現(xiàn)了異常代碼,try塊里異常代碼之后的代碼就不會(huì)執(zhí)行
3.一個(gè)try塊后面是可以跟多個(gè)catch塊,也就是一個(gè)try塊可以捕獲多種類型異常
4.一個(gè)try塊可以捕獲多種類型異常,但是異常的類型必須從小到大進(jìn)行捕獲,否則編譯報(bào)錯(cuò)

拋出處理

拋出處理要注意的細(xì)節(jié)

    public static void main(String[] args) {
        

    }
    public static void test(){
        //throw throws
        try {
            throw new Exception();
        }catch (Exception e){
            
        }
    }
}
    public static void main(String[] args) {
        try{
            test();
        }catch (Exception e){
            
        }

    }

    public static void test() throws Exception {
        //throw throws
        throw new Exception();
    }
}
    public static void main(String[] args) throws Exception{
            test();

    }

    public static void test() throws Exception {
        //throw throws
        throw new Exception();
    }
}

1.如果一個(gè)方法內(nèi)部拋出了異常,那么必須要在方法上聲明拋出
2.如果調(diào)用一個(gè)聲明拋出異常的方法,那調(diào)用者必須處理
3.如果一個(gè)方法內(nèi)部拋出一個(gè)異常對象,那么throw語句后面的代碼都不會(huì)執(zhí)行,該方法馬上停止
4.在一種情況下,一次只能拋出一種異常對象
誰調(diào)用誰處理,如果沒有處理就是檢查性異常

throw & throws

1.throw關(guān)鍵字是用于方法內(nèi)部,throws用于方法聲明
2.throw用于方法內(nèi)部拋出一個(gè)異常對象,throws用于方法聲明拋出異常類型
3.throw后只能跟一個(gè)異常對象,throws一次可以跟多個(gè)異常類型

自定義異常

步驟:
自定義一個(gè)類繼承Exception

class NoIpException extends Exception{

}
//模擬FQ,如果沒插網(wǎng)線,請插上網(wǎng)線
public class Test {
    public static void main(String[] args) {
        try{
            login(null);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("請插網(wǎng)線");
        }

    }
    public static void login(String ip) throws NoIpException{
        if (ip == null){
            throw  new NoIpException();
        }else {
            System.out.println("您的好友列表: ");
        }
    }
    

}
class NoIpException extends Exception{

}
import org.omg.PortableServer.ThreadPolicyOperations;

import java.util.Scanner;

/**
 * Created by Administrator on 2018/5/30.
 */
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int money = scanner.nextInt();
               try {
                   payment(money);
               }catch (Exception e){
                   e.printStackTrace();
                   System.out.println("霸王餐");
               }
    }
    public static void payment(int money) throws NoEnoughMoney{
        if (money < 20){
            throw new NoEnoughMoney();
        }else {
            System.out.println("paid");
        }
    }
}
class NoEnoughMoney extends Exception{

}

運(yùn)行時(shí)異常&編譯時(shí)異常

如果方法上聲明了一個(gè)運(yùn)行時(shí)異常,那么在方法上可以聲明也可以不聲明,調(diào)用者可以處理也可以不處理,如果一個(gè)方法內(nèi)部拋出了一個(gè)編譯時(shí)異常對象,那么方法上就必須要聲明,調(diào)用者也必須處理
RunTimeException及其子類都是運(yùn)行異常
RuntimeException 是那些可能在 Java 虛擬機(jī)正常運(yùn)行期間拋出的異常的超類。
可能在執(zhí)行方法期間拋出但未被捕獲的 RuntimeException 的任何子類都無需在 throws 子句中進(jìn)行聲明。

finally塊

資源文件使用完一定要解除占用,別的程序無法對該文件進(jìn)行操作,使用finally的前提一定要使用try塊
即使出現(xiàn)異常還是那個(gè)也會(huì)執(zhí)行finally塊
return, throw 之后finally依舊執(zhí)行

public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/0;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("執(zhí)行finally塊");
        }
    }
}
——> 執(zhí)行finally塊
public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/1;
            return;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("執(zhí)行finally塊");
        }
    }
}
——> 執(zhí)行finally塊

exit 之后finally不執(zhí)行
exit:終止當(dāng)前運(yùn)行的Java虛擬機(jī)

public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/1;
            System.exit(1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("執(zhí)行finally塊");
        }
    }
}
——> 執(zhí)行finally塊

組合:

try+finally:適合處理運(yùn)行時(shí)異常
try+catch:不用釋放資源
try+catch+finally:適合處理異常并且釋放資源 Ctrl+alt+t

try{
    ......  //可能產(chǎn)生異常的代碼
}
catch( ExceptionName1 e ){
    ......  //當(dāng)產(chǎn)生ExceptionName1型異常時(shí)的處置措施
}
catch( ExceptionName2 e ){
......  //當(dāng)產(chǎn)生ExceptionName2型異常時(shí)的處置措施
}  
[ finally{
......   //無論是否發(fā)生異常,都無條件執(zhí)行的語句
        }  ]
image.png

捕獲異常的相關(guān)信息

  • getMessage() 獲取異常信息,返回字符串。打印出錯(cuò)原因
  • printStackTrace() 獲取異常類名和異常信息,以及異常出現(xiàn)在程序中的位置。返回值void。打印函數(shù)調(diào)用棧
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容