Java异常(2)- 捕获和抛出异常
2023-02-18 16:23:22 时间
异常处理五个关键字:try,catch,finally,throw,throws
捕获异常
try、catch、finally
package oop.demo10; public class Outer { public static void main(String[] args) { int a = 1; int b =0; //捕获多个异常:范围从小到大 try {//try监控区域 System.out.println(a/b); } catch (Error e) {//catch(想要捕获的异常类型)捕获异常 System.out.println("Error"); } catch (Exception e) { System.out.println("Exception"); }catch (Throwable e) { System.out.println("Throwable"); }finally {//处理善后工作 System.out.println("finally"); } } }
选中代码:Ctrl+Alt+T快捷键
抛出异常
throw:一般用于方法中抛出异常
throws:在方法上抛出异常
package oop.demo10; public class Outer { public static void main(String[] args) { new Outer().test(1,0); } //假设方法中,处理不了这个异常。那就通过throws在方法上抛出异常 public void test(int a,int b) throws ArithmeticException{ if (b==0){ throw new ArithmeticException();//throw 主动抛出异常 }else{ System.out.println(a/b); } } }
相关文章
- 六西格玛与现有的项目管理方法有哪些不同?
- R绘图 | 快速入门ggplot2
- Pandas | 数据排序
- 小游戏变现的商业模式和实现效果
- 大数据Kudu(五):Kudu基于Cloudera Manager安装及配置
- Web3 和 NFT 游戏:为即将到来的风险和回报做好准备
- Xmind 2022 for Mac(思维导图软件)中文版v22.10.927
- 从输入URL到渲染的完整过程1
- 图解JVM整体结构、执行流程以及2种架构模型,你学会了吗?
- 中了exe病毒文件夹变exe解决办法
- Fallback多级降级,Request Cache减压。你知道是怎么实现的吗?
- 缓存雪崩、缓存穿透解决方案以及实战
- 常用的标准LCD驱动芯片,性价比高,稳定性好,多种封装型号选择VK1056
- 阿里大佬倾情力荐:Java全线成长宝典,从P5到P8一应俱全
- 从家庭场景到工商业场景,涂鸦智能携手一元电气打造储能为核心的智慧能源管理方案
- TG Pro for mac(硬件温度检测工具) v2.76激活版
- 深圳世微补光灯 恒流调光IC调光深度0.1%无频闪
- Commander One for Mac(双窗口文件管理器)v3.5免激活版
- 广域铭岛入选“2022年制造业质量管理数字化典型场景和解决方案优秀案例”
- 如何进行react状态管理方案选择