Exception handling is the only formal error reporting mechanism in Java. and enforced by the compiler.
Exception Parameters
The difference between throwing an exception and the normal return value of the method is that the "place" returned by the exception is completely different from the "place" returned by the normal method call. (The exception will be resolved in an appropriate exception handler, where his location may be far from being thrown, and may span many levels of the method call stack.) )
The most important part of an exception is the class name.
Catching exceptions
Exception handling theoretically has two models: the terminating model and the recovery model. Java supports terminating the model. For the recovery model, it can be used very little because of its tight coupling level.
In an exception handler, the Throwable class declaration (exception, which inherits from this class) is the Printstacktrace () method, which outputs the information to the standard error stream by default, or it can change the output stream by adding parameters. such as: E.printstacktrace (System.out); The information is output to the standard output stream.
Exceptions and Logging
It's a good habit to keep a log of the program at all times. In Java, you can use the static Logger.getlogger () method to get the logged object.
To re-throw an exception
A re-throw exception throws an exception to the exception handler in the upper-level environment, and subsequent catch clauses of the same try block are ignored. If you simply re-throw the current exception object, the Printstacktrace () method shows the call stack information of the original exception throw point , not the information of the re-throw point. To update this information, you can call the Fillinstacktrace () method, which returns a Throwable object. Such as:
Throw (Exception) e.fillinstacktrace ();
It is possible to throw another exception after an exception capture. The effect of doing so is similar to using Fillinstacktrace (). Information about the point at which the original exception occurred is lost. The rest is the new throw point information.
Exception Chain
Throws another exception after catching an exception, and wants to save the information of the original exception, which is called the exception chain. All Throwable subclasses can accept a cause object as a parameter in the constructor. This cause is used to represent the original exception, so that the original exception can be passed to the new exception, and even if a new exception is created and thrown in the current location, the exception chain can be traced to the original occurrence of the exception.
Of the Throwable subclasses, only three basic exception classes provide constructors with cause parameters, they are error (Java Virtual machine Reporting system error), Exception, and RuntimeException. If you want to link other types of exceptions, you should use the Initcause () method instead of the constructor.
Java Standard Exceptions
Throwable class objects can be classified into two types: error is used to represent compile-time and system errors, exception is a basic type that can be thrown, and can be thrown exception in Java class Library/user methods and runtime failures. So the base type of our relationship is usually exception.
It is important to remember that exceptions of type runtimeexception (and its subclasses) can only be ignored in code, and the handling of other types of exceptions is enforced by the compiler. The reason for this is that RuntimeException is a programming error:
- Unexpected error. For example, introduce a null reference within your control.
- The error that should be checked in the code. Array out-of-bounds access. 、
Deficiency: Abnormal loss
As shown in the following example:
public void Main (String [] args) {try{throw new RuntimeException ();} Finally{return;}}
This method drowns out the exception directly through finally. Cause an exception to be lost.
Limitations of exceptions
When overriding a method, only those exceptions listed in the exception description of the base class method can be thrown. This means that when the code used by the base class is applied to its derived class object, it works as it would .
the exception limit has no effect on the constructor . The subclass constructor can throw any exception, regardless of the exception thrown by the base class constructor. However, the accumulated constructor must be called (automatically called) in such a way that the derived class constructor must contain an exception description for the base class constructor .
the derived class constructor cannot catch exceptions thrown by the base class constructor .
Although the compiler enforces the exception description during inheritance, the exception description itself is not part of the method type. A method type consists of the name of the method and the type of the parameter. Therefore, the method cannot be overloaded based on the exception description.
[Think in Java] 12th by exception handling error