JAVA——运行时异常(RuntimeException)

Exception中有一个特殊的子类异常RuntimeException运行时异常。
如果在函数内抛出该异常,函数上可以不用声明,编译一样通过。
如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过。

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

对于异常分两种:
1、编译时被监测的异常
2、编译时不被监测的异常(运行时异常,RuntimeException以及其子类)

class FuShuException extends RuntimeException
{
    FuShuException(String msg)
    {
        super(msg);
    }
}


class Demo
{
    int div(int a,int b)//函数上没有抛出异常,因为FuShuException是RuntimeException的子类
    {
        if(b<0)
           throw new FuShuException("除数为负数");
        if(b==0)
           throw new ArithmeticException("被0除了");
    return a/b;
    }

}

class ExceptionDemo7
{
    public static void main(String[] args)
    {
        Demo d = new Demo();
        int x = d.div(4,-4);
        System.out.println("x="+x);
    }
}

输出结果:

Exception in thread "main" FuShuException: 除数为负数
    at Demo.div(ExceptionDemo00.java:15)
    at ExceptionDemo00.main(ExceptionDemo00.java:28)

举例:
问题是:
电脑冒烟
电脑蓝屏

要对问题进行描述,封装成对象。
可是当冒烟发生后,出现讲课进度无法继续。
出现了老师的问题:课时计划无法完成

class LanPingException extends Exception
{
    LanPingException(String message)
    {
        super(message);
    }
}

class MaoYanException extends Exception
{
    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;
    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());
    }

    System.out.println("讲课了!");
    }
    public void test()
    {
    System.out.println("LianXi");
    }
}

class ExceptionTest
{
    public static void main(String[] args)
    {
        Teacher t  = new Teacher("Wang");
    try
    {
        t.prelect();    
    }
    catch(NoPlanException e)
    {
        System.out.println(e.toString());
       System.out.println("换老师或者放假");
    }
    }
}

运行结果:
LianXi
NoPlanException: 课时无法继续冒烟了
换老师或者放假

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

//定义一个异常,让它继承运行时异常
class NoValueException extends RuntimeException
{
    NoValueException(String msg)
    {
        super(msg);
    }
}
//定义一个接口
interface  Shape
{
    void getArea();
}
//长方形实现Shape接口
class Rectangle implements Shape
{
    private int len,wid;
    Rectangle(int len,int wid)//这里不用throws NoValueException,因为NoValueException是RuntimeException的子类
    {
        if(len<=0||wid<=0)
            throw new NoValueException("出现非法值");

            this.len = len;
            this.wid = wid;
    }
    public void getArea()
    {
        System.out.println(len*wid);
    }
}
class Circle implements Shape
{
    private double  radius;
    public static final double PI = 3.14;
    Circle(double radius)
    {
        if(radius<=0)
            throw new NoValueException("非法");
        this.radius = radius;
    }
    public void getArea()
    {
        System.out.println(PI*radius*radius);
    }

}
class Demo2
{
    public static void main(String[] args)
    {
        Rectangle r = new Rectangle(3,4);
        r.getArea();

        Circle circle = new Circle(-8);
        circle.getArea();
        System.out.println("over");
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值