【203天】黑馬程序員27天視頻學習筆記【Day19-中】

叨逼叨兩句

如今真是個好時代啊。
看的懂代碼、理解人性絕對是巨大的優點java

19-06:Throwable的幾個常見方法

不經常使用面試

  1. getMessage()數據庫

  2. toString()jvm

  3. printStackTrace()函數

package com.test.demo001;


public class Demo31 {
        public static void main(String[] args) {
            int a = 10;
            int b = 0;
            try{
                System.out.println(a/b);
            } catch(Exception e){ //這裏至關於Exception e = new ArithmeticException("/by zero");
                System.out.println(e.getMessage());//打印異常信息,返回值String
                System.out.println(e.toString());//打印異常信息與類名,返回值String
                e.printStackTrace();//打印異常信息、類名與異常位置,JVM默認使用的就是這個,返回值void
            }

        }
}

19-07:throws方式進行異常處理

  1. RuntimeException拋出到主方法能夠不用寫throwsthis

package com.test.demo001;


public class Demo31 {
        public static void main(String[] args) {
            Student s = new Student();
            s.setAge(-1);
            System.out.println(s.getAge());
        }
}
public void setAge(int age) {
        if(age > 0 && age < 150){
            this.age = age;
        } else {
            throw new RuntimeException("年齡非法");
        }
    }
  1. 其它異常須要拋出來code

package com.test.demo001;


public class Demo31  {
        public static void main(String[] args) throws Exception {
            Student s = new Student();
            s.setAge(-1);
            System.out.println(s.getAge());
        }
}
public void setAge(int age) throws Exception {
        if(age > 0 && age < 150){
            this.age = age;
        } else {
            throw new Exception("年齡非法");
        }
    }

19-08:throw的概述以及和throws的區別

throw

  1. 後面可跟多個異常,用逗號隔開視頻

  2. 後面跟的是類名對象

  3. 用在方法外繼承

throws

  1. 後面只能跟一個異常

  2. 後面跟的是對象

  3. 用在方法內

public void setAge(int age) throws Exception,RuntimeException {
        if(age > 0 && age < 150){
            this.age = age;
        } else {
            throw new Exception("年齡非法");
            //上面這條語句至關於 
            //Exception e = new Exception("年齡非法");
            //throw e
        }
    }

19-09:finally關鍵字的特色及做用

finally的特色

  1. 被finally控制的語句必定會執行

  2. 特殊狀況:在執行到finally以前jvm退出(好比System.exit(0))

    package com.test.demo001;
    
    
    public class Demo31  {
            public static void main(String[] args) throws Exception {
                int a = 10;
                int b = 0;
                try{
                    System.out.println(a/b);                
                } catch(Exception e){
                    System.out.println("catch執行");
                    System.exit(0);
                    return;
                } finally {
                    System.out.println("finally執行");
                }
                
            }
    }

finally的做用

用於釋放資源,在IO流操做和數據庫操做中會見到

finally遇到return

  1. return至關於方法的最後一口氣,方法掛以前,會看看有沒有finally幫其完成遺願,有的話就完成finally後再完全返回。

package com.test.demo001;


public class Demo31  {
        public static void main(String[] args) throws Exception {
            int a = 10;
            int b = 0;
            try{
                System.out.println(a/b);    
            } catch(Exception e){
                System.out.println("catch執行");
                return;
            } finally {
                System.out.println("finally執行");
            }
            
        }
}

19-10:finally關鍵字面試題

  1. final,finally和finalize的區別

    • final

      • 修飾類,不能被繼承

      • 修飾方法,不能被重寫

      • 修飾變量,只能賦值一次

    • finally是try語句中的一個語句體,不能單獨使用,用來釋放資源

    • finallize是一個方法,當垃圾回收器肯定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。

  2. finally裏不要寫return,那樣會讓try和catch裏的return都再也不執行了。【一個方法執行完一個return就結束了】

package com.test.demo001;


public class Demo31  {
    public static void main(String[] args) throws Exception {
            Demo31 d =  new Demo31();
            int x = d.method();
            System.out.println(x);
        }
        
