当前位置:网站首页>Exception handling exception
Exception handling exception
2022-07-25 10:15:00 【Look at the bugs】
1, Throw an exception
①, Throw and catch exceptions in methods
// In the main function
try{
new A().test(1,0);}
catch (ArithmeticException e){
e.printStackTrace();
}
// Suppose in this method , Can't handle this exception , Method will actively throw an exception
// Method and then catch the exception in the main function to ensure the normal operation of the program
public void test(int a ,int b) throws ArithmeticException{
if(b==0){
throw new ArithmeticException(); // Actively throw an exception Generally used in methods
}
②, Exceptions that may be encountered during program execution , Direct selling
if(b==0){
throw new ArithmeticException(); // Actively throw an exception Generally used in methods
}
2, Capture exception
>VirtulMachineError
The order Throwable>Error>AWTError
>Exception>IOExeception
>RUNtimeException
//Ctrl+Alt+T Can quickly generate exception capture mechanism
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();// Print the captured stack information
} finally {
}
// Suppose that to catch multiple exceptions, the order needs to be from small to large
try{
new demo01().a();
}catch (Error e){
System.out.println(" Divisor cannot be zero ");
}catch ( Exception e){
System.out.println("Exception");
}
catch (Throwable e){
System.out.println("Throwable");
}
//finally Can don't
finally {
System.out.println("finally");
}
3, Custom exception
In order to implement some operations that are not exceptions but are conditional
// Define a variable to receive values >10 The abnormal
private int detail;
// Constructors
public MyException(int a){
this.detail=a;
}
//toString : Abnormal print information
@Override
public String toString() {
return "MyException{"+detail +'}';
}
-------------------------
public class Test {
// There may be abnormal methods
static void test(int a){
System.out.println(" The parameter passed is "+a);
if(a>10){
try {
throw new MyException(a);
} catch (MyException e) {
e.printStackTrace();
}
}
System.out.println("OK");
}
public static void main(String[] args) {
test(11);
}
4, Abnormal summary
1, In multiple exceptions, you can add one at the end catch(Exception) To handle possible missing exceptions
2, Try to deal with exceptions , Do not call simply printStackTrace() To print out
3, Try to add finally Statement block to release some occupied resources
边栏推荐
- 腾讯云之错误[100007] this env is not enable anonymous login
- JSP details
- CCF 201512-3 drawing
- NPM details
- [recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
- salt常见问题
- 微信小程序跳转其他小程序
- @Import,Conditional和@ImportResourse注解
- VLAN的配置及其应用(以华为eNSP为例)
- vscode插件开发
猜你喜欢

Common methods of nodejs version upgrade or switching

CCF 201604-2 Tetris

线程池的死锁事件
![[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)](/img/6e/da80133e05b18c87d7167c023b6c93.gif)
[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)

mysql 解决不支持中文的问题
![腾讯云之错误[100007] this env is not enable anonymous login](/img/a2/a209a0d94e3fbf607242c28d87e2dd.png)
腾讯云之错误[100007] this env is not enable anonymous login

字典树的使用

UE4 窗口控制(最大化 最小化)

UE4 碰撞(Collsion)

CCF 201512-4 delivery
随机推荐
Download and installation of QT 6.2
See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
Reflection 反射
vscode插件开发
Selenium 等待元素出现与等待操作可以执行的条件
JDBC操作数据库详解
MVC three-tier architecture understanding
Redux使用和剖析
[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.
[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand
文件的上传功能
多线程——Callable接口,lambda
Vant problem record
UE4 碰撞(Collsion)
概率论与数理统计 3 Discrete Random Variables and Probability Distributions(离散随机变量与概率分布) (下篇)
CentOS install redis
ROS distributed operation -- launch file starts nodes on multiple machines
几个常用的网络诊断命令
Detailed explanation of JDBC operation database
多线程——Runnable接口,龟兔赛跑