异常

异常

异常:就是程序在运行时出现不正常情况。

对于问题的划分分为两种:

  • 对于严重的,java通过Error类进行描述。对于Error一般不编写针对性的代码对其进行处理。
     
  • 对与非严重的,java通过Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。

java.lang.Throwable

直接已知子类:

Error, Exception

异常的处理:


try{

     需要被检测的代码;

}catch(异常类  变量){

     处理异常的代码;(处理方式)

}finally{

     一定会执行的语句;

}

对捕获到的异常对象进行常见方法:

String getMessage()
         返回此 throwable 的详细消息字符串。
String toString()
         返回此 throwable 的简短描述。
void printStackTrace(PrintStream s) 
         将此 throwable 及其追踪输出到指定的输出流。

在函数上声明异常。

便于提高安全性,让调用者进行处理。不处理编译失败。

public class ExceptionDemo {
    public static void main(String[] args) {
        Demo demo = new Demo();

        try {

            int res = demo.div(2,0);
            System.out.println(res);

        } catch (Exception e) {  // Exception e = new ArithmeticException();  //多态

            System.out.println("除0啦");
            System.out.println(e.getMessage());  //  / by zero
            System.out.println(e.toString());   // java.lang.ArithmeticException: / by zero
            e.printStackTrace(); //异常名称,异常信息,异常出现的位置。
                                //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。
                                //打印异常的堆栈的跟踪信息。
        }
    }
}


class Demo{
    int div(int a, int b) throws ArithmeticException { // 在功能上通过throws 声明该功能可能出现的问题
        return a/b;
    }
}


对多异常的处理。

  • 声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
  • 对方声明几个异常,就对应有几个catch块。如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。

建立在进行catch处理时,catch中一定要定义具体处理方式。
不要简单定义一句 e.printStackTrace(),也不要简单的就书写一条输出语句。

public class ExceptionDemo3 {
    public static void main(String[] args) {
        Demo demo= new Demo();

        try {

            int res= demo.div(3,6);
            System.out.println(res);

        }catch (ArrayIndexOutOfBoundsException e){

            System.out.println("角标越界!");
            System.out.println(e.toString());

        }catch (ArithmeticException e){

            System.out.println("除0 了!");
            System.out.println(e.toString());

        }catch (Exception e){

            System.out.println(e.toString());
        }

    }
}

class Demo{

    //在功能上通过throws的关键字声明了该功能有可能会出现问题。
    int div(int a, int b) throws ArrayIndexOutOfBoundsException,ArithmeticException{
        // 可能出现角标越界异常,除0异常
        int[] arr = new int[a];
        System.out.println(arr[4]);
        return a/b;
    }
}


自定义异常

      因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。所以对于这些特有的问题可以按照java的对问题封装的思想。将特有的问题。进行自定义的异常封装。

自定义异常:
必须是自定义类继承Exception。

如何定义异常信息呢?
因为父类中已经把异常信息的操作都完成了。
所以子类只要在构造时,将异常信息传递给父类通过super语句。
那么就可以直接通过getMessage方法获取自定义的异常信息。

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。

  • 要么在内部try catch处理。
  • 要么在函数上声明让调用者处理。

一般情况在,函数内出现异常,函数上需要声明。

 

需求:在本程序中,对于除数是-1,也视为是错误的是无法进行运算的。
那么就需要对这个问题进行自定义的描述。

 

// 自定义异常类
class FuShuException extends Exception{
    private int value;

