In this article is a large paragraph of text description , I’ve simplified it , I hope this can help readers , Reduce the impatient mind of reading words . Bring more dry goods to you .
Java The purpose of exception handling in
It’s about simplifying the large by using a small amount of code today 、 Reliable program generation , And in this way you can be more confident : There are no unhandled errors in your app .
The implementation of exception handling can be traced back to 20 century 60 The operating system of the S , Even BASIC Linguistic on error goto sentence . and c++ The exception handling mechanism of is based on Ada,Java Exception handling in is based on c++ Based on .
The benefits of using exception handling
You don’t have to check at the method call , Because the exception mechanism will ensure that this error can be caught .
Basically abnormal
Abnormal situation (exceptional condition): A problem that prevents the current method or scope from continuing . It can’t go on , So jump out of the current environment , Put the problem to the next level of the environment .
General questions : Enough information is available in the current environment , Always deal with this mistake .
After throwing an exception : First , Same as Java The creation of other objects in the , Will use new Create an object on the heap . then , Current execution path ( It can’t go on ) Terminated , And pop up the reference to the exception object from the current environment . here , Exception handling mechanism takeover procedure , And began to find a proper place to continue the program .
The proper place is the exception handler , Its task is to recover the program from an error state , To enable the program to run in a different way , Or keep running .
Abnormal parameter
All exception classes have two constructors : One is the default constructor ; The other is to accept strings as parameters , So that the relevant information can be put into the constructor of the exception object :
throw new NullPointerException(”t=null”);
Capture exception
Monitoring area (guarded region): A piece of code that might generate an exception , Followed by the code that handles these exceptions .
- try block
To prevent an exception from being thrown inside a method , Causes the method to end in the process of throwing an exception , We set up a special block within the method to catch exceptions .
In this block “ Try ” Various ( May cause abnormal ) Method call , So called try block . Follow the keyword try The common block that follows .
try{
//Code that might generate exceptions
}
2. Exception handler
The exception thrown must be handled somewhere . This “ place ” It’s an exception handler . The exception handler follows try After block , With keywords catch Express :
try{
//Code that might generate exceptions
}catch(Type1 id1){
//handle exceptions of Type1
}catch(Type2 id2){
//Handle exceptions of Type2
}
//etc
Be careful :
stay try The interior of the block , Many different method calls can result in exceptions of the same type , You only need to provide an exception handler for this type .
Termination and restoration
Theoretically, there are two basic models of exception handling .
Java Support termination model ( It is Java and C++ Supported models ).
In this model , It’s critical to assume that it’s wrong , So that the program can’t return to the place where the exception happened and continue to execute . Once the exception is thrown , It means that the mistake is irreparable , And you can’t come back and carry on .
The other is recovery model .
The job of the exception handler is to fix the error , Then try calling the wrong method again , And think the second time will be successful .
For the recovery model : We usually want to be able to continue executing the program after the exception has been handled . If you want to use Java Implement recovery like behavior , Then you can’t throw an exception when you encounter an error , Instead, call methods to fix the error . perhaps , hold try Block placed while In circulation , It’s going to keep coming in try block , Until a satisfactory result is obtained .
The article is about to end here , I hope after you read it , It helps you all , In this way, my heart is happy , Is the biggest encouragement to me !
Thank you. , We grow together !