Java—Exception异常处理try-catch

runtime
运行的时候出现的异常,程序本身没有问题。

在这里插入图片描述

  • error:跟系统本身有关的异常
  • Arithmetic:与运算法则相关的问题
  • Nullpointer:对象没有初始化,对象所应用的内容不存在
  • IndexOutOfBounds:下标超出了数组的范围
  • IllegalArgument:语法没有符合要求,比如1/2输出0,与规则相关。

checked exceptions VS unchecked exceptions
能否被编译器查出来的
checked—error
unchecked—runtimeException

  • try-catch
try {
   cash=Long.parseLong(in.nextLine());
  }
  catch(NumberFormatException nfe) {
   System.out.println("输入金额不正确,请重新输入取款金额");
  }
  • 在两个方法里的 try-catch
public void withDraw(long cash) throws InterruptedException{
  if(..)
   throw new InterruptedException();
  else{
      ...
  }
}
public void B() {
  try {
   Bank bank=new Bank();
   bank.withDraw(cash);
  }
  catch (InterruptedException ie) {
   System.out.println("异常对象中保存的错误消息");
  }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值