The main points to tell:
I. Introduction to Anomalies
Second, exception processing flow
Iii. run-time exceptions and non-run-time exceptions
Iv. Throws and Throw keywords
First, Exception introduction
Exception handling is a situation that occurs when a program is running, such as a divisor of zero. The parent class of the exception Class (Exception) is the Throwable class, with two subclasses under the Throwable class, error, Exception. The difference between the two is:
1, Exception : This refers to an exception that is run by the program and can be processed by the user.
2. Error : This refers to the JVM error, the program is not executing and the user cannot process it.
All exception-handling classes are exception classes or their subclasses, as an example:
Java.lang.Object | -java.lang.Throwable |-Java.lang. Exception | -java.lang.RuntimeException |-Java.lang. ArithmeticExceptionjava.lang.Object |- java.lang.Throwable|-Java.lang. Exception | -java.lang.RuntimeException |- java.lang.IllegalArgumentException|-java.lang. NumberFormatException
Second, exception processing flow
Exception Flow: An exception class object that is automatically instantiated by the JVM when an exception occurs when the ⑴ program does not require the user to manually new. ⑵ to determine whether abnormal capture operation exists in the procedure, and to monitor the program that may appear abnormal. If it does not exist, it will be sent to the JVM for exception information output before the program ends. ⑶ If there is an exception capture operation, the exception type of the catch after the try statement is matched, and this exception is handled if there is a matching catch exception type. Otherwise, continue to match each catch. ⑷ executes the code into the finally, regardless of whether the exception was successfully captured. When the code in finally is finished, you need to determine whether the exception has been processed. If the exception has already been handled, the other program is executed until the program ends. If the exception is not handled, the exception information is sent to the JVM for output, and then the program ends. Attention:
The ⑵exception class is the topmost level of any exception class and can match all exceptions, but all exceptions will be output in one way and cannot be distinguished.
Iii. run-time exceptions and non-run-time exceptions
Exception This exception is divided into two major classes of runtime exceptions and non-runtime exceptions (compilation exceptions). These exceptions should be handled as much as possible in the program.
the RuntimeException class exception is a class specifically designed for Java to facilitate code writing. When the RuntimeException class method is thrown using the throws definition, the program does not force the capture to handle the exception, which can be selected by the user and, if not processed, by default by the JVM. common RuntimeException Exceptions: ArithmeticException , nullpointerexception , classcastexception, numberformatexception Example:
Java.lang.Object | -java.lang.Throwable |- java.lang.Exception|-Java.lang. RuntimeException Run-time exception |-java.lang.IllegalArgumentException |-java.lang. NumberFormatException
Non-runtime exception (compile exception): is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, generally do not customize check exceptions.
Iv. Throws and Throw keywords
Packageexceptiontest;classmymath{//The throws definition throws an exception that appears on the defined method, and if an exception is handled by the caller, the exception Public Static intDivintXintYthrowsException {intresult = 0; Try{System.out.println ("*******1, Data start processing *********"); Result= x/y; } Catch(Exception e) {//The throw keyword throws an exception in the method, or it can be omitted. Throwe; } finally{//finally is the end of the program, which executes the code regardless of whether an exception is thrownSystem.out.println ("*******2, end of data processing *********"); } returnresult; }} Public classExceptiontest { Public Static voidMain (string[] args) {//calling the div () method must catch its exception Try{System.out.println (Mymath.div (2, 2)); } Catch(Exception e) {e.printstacktrace (); } }}
Throws and throw keyword differences
1, throws definition throws an exception, used on the defined method, indicates that an exception was handled by the caller, and the corresponding call must set the exception thrown by the capture throws definition.
2. Throw throws an exception, used in the method, to indicate that there can be catch capture.
Reference Learning Connection: http://blog.csdn.net/hguisu/article/details/6155636 in-depth understanding of Java exception handling mechanisms
JAVA exception handling mechanism