异常机制

异常:程序在运行时出现的不正常情况。
异常的由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。

问题的划分:

  • 严重问题:通过Error类描述。
    对于Error不编写针对性的代码进行处理。
  • 非严重问题:通过Exception类进行描述。
    编写针对性的代码的处理方式进行处理。
  • 无论Error或Exception都具有一些共性内容,比如不正常信息,引发原因等。
  • java.lang.Throwable :所有错误和异常的超类。
    直接字类:Error、Exception。

异常的处理:java提供了特有的语句进行处理。

try { 需要被检测的代码块 }
catch( 异常类 变量 ) { 处理异常代码块;(处理方式)}
finally{ 一定会执行的语句 }
    class Demo{
    int div(int a, int b) {
        return a/b;    //new AritchmeticException();虚拟机常见一个对象
    }
}
class ExceptionDemo{
    public static void main(String[] args){
        Demo d = new Demo();
        try{
            int x =d.div(4,0);    //new AritchmeticException()
            System.out.println("x = "+x);
        } 
        catch(Exception e){        //Exception e = new ArithmeticException(); 接受异常对象
            System.out.println("除零啦!!");
            System.out.println(e.getMessage()); // by zero; 获取异常信息
            System.out.println(e.toString());    //异常名称; 获取异常名称

            e.printStackTrace();    //异常名称,异常信息,异常出现的位置。
        }                            //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。
                                    //打印异常的堆栈的跟踪信息。
        System.out.println("over !!");
    }
}
  • throws : 函数抛出异常
    调用者不处理异常:
class Demo{
    int div(int a, int b) throws Exception{        //在功能上通过throws的关键字声明了该功能可能会出问题。
        return a/b;    //new AritchmeticException();虚拟机常见一个对象
    }
}

class ExceptionDemo{
    public static void main(String[] args) throws Exception{    //Demo抛出异常,主函数调用了它,就需要处理这个异常,
        Demo d = new Demo();                                    //如果不进行捕获的话,也可以抛给虚拟机,虚拟机会用默认处理方式。
        int x =d.div(4,0);    //new AritchmeticException()
        System.out.println("x = "+x);
        
        System.out.println("over !!");
    }
}
  • 调用者处理异常
    * class Demo{
    int div(int a, int b) throws Exception{        //在功能上通过throws的关键字声明了该功能可能会出问题。
        return a/b;    //new AritchmeticException();虚拟机常见一个对象
    }
}

class ExceptionDemo{
    public static void main(String[] args) {    
        Demo d = new Demo();                                    
        try{
            int x =d.div(4,0);    //new AritchmeticException()
            System.out.println("x = "+x);
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
        
        System.out.println("over !!");
    }
}
  • 对多异常的处理:
    • 声明异常时,建议声明更为具体的异常。这样处理更具体。
    • 对方声明多少个异常,就要对应几个catch块。不要定义多余的catch块。
    • 如果多个catch块出现继承关系,父类异常catch块放在最下面。
class Demo{
   int div(int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{ //在功能上通过throws的关键字声明了该功能可能会出问题。
       return a/b;    //new AritchmeticException();虚拟机常见一个对象                    //抛出多个异常,用逗号隔开!
   }
}
class ExceptionDemo{
   public static void main(String[] args) {    
       Demo d = new Demo();                                    
       try{
           int x =d.div(4,0);    //new AritchmeticException()
           System.out.println("x = "+x);
       }
       catch(ArithmeticException e){    //函数中抛出什么异常,catch就要处理什么异常!
           System.out.println(e.toString());
           System.out.println("除零了 !!!");
       }
       catch(ArrayIndexOutOfBoundsException e){
           System.out.println(e.toString());
           System.out.println("角标越界了!!");
       }
       catch(Exception e){
           System.out.println(e.toString());
       }
       
       System.out.println("over !!");
   }
}
  • 建立catch处理时,catch中一定要定义具体的处理方式。不要简单定义一句e.printStackTrace( ),也不要简单书写输出语句。用户看不懂,也处理不了!!
  • 自定义异常:我们根据自己的特有问题进行描述封装成对象。这些问题java都没有对其描述并进行封装。
    • 当函数内部出现了throw抛出异常,那就必须出对应的处理动作。
    • 函数内throw抛出自定义异常,函数上要throws声明异常。
    • 发现打印的名称只有异常的名称,没有异常的信息;因为还没有定义异常的信息!
    • 如何定义异常信息?
      因为父类已经把异常信息的操作都处理好了,所以字类只需要在构造时,将异常信息通过super语句传递给父类,再通过getMessage( )方法获取自定义的异常信息。
    • 自定义异常:
      • 自定义类必须继承Exception(Throwable,Error...)。原因:异常体系有个特点:因为异常类和异常对象都被抛出;他们都具有可抛性;这个可抛性时throwable这个体系中独有的特点。
      • 只有这个体系中的类和对象才可以被throw和throws操作。
    • 代码如下:
    * class FuShuException extends Exception{
    FuShuException(String mess){
        super(mess);
    }
}
class Demo{
    int div(int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException,FuShuException{ 
        if(b<0)                                                                    //在功能上通过throws的关键字声明了该功能可能会出问题。    
            throw  new FuShuException("一个负数");  //手动通过throw关键字抛出异常。            //抛出多个异常,用逗号隔开!
        return a/b;    //new AritchmeticException();虚拟机常见一个对象                    
    }
}
class ExceptionDemo{
    public static void main(String[] args) {    
        Demo d = new Demo();                                    
        try{
            int x =d.div(4,-1);    //new AritchmeticException()
            System.out.println("x = "+x);
        }
        catch(ArithmeticException e){    //函数中抛出什么异常,catch就要处理什么异常!
            System.out.println(e.toString());
            System.out.println("除零了 !!!");
        }
        catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e.toString());
            System.out.println("角标越界了!!");
        }
        catch(FuShuException e){
            System.out.println(e.toString());
            System.out.println("亲,有负数!!!!");
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
        System.out.println("over !!");
    }
}

throw和throws的区别:

  • throws使用在函数上。
  • throw使用在函数内。
  • throws后面跟异常类,可以跟多个,中间可以用逗号隔开。
  • throw后面跟的是一场对象。

试试手动抛出ArithmeticException

  • RuntimeException的字类异常类,在函数内抛出,函数上无需声明!(RuntimeEexception的特殊之处)
  • 如果函数上声明了该异常,调用者可以不用进行处理,编译一样能通过。(原因:RuntimeException的子类异常是不需要调用者处理的异常,它会停止程序,强迫程序员修改代码!)

自定义异常: 如果该异常的发生,无法再继续进行运算,就让自定义异常继承RuntimeException。

  • Object类下面有一个wait( long timeout)
    IllegalArgumentException 非法参数异常(参数为timeout时,超时异常)

总结:对于异常分两种:

  • 编译时被检测异常。
  • 编译时不被检测异常。(运行异常,RuntimeException及其字类异常)。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容