Previously, when I heard the boss explain the exception mechanism to us, I wanted to write something about it. It took two weeks to write.
First, let's get rid of what we want to talk about today.
1. Why exceptions?
2. Categories of exceptions
3. What are precautions for exception handling?
4. How should I use exceptions in projects?
Why is there an exception? During program execution, running errors such as Division by zero overflow and array out-of-bounds often occur, affecting the normal execution of the program. Errors and exceptions are inevitable. A good application should be able to anticipate various exceptions that may occur during program execution while satisfying various features required by users, and can handle exceptions properly. In Java, this technology is exception handling.
The Java language uses an object-oriented exception processor to solve errors during runtime, which can prevent unexpected results caused by incorrect program code or system errors. Reduces the workload of programmers and increases program flexibility.
Exception classification: first, exceptions are classified into two categories: Error and exception. Errors at the error level often refer to errors that cannot be controlled by Java programmers, such as common Java. Lang. outofmemoryerror
Another is exception, which is our most common exception. It is divided into two types: checked exceptions and non-checked exceptions. The code must be caught or throw an exception due to the check exception. As the name suggests, non-check exceptions do not require any force throw, but the most commonly used non-check exceptions in our program. In normal use, our programmers Process Code that may be wrong through the exception mechanism based on the expected errors of the Code. For example, you can return an error to the user to let the user know where the error has occurred or reply to the error.
Of course, usually exceptions are caught, and few people reply to the exceptions.
When learning exceptions, we must first know the functions of some methods. printstacktrace () prints the error message fillinstacktrace () on the console to add the exception chain to the initcause () location where the update record is incorrect,
Note the following when exceptions are used.
I threw an exception in the print () method. E. printstacktrace (); method, in fact, this is a very common error, many people will make this error. The error is not handled when an exception is caught. The Exception error message is printed directly, which is convenient for programmers to debug, but useless for customers.
Next, let's take note of the second situation. Use finally carefully to read the code first.
Call lostmessage. F () when executing this program. The exception should be thrown because the information is a very important exception! But what about the results?
Is it strange why the exception is thrown. Yes, this is the loss of exceptions. This is a flaw in Java exception implementation. If you want to know why, you can check the exception mechanism of thinking in Java.
How can we reasonably use the exception mechanism in the project? You need to know that the purpose of using exceptions is to capture the expected exceptions where errors may occur and then process them or reply to them, you can either return a message to the user to let the user know what went wrong. So we need to match exceptions when using
When an exception is matched, you can handle the exception in the corresponding level.