1. JAVA Definition of exception in
abnormal (Exception) : It refers to the error in the running of the program , for example ： In Division , If the divisor is 0, Then run JAVA It will automatically throw arithmetic exception .
JAVA abnormal ： It is also represented by an object , An exception thrown by a program at run time , It's actually an exception object , This object not only encapsulates the error information , There are also some ways to deal with it .
2. Abnormal system
Abnormal root class ：
stay Java in , All anomalies and errors are born of java.lang.Throwable This class . Only if the object is an instance of this class and its subclasses , Can pass Java Virtual machine or Java throw Statement throw . Similarly , Only this class or one of its subclasses can be catch Parameter type in clause .
java.lang.Throwable There are two subclasses ：java.lang.Error（ error ） And java.lang.Exception（ abnormal ）, The usual term "abnormal" refers to java.lang.Exception.
Throwable -- Error |_ Exception -- ClassNotFoundException |_ IoException -- FileNotFoundException |_ RuntimeException -- ArithmeticException |_ ArrayIndexOutOfBoundsException |_ ClassCastException |_ NullPointerException |_ ......
The difference between an exception and a mistake is ：
Exceptions can be handled by the program itself , The mistake is that you can't handle .
Throwable yes Java Superclass of all errors or exceptions in a language . Contains a snapshot of the thread execution stack when its thread is created , It provides printStackTrace() And other interfaces are used to obtain stack trace data and other information .
Throwable Commonly used API：
public void printStackTrace() // Print exception details . Contains the type of exception , The cause of the abnormality , It also includes the location of the exception . public void printStackTrace(PrintStream s)： This method is usually used to output the exception content to the log file , In order to see , Similar to redirection . public String getMessage() // Get the cause of the exception , Return string . public String toString() // Get exception type and exception description information , Return string .
3. List of common exception classes
|Exception class name||Meaning of exception class|
|ArithmeticException||Arithmetic exception class|
|ArraylndexOutOfBoundsException||Array subscript out of bounds exception|
|ArrayStoreException||Exception thrown when assigning values incompatible with array types to array elements|
|ClassCastException||Type cast exception|
|ClassNotFoundException||No corresponding exception class found|
|EOFException||End of file exception|
|FileNotFoundException||File found no exception|
|illegalAccessException||Exception thrown when access to a class is denied|
|IndexOutOfBounchsException||An exception is thrown when the index of an object is out of range|
|InstantiationException||Try to pass new Instance() Method to create an instance of an abstract class or interface|
|IOException||I / O is abnormal|
|NegativeArraySizeException||Array negative subscript exception|
|NoSuchFileException||Exception not found for field|
|NoSuchMethodException||Method found no exception|
|NullPointException||Null pointer exception|
|NumberFormatException||String to number exception|
|OutOfMemoryException||The memory allocated to the new object is too small|
|ProtocolException||There is an error in the network protocol|
|SecurityException||Applet An attempt was made to perform an action not allowed by the browser's security settings|
|StringIndexOutBounchsException||The program attempted to access a character position in a string where the character does not exist|
|SockedException||Can't complete normally Socked operation|
|SQLException||Operation database exception|
Be careful ： If you don't know what kind of exception code throws , You can specify their parent class Throwable and Exception.
4. Exception handling mechanism
Throw an exception
JVM Default exception handling ： Print error messages on the console , And terminate the program
5. Exception handling process ：
- If there is an exception in the program , Then it will be automatically controlled by JVM Depending on the type of exception , Instantiate an object that specifies the exception class ;
- If there is no exception handling operation in the program at this time , Then the instantiated object of this exception class will be given to JVM To deal with , and JVM The default processing method is to output exception information , Then interrupt the program execution
- If there is exception handling in the program , It will be try Statement to capture the resulting exception class object
- And try Every one after catch Match , If the match is successful , Then use the specified catch To deal with , If no match succeeds , And then to the back catch Continue matching , If there is nothing catch The match is successful , At this time, it will be handed over to JVM Perform default processing
- It will be executed whether there is an exception or not finally Program , If there is no exception at this time , After execution finally, Will continue to execute other code in the program , If there is an exception at this time, it can not be handled （ None of them catch You can meet ）, Then it will be carried out as well finally, But after the execution finally after , Give the default to JVM Output abnormal information , And the program interrupts
5. Exception handling in development ：
try....catch(finally): Capture , Do it yourself
throws： Throw out , Leave it to the caller
Custom exception classes
author ： Happy company