The first few sections are the basis of the basics, starting with the third note, each time the notes are for a knowledge block of Java.
Java Exception Handling
1. What is an exception?
An exception is a run-time error. For example: divide by one 0; the array is out of bounds; the read file does not exist.
exception handling, to control exceptions within the programmer's reasonable range. An array of out-of-bounds Indexoutofarray is not understandable to the user, so the programmer wants to capture this exception, and feedback to the front end tells the user that it is "out of range."
An exception object is automatically generated in Java for exception events. This exception object encapsulates the information for the exception event and submits it to the Java runtime System. The Java Runtime system prints the exception event information encapsulated in the exception object to the console. What we did was catch the exception before he handed it to the Java runtime System. So the question is, how do we catch the anomaly? Use a catch statement. This arithmeticexception exception, such as catch (ArithmeticException e), is generated by the Java runtime to help you. When the catch is used, when the exception is used Bai, to match with try to use. As in Microsoft products, the system is now in error, would you like to report this error to us.
What about throws? Throws is used in the method. Throws explains the exceptions that may occur with my method. such as NullPointerException.
2. What is the system structure in the exception?
Throwable is a root class. Below it is a exception class, an error class. Error is internal to the system, and the programmer cannot control it. Exception is an exception that programmers can solve. Then the exception class is divided into RuntimeException class and non-RuntimeException class.
RuntimeException can be used without catch, but non-runtimeexception must catch. Exceptions that are throws after methods in the JDK's API are non-runtimeexception exceptions. such as IOException. These exceptions must be captured. Otherwise, the program cannot run.
Printstacktrace, print out all the causes that could be causing the exception. Literally, it is the trajectory of all stacks.
Summary: Throwable-->error
-->exception-->runtimeexception
--Non-runtimeexception (must be captured)
For example, like: fileinputstream in = new FileInputStream ("123.txt"), this statement in Eclipse, if not try...catch statement to wrap, its small red fork will remind you: unhandled exception of FileNotFoundException. And then immediately following it there will be a reminder: Add throws declaration or surround with Try/catch. We have come to the conclusion. For throws exceptions in the methods in the JDK API documentation, you must capture them. Otherwise, the program has a bug.
3. When a try contains a statement that may have more than one exception, you can use multiple catch combinations later.
The logic of the thing is this: the code in the try...catch/catch/catch...finally.finally will execute. That is, the code in the finally will be executed, regardless of whether an exception is generated.
Why should we introduce this mechanism? When a exception is caught, the program is not executed. Let finally the statement in whatever state is executed, to ensure that the program can run normally. Finally provides a unified exit for the exception section. Finally, it is typically used for cleanup of resources, such as closing open files and deleting temporary files.
4, for the exception can not be handled, how to deal with?
Try...catch in this structure, catch is used to capture the structure that handles exceptions. So, what if we can't handle the anomalies? That would use another keyword, throws.
void function () throws FileNotFoundException, ioexception{} In this way, you are only responsible for throwing the exception of this piece of code. You just have to throw it out, because you can't handle it. then, when the other function is to be called, then it is necessary to write try...catch or to use the throws exception.
However, the final exception must be handled, and can no longer be swallowed in the program itself. How can this procedure be maintained? E.printstacktrace () (print out all of its stack information) or record it in a log document with log4j.
The best thing to do is to throw an exception with throws on the public static void Main method, giving the exception to the Java runtime System. Exceptions are what programmers should do and teach Java runtime systems to do poorly in programming habits.
5. Knowledge supplement to Be continued:
An exception-handling function that is called by another function. They can use throws at the first level to throw exceptions, do not handle exceptions, but eventually, they will have to use Try...catch to catch handling exceptions, and they must not be handed over to the Java runtime System for processing.
When we debug the code, such as an array out of bounds an error, on the console, you will see a series of errors thrown. They are all based on e.printstacktrace to print out the exception. And because they are all at all levels with throws to throw an exception, do not handle the exception, so, only then, a bug, will be the stack space of all the exception print out. Because you know, in the Java API, a lot of functions are just throws exceptions, not processing. At this time, to focus on ... (Test.java 18), follow the content at the end of the, indicating which file in which the first few lines of error.
6, catch the time, pay attention to the order of the catch problem.
First catch small, subclass of exception. Recapture large, such as exception anomalies. If the exception exception is caught first, then the subsequent exception cannot be captured again.
After the exception is thrown, the remaining parts of the try are not executed.
Overriding the method, you need to throw the same exception as the original method, not large, not small. But you don't have to write throws. Remember!
7. Custom exceptions?
Customize the exception to build your own exception class. You just have to inherit exception or inherit runtimeexception. such as: When landing the webpage, we custom exception, user not found exception, or password is uncorrect exception.
The finer of the exception is the error of the user name, or the wrong password, or the wrong check code. The rough writing is the error. Obviously, the first anomaly is a better effect.
Java Basics Review Notes series three