The above mainly describes the generation of anomalies, abnormal capture, exception processing flow. Exceptions in a program cannot be avoided, so how to deal with exceptions is especially important.
How to handle exceptions 1, only capture, do not do any processing
When an exception occurs, the above code captures the exception, enters the catch clause after the capture occurs, does not have any code in the Catch clause, and the exception is not processed, so the code after the catch clause executes "as if nothing had happened". Continue execution. In other words, Mode 1 did not respond to the exception, just gave it (swallowed). This is dangerous because subsequent programs can fail, and because of the lack of necessary processing and logging of the above exceptions, it is very difficult to locate errors directly.
2, catch the exception, simple printing
E.printstacktrace () for abnormal situation output, debugging may have some help, but after the commissioning phase, it is not conducive to the maintenance of the program. A more secure and rational way is to output the location of the exception on the screen and the cause of the exception, as well as outputting the exception details to a log file.
Public classExceptiondemo { Public Static voidthrowstest () {Try{class.forname ("Java.util.List"); } Catch(ClassNotFoundException e) {//Exception handling SYSTEM.OUT.PRINTLN ("Class.forName () class Load exception:" +e.getmessage ()); LogFile (NFE); } } Public Static voidMain (string[] args) {throwstest (); }}2, throws abnormal
Throws is used at the declaration of a method to indicate that this method does not handle exceptions, that all exceptions are passed to the used for processing, and that the invocation can continue to submit the exception upwards.
Public class Exceptiondemo { publicstaticvoidthrows classnotfoundexception{ class.forname ("java.util.List"); } Public Static void throws classnotfoundexception { throwstest (); }}
Exceptions are not handled here, and eventually the JVM is handled abnormally, with the exception output in the background.
3. Throw exception
Throw is mainly used in the method body, which means to throw an instantiated object of an exception class, commonly used to throw a custom exception, used in conjunction with throws to leave the exception to the caller for processing.
Remarks: How to customize exceptions
In normal usage situations, it is very rare to define your own exception classes, but it is possible to define many exception classes that belong to this framework if you are learning the framework, or if you are designing the framework later.
The above exception, the final processing form is returned to the processing rules of mode 2. Make the error message simple output, complete information written to the log file.
Summary:
Do not procrastinate or delay the handling of exceptions, try to solve them in situ, the best in- place solution, and need to deal with the real, not just capture, not action.
java--Anomaly (ii)