35 异常机制 异常处理机制 异常处理五个关键字 try、catch、finally、throw、thorws 代码
2023-09-11 14:16:44 时间
异常处理机制
概念
-
抛出异常
-
捕获异常
-
异常处理五个关键字
- try、catch、finally、throw、thorws
代码
// main
{
int a = 1;
int b = 0;
// 假设要捕获多个异常:从小到大
try{// try 监控区域
if(b==0){ //throw thorws
throw new ArithmeticException();// 主动抛出异常
}
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");
}
// finally 可以不要finally,假设IO,资源,关闭!
}
public void a(){
b();
}
public void b(){
a();
}
// --------------------
// main
// 快捷键:ctrl+alt+t
int a = 1;
int b = 0;
try{
System.out.println(a/b);
}catch{
System.exit(1); // 结束程序
}finally{
}
// --------------------
// main
{
new test().test(1,0)
}
// 假设这方法中,处理不了这个异常。方法上抛出异常
public void test(int a ,int b) throws ArithmeticException{
if(b==0){ //throw thorws
throw new ArithmeticException();// 主动抛出异常,一般在方法中使用
}
System.out.println(a/b);
}
相关文章
- [Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
- 【Linux异常】执行man proc查看proc文档时提示错误 man: can't set the locale; make sure $LC_* and $LANG are correct No manual entry for proc
- 【异常】ERROR in ch.qos.logback.core.joran.spi.Interpreter@159:22 - no applicable action for [charset], current ElementPath is [[configuration][appender][encoder][charset]]
- 编写高质量代码改善C#程序的157个建议[用抛异常替代返回错误、不要在不恰当的场合下引发异常、重新引发异常时使用inner Exception]
- 编写高质量代码改善java程序的151个建议——[110-117]异常及Web项目中异常处理
- java代码将e.printStackTrace()写入log4j文件异常信息
- 利用Python进行异常值分析实例代码
- Hbase2.1.0-CDH6.3.2 Region in Transition (永久RIT) 异常解决
- [FAQ] MetaMask ALERT: 交易出错. 合约代码执行异常.
- 程序员的职业病(职业素养)之一:动手写业务代码之前先考虑异常处理
- HRESULT:0x80070057 (E_INVALIDARG)的异常的解决方案
- Atitit 提升水平 把代码写的有技术含量 目录 1. 提高可读性(重要)2 1.1. 异常模式代替返回值2 1.2. Dsl2 1.3. 流畅接口方法链2 1.4. 层次结构抽象2 1
- Java编程常见内存溢出异常与代码示例
- 【异常】IDEA启动之后,提示java.lang.ClassNotFoundException(找不到或无法加载主类)
- 【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
- 用于回归问题的异常鲁棒极限学习机(ORELM)(Matlab代码实现)
- 关于C++的异常处理,下列说法正确的有?
- 爬虫日记(101):Twisted:使用Deferred重构异常代码
- [springMVC学习]12、异常处理
- 【状态估计】观测信号(包括异常值)的状态估计方法(Matlab代码实现)