浅析Java中的异常处理机制
作者:Hateyes 时间:2021-08-19 05:42:48
异常处理机制
1、抛出异常
2、捕获异常
3、异常处理五个关键字:
try、catch、finally、throw、throws
注意:假设要捕获多个异常:需要按照层级关系(异常体系结构) 从小到大!
package exception;
/**
* Java 捕获和抛出异常:
* 异常处理机制
* 1、抛出异常
* 2、捕获异常
* 3、异常处理五个关键字
* try、catch、finally、throw、throws
* 注意:假设要捕获多个异常:需要按照层级关系(异常体系结构) 从小到大!
*/
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
/**
* try catch 是一个完整的机构体,finally 可以不要
* 假设IO流,或者跟资源相关的东西,最后需要关闭,关闭的操作就放在 finally 中
*/
try { //try 监控区域
System.out.println(a / b);
} catch (ArithmeticException exception){ //catch(想要捕获的异常类型) 捕获异常
System.out.println("程序出现异常,变量b不能为0");
} finally { //处理善后工作
System.out.println("finally");
}
System.out.println("-------------- 分隔符 --------------");
try {
new Test().a(); //无限循环
} catch (Error error){
System.out.println("Error");
} catch (Exception exception){
System.out.println("Exception");
} catch (Throwable throwable){
System.out.println("Throwable");
} finally {
System.out.println("finally");
}
}
public void a(){
b();
}
public void b() {
a();
}
}
捕获异常
快捷键:选中代码 Ctrl + Alt + T
捕获异常的好处:程序不会意外的停止,try catch 捕获异常后程序会正常的往下执行
package exception;
/**
* 捕获异常快捷键
* 选中代码后:Ctrl + Alt + T
* 如:
* 选中 System.out.println(a / b);
* 然后快捷键 Ctrl + Alt + T
*/
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a / b);
} catch (Exception exception) {
exception.printStackTrace(); //打印错误的栈信息
} finally {
}
}
}
抛出异常
1、在方法中抛出异常:throw
2、在方法上抛出异常:throws
package exception;
/**
* 捕获异常
* 抛出异常
*/
public class Test3 {
public static void main(String[] args) {
/**
* 方法中抛出异常
*/
new Test3().test(1,0); //匿名内部类直接调用
System.out.println("------------ 分隔符 -------------");
/**
* 方法上抛出异常
* 捕获异常的好处:
* 程序不会意外的停止,try catch 捕获异常后程序会正常的往下执行
*/
try {
new Test3().test2(1,0); //匿名内部类直接调用
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
/**
* 在方法中抛出异常:throw
* @param a
* @param b
*/
public void test(int a, int b){
if (b == 0){ //throw
throw new ArithmeticException(); //主动抛出异常,一般在方法中使用
}
System.out.println(a / b);
}
/**
* 假设在方法中处理不了这个异常,就在方法上抛出异常,然后捕获异常
* 在方法上抛出异常:throws
* @param a
* @param b
* @throws ArithmeticException
*/
public void test2(int a, int b) throws ArithmeticException{
if (b == 0){
throw new ArithmeticException();
}
}
}
来源:https://www.cnblogs.com/ming2/p/13905469.html
标签:Java,异常处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Selenium实现多窗口切换的示例代码
2022-01-22 22:48:08
Java解码H264格式视频流中的图片
2023-11-24 23:58:24
Spring MVC项目中的异常处理详解
2021-12-22 09:19:20
![](https://img.aspxhome.com/file/2023/9/69929_0s.jpg)
gson对象序列化的示例
2023-11-25 08:54:28
![](https://img.aspxhome.com/file/2023/6/59956_0s.png)
Java详细分析梳理垃圾回收机制
2023-10-30 04:02:33
![](https://img.aspxhome.com/file/2023/5/62765_0s.png)
java多线程Thread的实现方法代码详解
2022-01-03 01:02:28
Java微信公众平台开发(14) 微信web开发者工具使用
2023-01-30 19:21:34
![](https://img.aspxhome.com/file/2023/0/67150_0s.jpg)
Java IO之File 类详解
2023-08-07 20:02:12
![](https://img.aspxhome.com/file/2023/2/57802_0s.png)
springboot+mybatis-plus 两种方式打印sql语句的方法
2022-12-29 13:41:11
![](https://img.aspxhome.com/file/2023/9/64379_0s.jpg)
Java新手环境搭建 JDK8安装配置教程
2023-11-25 17:23:10
![](https://img.aspxhome.com/file/2023/1/60311_0s.jpg)
将JavaDoc注释生成API文档的操作
2023-06-16 18:24:06
![](https://img.aspxhome.com/file/2023/1/61151_0s.png)
java中初始化MediaRecorder的实现方法
2023-11-29 03:54:52
Java集合定义与用法实例总结【Set、List与Map】
2023-11-21 08:26:40
![](https://img.aspxhome.com/file/2023/8/59588_0s.png)
C#中 MessageBox的使用技巧
2023-06-25 16:15:48
![](https://img.aspxhome.com/file/2023/2/85682_0s.webp)
spring boot 动态生成接口实现类的场景分析
2022-12-14 05:09:33
![](https://img.aspxhome.com/file/2023/7/64207_0s.png)
Java正则验证正整数的方法分析【测试可用】
2022-08-02 21:50:05
![](https://img.aspxhome.com/file/2023/9/66229_0s.jpg)
Android实现悬浮窗的简单方法实例
2023-06-17 18:11:02
java实现打砖块游戏算法
2023-01-28 20:51:06
JAVA SPI特性及简单应用代码实例
2021-11-11 14:54:54
![](https://img.aspxhome.com/file/2023/4/63674_0s.png)
Java通过反射将 Excel 解析成对象集合实例
2023-05-22 18:49:02
![](https://img.aspxhome.com/file/2023/2/69942_0s.jpg)