Java中try、catch的使用方法
作者:Locpeng 时间:2021-11-08 00:48:12
Java中有两种处理异常的方式,分别是用throws抛出异常、用try、catch捕获异常。
try-catch
在Java
try-catch语句的语法格式:
try{
//代码块
}catch(Exception1 e){
//抛出异常后所要进行的操作
}
当try语句当中捕获到异常时,会将异常抛出到catch语句中匹配异常类型,当异常和catch后面的异常类型相匹配时,则catch语句将会执行。
举个最基础的例子,除数不能为0
try中的异常与catch后面的异常类型匹配的情况:
try{
int i = 1/0;//发生异常立即跳往catch语句中执行,不执行异常代码下面的代码
System.out.println("输出结果为:" + i);
}catch(ArithmeticException e){
e.printStackTrace();
System.out.println("编译报错,除数不能为0");
}
输出结果:
try中抛出的异常与catch后面的异常类型不匹配的情况:
try{
int i = 1/0;//发生异常立即跳往catch语句中执行,不执行异常代码下面的代码
System.out.println("输出结果为:" + i);
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("编译报错,除数不能为0");
//因为异常类型不匹配所以该catch语句并未执行
}
输出结果:
一个try语句可以接多个catch语句
当有多个catch语句时,try部分的语句如果发生异常,就会在依次在各个catch语句中试图匹配异常类型。
try{
int i = 1/0;//发生异常立即跳往catch语句中执行,不执行异常代码下面的代码
System.out.println("输出结果为:" + i);
}catch(ArithmeticException e){
e.printStackTrace();
System.out.println("运算条件异常");
//try语句捕获的异常与该catch语句后的异常相匹配,所以执行该catch语句
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("空指针异常");
//try语句捕获的异常与该catch语句后的异常不匹配,所以不执行该catch语句
}
JDK1.7之后还有一种不常用的写法:
try{
int i = 1/0;//发生异常立即跳往catch语句中执行,不执行异常代码下面的代码
System.out.println("输出结果为:" + i);
}catch (NullPointerException | ArithmeticException e) {
e.printStackTrace();
if (e instanceof NullPointerException)
System.out.println("空指针异常");
if (e instanceof ArithmeticException)
System.out.println("运算条件异常");
}
这种写法需要通过instanceof 进行判断具体的异常类型。
两种写法的输出都是一样的:
在catch内部,通常下面的方法输出异常信息:
getMessage() 返回一个字符串对发生的异常进行描述。
printStackTrace() :打印异常信息出错的位置及原因。
来源:https://blog.csdn.net/Locpeng/article/details/106835636
标签:Java,try,catch,使用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解JAVA类加载机制
2023-11-08 04:05:16
![](https://img.aspxhome.com/file/2023/0/74910_0s.png)
C#使用ScrapySharp快速从网页采集数据
2021-08-14 09:25:14
![](https://img.aspxhome.com/file/2023/8/71728_0s.png)
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45
![](https://img.aspxhome.com/file/2023/2/104692_0s.gif)
Java JDK动态代理(AOP)用法及实现原理详解
2021-11-14 16:45:21
![](https://img.aspxhome.com/file/2023/2/69292_0s.png)
Java集合TreeSet用法详解
2023-11-10 22:53:34
![](https://img.aspxhome.com/file/2023/4/59354_0s.jpg)
MyBatis中的properties配置(推荐)
2023-06-04 20:47:58
Java实现企业员工管理系统
2023-08-22 16:44:50
基于C#实现XML文件读取工具类
2021-10-07 07:42:19
Java原生服务器接收上传文件 不使用MultipartFile类
2023-11-09 19:31:36
Springboot Vue实现单点登陆功能示例详解
2023-11-05 00:29:11
![](https://img.aspxhome.com/file/2023/7/58877_0s.png)
详解Spring Boot微服务如何集成fescar解决分布式事务问题
2022-01-21 18:43:51
![](https://img.aspxhome.com/file/2023/1/67951_0s.png)
Android 资源 id详解及的动态获取
2023-06-25 00:32:21
C语言实现两个矩阵相乘
2023-07-22 12:41:20
![](https://img.aspxhome.com/file/2023/6/103076_0s.png)
Java中的注解和反射实例详解
2023-02-02 04:25:45
![](https://img.aspxhome.com/file/2023/0/65780_0s.png)
超详细的Intellij IDEA 看源码必备技能
2021-10-27 09:03:16
![](https://img.aspxhome.com/file/2023/2/69312_0s.png)
Mybatis Trim标签用法简单介绍
2021-10-11 03:04:00
Spark JDBC操作MySQL方式详细讲解
2021-05-24 12:41:51
Java基于ShardingSphere实现分库分表的实例详解
2022-04-20 18:43:37
![](https://img.aspxhome.com/file/2023/0/76270_0s.jpg)
java 中HashCode重复的可能性
2021-09-13 17:38:05
一文带你了解C#中的协变与逆变
2022-08-06 22:31:21