    FuShuException(){
        super();
    }
    FuShuException(String msg, int value){
        super(msg);
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

//当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。
//要么在内部try catch处理。
//要么在函数上声明让调用者处理。
//
//一般情况在,函数内出现异常,函数上需要声明。

class Demo{
    int  div(int a, int b) throws FuShuException {
        if(b<0){
            throw new FuShuException("出现了除数是负数的情况",b);  //手动通过throw关键字抛出一个自定义异常对象。

        }

        return a/b;
    }
}

public class ExceptionDemo3{
    public static void main(String[] args){
        Demo demo = new Demo();
        try {
            int res = demo.div(5, -1);
            System.out.println(res);
        } catch (FuShuException e) {
            System.out.println(e.toString());   // 异常.FuShuException: 出现了除数是负数的情况
            System.out.println(e.getMessage()+"\\"+e.getValue()); //出现了除数是负数的情况\-1
        }
    }
}


throws和throw的区别

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

RuntimeException

      Exceptoin中有一个特殊的子类异常RuntimeException 运行时异常。


如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。

如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;

之所以不用在函数声明,是因为不需要让调用者处理。当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正

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

 

对于异常分两种:

  • 1,编译时被检测的异常。
  • 2,编译时不被检测的异常(运行时异常。RuntimeException以及其子类)
public class ExceptionDemo4 {
    public static void main(String[] args) {
        Demo demo = new Demo();
        int res = demo.div(3,-1);
        System.out.println(res);
    }
}

// 异常类  继承 RuntimeException 
class FuShuException extends  RuntimeException{
    FuShuException(String msg){
        super(msg);
    }
}

class Demo{

    int div(int a, int b){
        if(b<0){
            throw new FuShuException("出现了除数为负数!");
        }
        return a/b;
    }
}

练习

毕老师用电脑上课。

开始思考上课中出现的问题。


比如问题是
    电脑蓝屏。
    电脑冒烟。

要对问题进行描述,封装成对象。

当发生冒烟时,课程无法继续,

当课程无法继续时,换老师。

// 异常类
class LanPingException extends Exception{
    LanPingException(String msg){
        super(msg);
    }
}

class MaoYanException extends Exception{
    public MaoYanException(String message) {
        super(message);
    }
}


class NoPlanException extends Exception {
    NoPlanException(String msg) {
        super(msg);
    }
}

class Computer{
    private int state = 3;

    public void run() throws LanPingException,MaoYanException{

        if(state == 2)
            throw new LanPingException("蓝屏了!");
        if(state == 3)
            throw  new MaoYanException("冒烟了!");

        System.out.println("电脑运行!");
    }

    public void reset(){

        state = 1;
        System.out.println("电脑重启!");
    }
}

class Teacher{
    private String name;
    private Computer cmpt;

    Teacher(String name){
        this.name = name;
        this.cmpt = new Computer();
    }

    public void prelect() throws  NoPlanException{
        try {
            cmpt.run();
        } catch (LanPingException e) {
            // 蓝屏 将电脑重启
            cmpt.reset();
        } catch (MaoYanException e) {
            // 冒烟
            test();
            throw new NoPlanException("课时无法继续!"+e.getMessage());
        }
    }

    public void test(){
        System.out.println("练习!");
    }
}

public class ExceptionTest {
    public static void main(String[] args) {
        Teacher t = new Teacher("毕老师");

        try {
            t.prelect();
        } catch (NoPlanException e) {
            System.out.println(e.toString());
            System.out.println("换老师");
        }
    }
}

异常的三种常用格式:

第一个格式:
try{


}
catch (){

}

第二个格式:
try{
    
}
catch (){

}
finally{

}

第三个格式:
try{
    
}
finally{
}
//记住一点:catch是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常。那么必须声明。
 

异常在子父类覆盖中的体现:

  • 1,子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
  • 2,如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
  • 3,如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。 如果子类方法发生了异常。就必须要进行try处理。绝对不能抛。

有一个圆形和长方形。
都可以获取面积。对于面积如果出现非法的数值,视为是获取面积出现问题。
问题通过异常来表示。
现有对这个程序进行基本设计。

public class ExceptionTest2 {
    public static void main(String[] args) {
        Rect r = new Rect(10,-2);
        r.getArea();

    }
}


// 自定义异常类   (编译不报错)
class NoValueException extends RuntimeException{
    NoValueException(String message){
        super(message);
    }
}

// 定义接口
interface Shape{
    public abstract void getArea();
}



class  Rect implements Shape{

    private int width;
    private int height;

    Rect(int width,int height) throws  NoValueException{
        if(width < 0 || height < 0)
            throw new NoValueException("长或宽不能为负数!");
        this.width = width;
        this.height = height;
    }

    public void getArea (){
        System.out.println(width*height);
    }
}

/*
Exception in thread "main" 异常.NoValueException: 长或宽不能为负数!
	at 异常.Rect.<init>(ExceptionTest2.java:39)
	at 异常.ExceptionTest2.main(ExceptionTest2.java:7)
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值