    public int method(){
        int a = 10;
        try{
            a = 20;
            System.out.println(1/0);
            return a;
        } catch(Exception e){
            a = 30;
            return a;
        } finally {
            a = 40;
            return a;
        }
    }    
        
}
  1. 如下的代碼結果是30,finally裏的語句雖然能夠運行,但他已經影響不了在catch中等待返回的a了【不記得了就看視頻19-10的講解】

package com.test.demo001;


public class Demo31  {
    public static void main(String[] args) throws Exception {
            Demo31 d =  new Demo31();
            int x = d.method();
            System.out.println(x);
        }
        
    public int method(){
        int a = 10;
        try{
            a = 20;
            System.out.println(1/0);
            return a;
        } catch(Exception e){
            a = 30;
            return a;
        } finally {
            a = 40;
        }
    }    
        
}

19-11:自定義異常

三步曲

  1. 要定義一個名字像異常的類

  2. 要繼承某個異常類

    1. 繼承Exception:編譯器須要處理異常.

    2. 繼承RuntimeException:運行時處理異常.

  3. 編寫構造函數

package com.test.demo001;

public class Demo31  {
    public static void main(String[] args){
        Student s = new Student();
            s.setAge(-10);
    }
}

class AgeOutOfException extends RuntimeException{

    public AgeOutOfException() {
        super();
    }

    public AgeOutOfException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public AgeOutOfException(String message, Throwable cause) {
        super(message, cause);
    }

    public AgeOutOfException(String message) {
        super(message);
    }

    public AgeOutOfException(Throwable cause) {
        super(cause);
    }
    
}
public void setAge(int age) throws AgeOutOfException {
        if(age > 0 && age < 150){
            this.age = age;
        } else {
            throw new AgeOutOfException("年齡非法");
            //上面這條語句至關於 
            //Exception e = new Exception("年齡非法");
            //throw e
        }
    }

19-12:異常的注意事項及如何使用異常處理

  1. 異常注意事項

    1. 子類重寫父類方法,子類方法中拋出的異常必須與父類相同,或者是父類異常的子類(異常界,父王最壞)

    2. 若是父類拋出多個異常,子類重寫父類時,只能拋出相同的異常或者是他的子類,子類不能拋出父類沒有的異常。

    3. 若是被重寫的方法沒有異常拋出,那麼子類的方法絕對不能夠拋出異常,若是此時子類中有異常發生,那麼子類只能try,不能throws。

  2. 如何使用異常處理

    1. 原則:若是該功能內部能解決,用try;處理不了,用throws。

    2. 區別:後續代碼還要繼續執行,用try;後續代碼不執行了,用throws【將終止程序運行】。

    3. 若是JDK沒有提供對應的異常,須要自定義異常

19-13:練習

package com.test.demo001;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Demo009 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("Please");
            String line = sc.nextLine();
            try{
                int num = Integer.parseInt(line);
                System.out.println(num);
                break;
            } catch(Exception e){
                try {
                    new BigInteger(line);
                    System.out.println("您輸入的整數太大了");
                } catch (Exception e1) {
                    try{
                        new BigDecimal(line);
                        System.out.println("您不能輸入小數");
                    } catch(Exception e2){
                        System.out.println("非法字符");
                    }
                }
            }
        }
    }
}

異常我的總結:組成異常的三部分

  1. 一個異常類【關鍵是類名+構造函數】

  2. 一個拋出異常的方法【明確何種狀況拋異常】

  3. 一個調用2中方法的主函數【明確對該方法究竟是try處理仍是throws給別人處理】

package com.test.demo001;

import java.util.Scanner;

public class Demo31  {
    public static void main(String[] args) throws AgeOutOfException{
            demo02();
    }
    
    private static void demo01() throws AgeOutOfException {
        throw new AgeOutOfException("YC");
    }

    private static void demo02() throws AgeOutOfException {
            demo01();
    }
}

class AgeOutOfException extends Exception{

    public AgeOutOfException() {
        super();
    }

    public AgeOutOfException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public AgeOutOfException(String message, Throwable cause) {
        super(message, cause);
    }

    public AgeOutOfException(String message) {
        super(message);
    } 

    public AgeOutOfException(Throwable cause) {
        super(cause);
    }
    
}
相關文章
相關標籤/搜索