当前位置:网站首页>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 )
边栏推荐
- 【LDA】EM变分推理 粗略版笔记【待完善
- Jenkins' RPC test project
- 學習是一件逆人性的事情(成為高手的內功心法)
- Function recursion example
- 三维重建系统 | L3增量运动恢复结构(增量SFM)
- [wp][beginner level] attack and defense world game
- PTA:自测-1 打印沙漏 (20分)
- Pta: self test -2 prime pair conjecture (20 points)
- Phpstudy indicates that the hosts file may not exist or be blocked from being opened. How to resolve the failure of synchronizing hosts
- ROS初学者编写小乌龟以一定速度旋转一定角度的server
猜你喜欢
随机推荐
Left aligned, right aligned, random number, goto, compare output bool
First set and follow set in vernacular
PTA:自测-1 打印沙漏 (20分)
Selenium advanced
h3c GR5200路由器上如何设置公网ip可以访问
Jenkins' RPC test project
安装PS软件时提示程序无法访问关键文件/目录,错误代码:41的解决方法
Browser fingerprint interpretation
学习是一件逆人性的事情(成为高手的内功心法)
[LDA] rough version notes of EM variational reasoning [to be improved
机器人前行、旋转的service编写
三维重建系统 | L3双视角运动恢复结构(SFM双目SFM)
Structure example
Scala下载及IDEA安装Scala插件(保姆级教程超详细)
[datetmeformatter] realize the conversion between localdatetime and text
【Environment】1. Get the configuration in YML through the environment in the configuration class
SQL cross database injection
解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击
ngork实现内网穿透--免费
odom坐标系的理解









