当前位置:网站首页>Self induction of exception handling
Self induction of exception handling
2022-06-12 15:08:00 【CXgeng】
Exception handling self induction
Exceptions are classified as follows :
Object class :
Throwable class
error:
exception : Checked exceptions and runtime exceptions ( Not subject to inspection )
error
: The main exception is java Runtime system internal error or resource exhaustion error ( Usually there is not enough memory or the bytecode is illegal ), Such errors are not caused by the application itself , It's a hardware problem , The application will not throw such an object . If such an error occurs , Try to make the program end safely .
exception
: It contains two main types of exceptions , Checked exceptions and runtime exceptions (RuntimeException).
Abnormal under examination : It refers to the exceptions that the compiler forces you to handle when you are writing code , If the code is not disposed, an error will be reported ( Can't compile ).
Runtime exception : It refers to exceptions that the compiler does not force you to solve , Although your code may go wrong , But the compiler allows you to compile .
Common checked exceptions
1、IOException( Input or output exception , I.e. read / write exception )
What happened : Generally, it is easy to release this situation when reading and writing data
2、FileNotFoundException
What happened :: establish File Object time ( One is “ Access denied ”, Two is “ The system cannot find the specified path ”)
3、ClassNotFoundException
What happened : (1)web.xml The configuration of this class does not exist in the file at all, or the path of the configuration is written incorrectly ;
( Quite often see )
(2)web.xml There is... In the question , But the class in the project has the wrong name ;
(3) Class is placed in the wrong folder ;
4、NullPointException( Null pointer exception )
Common unchecked exceptions
1、ClassCastException( Class conversion exception )
What happened : yes JVM Runtime exception thrown when a conversion incompatibility between two types is detected
2、IndexOutOfBoundsException( Array out of bounds exception )
What happened : Array operations are usually prone to occur when they are out of bounds
3、NullPointerException( Null pointer exception )
What happened : Variables of defined reference types , give null, It does not point to an actual memory space when this variable is used , This error will occur
ArrayStoreException( Data storage exception , The types are inconsistent when manipulating arrays )
4、BufferOverflowException( also IO Operation of the , Buffer overflow exception )
When handling exceptions , When to use try… catch , When to use throws
If the exception is caused by parameter passing , Should be used trows Throw it out
Use in other situations try ...catch Solve it on the spot
To put it bluntly, if the current method generates an exception for its own reason , It should be solved by itself , If you don't solve it yourself , Its exceptions will always exist , All the time , Until it's thrown JVM Where? , I went around and came back , It can't be solved . But if it is generated by other objects it uses , It should be thrown out at this time , Throw it to the object that calls it
finally Deep understanding of keywords ( Questions often asked in interviews )
What is usually said finally stay try...catch After the statement , No matter whether there is an exception , It will be executed in the end
But under what circumstances finally It's not enforced :
Generally, the computer is powered off suddenly , The program does not exist in memory , Or the exit program code is executed
(system.exe());
When the program is executed return After the statement ,finally Whether the statement is executed ?
The answer is yes , Because when executing return When the sentence is , The program will now prepare a Return results , Even if no return is required
result , The program will also return a result with no result , And in the meantime , Namely finally The stage of statement execution
Judge the output value :
public static void main(String []args){
Person p = Go();
System.out.println(p.age);
}
public static void Go{
try{
Person p = new Person();
p.age = 20;
return p;
}catch(Exception e){
}
finally{
p.age = 10;
}
}
static class Person{
// Member inner class
int age;
}
Output at this time What is your age ??
The output at this time by 10, Because when you execute finally When the sentence is ,return Ready to return object , In this case, the value of the returned object is a copy p Value , The copied value is an address value , To put it bluntly , When it outputs, it points to the value in the heap memory ( Have already been finally Revised ), So his output is 10;
public static void main(String []args){
int a = Go();
System.out.println(a);
}
public static void Go{
try{
int a = 20;
return a;
}catch(Exception e){
}
finally{
a = 10;
}
}
Output at this time What is your age ??
The output value is 20, Because when you execute finally When the sentence is , The returned value is also ready , The value returned at this time , It's backup Variable a Value , Now the variable a The memory is stack memory , In this case, the returned is the copied a Value , It does not refer to the stack memory a , here finally The executed statement changes the value in the stack memory , So the output is 20;( The root cause is the relationship between reference types and non reference types )
边栏推荐
- Chapter I exercises of program construction and interpretation
- MH32F103ARPT6软硬件兼容替代STM32F103RCT6
- How to add WWW to the domain name
- 应势而变,2022年下半场的升级之路
- Jetpack architecture component learning (3) -- activity results API usage
- 频繁项集产生强关联规则的过程
- 三维重建系统 | L3双视角运动恢复结构(SFM双目SFM)
- ES6新特性
- 基于TensorRT的深度学习模型部署实战教程!
- Function recursion example
猜你喜欢

MH32F103ARPT6软硬件兼容替代STM32F103RCT6

Ankai microelectronics rushes to the scientific innovation board: the annual revenue of 500million Xiaomi industry fund is the shareholder

野指针理解

C scanf function

Seaborn Brief

ngork实现内网穿透--免费

Yiwei lithium energy plans to raise 9billion yuan: liujincheng and luojinhong jointly subscribe for 6billion yuan of layout Optical Valley

启明云端分享| 通过Matter协议实例演示开关通过matter协议来做到对灯亮灭的控制

三维重建系统 | L3增量运动恢复结构(增量SFM)

C string
随机推荐
关于互联网大厂裁员
Tensorrt based in-depth learning model deployment practice tutorial!
Pta: self test -3 array element cyclic right shift problem (20 points)
NETCORE combined with cap event bus to realize distributed transaction - message (2)
Use of boost:: bind() in ROS
Common assertions in JUnit testing
idea 拉取分支代码
Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
【Environment】1. Get the configuration in YML through the environment in the configuration class
Function related matters
ROS 中 boost::bind( ) 的使用
JUnit test suite method sorting (method 2 is not easy to use)
应势而变,2022年下半场的升级之路
ROS初学者编写小乌龟以一定速度旋转一定角度的server
Data collection
Ankai microelectronics rushes to the scientific innovation board: the annual revenue of 500million Xiaomi industry fund is the shareholder
New关键字、引用&与指针的学习记录
【Instant】1. Equivalent to date class 2 Represents a moment
leetcode每日一题-公平的糖果棒交换
C 操作符