当前位置:网站首页>异常处理4种方法
异常处理4种方法
2022-06-28 09:21:00 【用户9857551】
异常处理4种方法
throw关键字:可以在指定方法中抛出指定异常。 使用格式:throw new xxxException(“异常产生的原因”); 注意: 1.throw关键字必须写在方法内部。 2.throw关键字后边的new对象必须是Exception或者是Exception的子类对象。 3.throw关键字抛出指定的异常对象,我们就必须处理。 throw关键字后边是RuntimeException或是RuntimeException的子类对象,我们就可以不处理,默认交给JVM处理(打印异常,中断处理)。 throw关键字后边是编译异常(写代码报错),我们必须处理这个异常,要么throws要么try-catch。
1.throws使用
修饰符 返回值类型 方法名(参数列表)throw xxxException...{
throw new xxxException("产生原因");
throw new xxxException("产生原因");
}如果还有后续代码,就用到try-catch,在try-catch外边写。
2.try-catch
格式:
try{
可能产生异常的代码
}catch(定义一个异常的变量,用来接收try中抛出来的异常对象){
异常的处理逻辑,异常之后怎么处理异常对象
一般会记录在日志中
}
......
catch(异常类名 变量名){
}注意: 1.try可能抛出多个异常,可以多个catch处理 2.产生异常catch处理完了,继续进行下边的代码。如果没有异常不执行catch,继续下边的代码。
3.Throwable类
这个类下边有三个方法
4.finally代码块
这个代码块不能单独使用,是和try-catch一块用的,一般用于资源回收,资源释放。有时候某些代码必须执行就可以写到finally里边。 注意:尽量不要再finally里边写return,永远返回finally里的结果。
try{
可能产生异常的代码
}catch(定义一个异常的变量,用来接收try中抛出来的异常对象){
异常的处理逻辑,异常之后怎么处理异常对象
一般会记录在日志中
}
finally{
必须要执行的代码,无论是否出现异常,都会执行。
}边栏推荐
- Learn how Alibaba manages the data indicator system
- PMP needs to master its own learning methods
- 什么是在线开户?现在网上开户安全么?
- Illustration of MySQL binlog, redo log and undo log
- Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
- 如何实现基于 RADIUS 协议的双因子认证 MFA?
- spark的资源调度和任务调度
- STL - inverter
- Implementation of single sign on
- Scenario method and error recommendation method for learning basic content of software testing (2)
猜你喜欢

Assertions used in the interface automation platform

For the development of short video app, the elder warned me to choose the open source code

Calculation of stock purchase and sale expenses

基于宽表的数据建模

满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”

Write a simple timeline

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version

How to solve the problem of port number occupation

Apache Doris 成为 Apache 顶级项目

Data visualization makes correlation analysis easier to use
随机推荐
1182:合影效果
Valentine's Day - VBS learning (sentences, love words)
Write a simple timeline
Copy & Deepcopy
Is it safe to open an account for mobile phone stock speculation?
Check whether the table contains rows SQL Server 2005 - check whether a table contains rows or not SQL Server 2005
DEJA_VU3D - Cesium功能集 之 052-模拟卫星轨道(高空)效果
P2394 yyy loves Chemistry I
new URL(“www.jjj.com“)
Common tools for interface testing --postman
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
虚拟机14安装win7(图教程)
Import and export of a single collection in postman
Interpretation of new products: realm launched GT neo2 Dragon Ball customized version
Using transform:scale causes the page mouse hover event to disappear
State machine program framework
数字人行业爆发在即,市场格局几何?
Test cases for learning the basic content of software testing (II)
The digital human industry is about to break out. What is the market pattern?
Implementation of single sign on