【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);
    }
    
}
相关文章
相关标签/搜索