Article directory
Table of contents
foreword
Hello everyone, I love animal milk the most. Today I will introduce to you the affectionate brother in java- Exception
The autumn wind is lingering and the autumn water is cold, a little sad and sad; this life is silent and only dances lightly, breaking through the nine heavens for Qing!
One, the origin of Exception
C and other early languages often had multiple error-handling modes, which were often based on conventions rather than part of the language. Usually a special value or mark is returned, and by checking the returned value or mark, it has been determined whether an error has occurred. However, with the passage of time, people discovered!!!
Proud programmers are more likely to think when using a library: "Yes, mistakes may happen, but they are caused by others, and it is none of my business"
So, it's not surprising that programmers don't check for bugs, how can this work!!!
Therefore, those who are out of the ordinary are also
If we add exceptions to the code, we can not only increase the robustness of our code, but also reduce the complexity.
so the exception is very important, the company code is indispensable when looking for projects in the future!
Second, the exception class
Second, the basic exception
Let's first understand the exception condition: it is a problem that prevents the current method or scope from continuing to execute
Yes, it is a question, so what is the difference between it and ordinary questions?
A common problem is that enough information is available in the current environment to always handle the error.
For abnormal problems, you can't go on anymore, because you can't get the necessary information to solve the problem in the current environment, what you can do is to jump out of the current environment and hand over the problem to the upper-level environment
for example
So how do we throw an exception?
Java provides us with the keyword throws
Like other classes in java, we always create exception objects on the heap and pass a reference to the created object to throw
Since we can throw exceptions, should we also accept and handle exceptions? Here we introduce our affectionate brother catch
No matter where try is, brother catch can always arrive at the first time, just like someone in front of the screen
try{
Code that may throw exceptions
}catch(Exception e){
exception handler
}finally{
Regardless of whether there is an exception or not, the code in the final block will be executed regardless of whether the exception is handled or not.
}
Third, custom exceptions
The inheritance system provided by java cannot foresee all the exceptions that you want to report, so you can customize exceptions to represent specific problems that may be encountered in the program
Summarize
The above is the main content of our blog post. You don’t need to know so much about the bottom layer, just master the usage.
In the next article, I will bring you a classification of abnormalities, so stay tuned!