Java day_12 异常

异常

异常就是程序运行的时候出现的不正常的情况.
异常的由来:Java是纯面向对象语言,异常就是Java用面向对象的思想将不正常的情况进行了封装,所有异常都继承自Runnable接口.
继承Exception是编译时异常
继承RuntimeException运行时异常

Error:非常严重的错误,相当于不治之症.一般很少见,也难通过程序解决,他可能源于程序bug,但一般更可能源于环境问题,如内存消耗,错误在程序中无需处理,而由运行环境处理.编译时异常(非运行时异常): 程序正确,但因为外在的环境条件不满足引发.
Exception:不是很严重的错误,相当于平时的感冒,可以解决.
运行期异常(RuntimeException):意味着程序存在bug,如数组越界,0被除,空指针异常,需要修改程序来避免.

异常处理

try{
需要检测的代码块(可能会抛出的异常,也可能不会抛出异常)
}catch(异常的类型 异常类型的变量){
捕获异常后处理异常
}finally{
一定会被执行的代码(不管异常抛不抛出都会执行,例如数据库释放连接)
}
多重捕获块(多个catch)
try {
需要检测的代码(可能会抛出异常,也可能不会抛出异常)
} catch (异常的类型1 异常类型的变量1) {
捕获异常后处理异常
} catch (异常的类型2 异常类型的变量2) {
捕获异常后处理异常
} catch (异常的类型3 异常类型的变量3) {
捕获异常后处理异常
} finally {
一定会被执行的代码(不管异常抛不抛出都会执行,例如数据库释放连接)
}
Throw与Throws
throw:手动抛出异常
throws:继续向上抛出异常

Error案例
反复调用fun()函数,System.out.println(“ErrorDemo.fun() end”);无法得到执行.

package Day.day_12;

public class Demo01Error {
    public static void main(String[] args) {
        fun();
    }

    private static void fun() {
        System.out.println("ErrorDemo.fun() start");
        fun();
        System.out.println("ErrorDemo.fun() end");
    }
}

TryCatch案例

package Day.day_12;

public class Demo02trycatch {
    public static void main(String[] args) {
        int num1=3;
        int num2=1;
        System.out.println("---------------");
        try{
//            需要检测的代码
            int result=num1/num2;
        }catch (ArithmeticException a){
            System.out.println("catch ArithmeticException");
//            捕获异常后处理异常,程序不会崩溃
            a.printStackTrace();
        }finally {
//            一定会被执行的代码
            System.out.println("----finally----");
            System.out.println("----end----");
        }
    }
}

MoreTryCatch案例

package Day.day_12;

public class Demo03morectrcatch {
    public static void main(String[] args) {
        int num1 = 3;
        int num2 = 1;
        System.out.println("----start----");
        try {
            //需要检测的代码(可能会抛出异常,也可能不会抛出异常)
            int result = num1 / num2;
            int[] array = new int[2];
            array[2] = 3;
            //Unreachable
        } catch (ArithmeticException e) {
            System.out.println("catch ArithmeticException");
            // 捕获异常后处理异常,程序不会崩溃
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("catch ArrayIndexOutOfBoundsException");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("catch Exception");
            e.printStackTrace();
        } finally {
            //一定会被执行的代码(不管异常抛不抛出都会执行,例如数据库释放连接)
            System.out.println("---finally----");
        }
        System.out.println("----end----");
    }
}

Bank案例

没钱异常:

package Day.day_12.Demo04Bank;

public class MeiQianException extends Exception{
    private double money;
    public MeiQianException(double money){
        super("余额不足还差"+money);
        this.money=money;
    }
}

账户管理端:

package Day.day_12.Demo04Bank;

public class Account {
    // 余额(当前账号有多少钱)
    private Double balance;
    // 账号
    private int number;

    public Account(int number) {
        balance=0.0;
        this.number = number;
    }


    // 存钱 deposite
    public void deposite(double amount) {
        balance = balance + amount;
    }
    // 取钱withdraw
    public void withdraw(double amount) throws MeiQianException {
        if (amount <= balance) {
            balance = balance - amount;
        } else {
            double need = amount - balance;
            throw new MeiQianException(need);
        }
    }
    public Double getBalance() {
        return balance;
    }
    public void setBalance(Double balance) {
        this.balance = balance;
    }
    public Integer getNumber() {
        return number;
    }
    public void setNumber(Integer number) {
        this.number = number;
    }
}

用户端:

package Day.day_12.Demo04Bank;

public class BankAdmin {
    public static void main(String[] args) {
        Account accountAdmin = new Account(1);
        System.out.println("存款500");
        accountAdmin.deposite(500);
        System.out.println("取钱100");
        try {
            accountAdmin.withdraw(100);
        } catch (MeiQianException e) {
            e.printStackTrace();
        }
        System.out.println(accountAdmin.getBalance());
        System.out.println("取款600");
        try {
            accountAdmin.withdraw(600);
        } catch (MeiQianException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值