11 异常

异常:
异常就是Java程序在运行过程中出现的错误。
异常的分类:
    通过API查看Throwable

  •  Error

        * 服务器宕机,数据库崩溃等

  •   Exception

异常的继承体系

  •  Throwable

        * Error    
        * Exception
            * RuntimeException

JVM默认是如何处理异常的
    * main函数收到这个问题时,有两种处理方式:
      * 自己将该问题处理,然后继续运行
      * 自己没有针对的处理方式,只有交给调用main的jvm来处理
    * jvm有一个默认的异常处理机制,就将该异常进行处理.
    * 并将该异常的名称,异常的信息.异常出现的位置打印在了控制台上,同时将程序停止运行

public class Demo1_Exception {

    public static void main(String[] args) {
    /*    int[] arr = {11,22,33};
        arr = null;
        System.out.println(arr[1]);*/

        try {
            int i = div(10,0);
            System.out.println(i);
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        }catch (Exception e) {
            System.out.println("出错了");
        }
        //jdk7多个异常的处理方式
        try {

        } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {

        }catch (Exception e) {

        }
    }

    private static int div(int i, int j) {
        return i/j;

    }

}



Java中的异常被分为两大类:编译时异常和运行时异常。
所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常
    * 编译时异常
        Java程序必须显示处理,否则程序就会发生错误,无法通过编译
    * 运行时异常
        无需显示处理,也可以和编译时异常一样处理


public class Demo2_Throwable {

    public static void main(String[] args) {
    /*    Throwable的几个常见方法
        getMessage()
            * 获取异常信息,返回字符串。
        toString()
            * 获取异常类名和异常信息,返回字符串。
        printStackTrace()  //jvm默认处理异常的方式
            * 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。

*/
        try {
            int num = 10/0;
        } catch (Exception e) {
//            System.out.println(e.getMessage()); //  / by zero
//            System.out.println(e.toString());//java.lang.ArithmeticException: /

by zero
        }

    }

}
public class Demo3_Exception {
    /*throws的方式处理异常
    定义功能方法时,需要把出现的问题暴露出来让调用者去处理。
    通过throws在方法上标识。*/
    public static void main(String[] args) throws Exception {
        Person person = new Person();
        person.setAge(-12);
        System.out.println(person.getAge());
    }

}
class Person{
    private int age;

    public Person() {
        super();
    }

    public Person(int age) {
        super();
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {  
        if (age<0 || age>150) {
            throw new RuntimeException("非法的年龄!"); //运行时异常
        }
        this.age = age;
    }

/*    public void setAge(int age) throws Exception{  //编译时异常
        if (age<0 || age>150) {
            throw new Exception("非法的年龄!");
        }
        this.age = age;
    }*/

}



throws和throw的区别
    throws
        用在方法声明后面,跟的是异常类名
        可以跟多个异常类名,用逗号隔开
        表示抛出异常,由该方法的调用者来处理
    throw
        用在方法体内,跟的是异常对象名
        只能抛出一个异常对象名

finally的特点
    * 被finally控制的语句体一定会执行
    * 特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))
finally的作用
    * 用于释放资源,在IO流操作和数据库操作中会见到



final,finally和finalize的区别:
    * final可以修饰类(不能被继承),修饰方法(不能被重写),修饰变量(只能赋值一次)
    * finally是try语句中的一个语句体,不能单独使用,用于释放资源
    * finalize,是一个方法,当垃圾回收器确定不存在对该对象的更多引用时,由垃圾回收器调
用此方法

public class test1 {

    public static void main(String[] args) {
        //如果catch里面有return语句,请问finally的代码还会执行吗?
        System.out.println(getNum());
    }
    public static int getNum() {
        int x = 10;
        try {

            int num = 10/0;
            return x;
        } catch (Exception e) {
            x = 20;
            return x;
        }finally {
            System.out.println("----");
            x = 30;
        }
    }
}
/*----
20
*/



自定义异常:
    * 继承自Exception

 class AgeOutOfBoundsException extends Exception{

            public AgeOutOfBoundsException() {
                super();
            }

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

        }
View Code

    * 继承自RuntimeException

异常注意事项
    子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。
    如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出

父类没有的异常
    如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常
发生,那么子类只能try,不能throws


如何使用异常处理
    原则:如果该功能内部可以将问题处理,用try,如果处理不了,交由调用者处理,这是用throws
    区别:
        后续程序需要继续运行就try
        后续程序不需要继续运行就throws
    如果JDK没有提供对应的异常,需要自定义异常。

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

public class Test2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");

        while(true) {
            String line = scanner.nextLine();
            try {
                int num = Integer.parseInt(line);
                System.out.println("二进制为:"+Integer.toBinaryString

(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("录入错误,录入的是非法字

符,请出现录入!");
                    }
                }
            }
        }

    }

}
原文地址:https://www.cnblogs.com/fly-book/p/9864880.html