1. Abnormal
An exception is a flow of instruction that causes the program to run, and if the exception is not handled correctly, it can cause the program to break execution and cause unnecessary loss.
2. Handling Exceptions
try{
Exception statement
}catch (Exception e) {
}finally{
Code that is bound to execute
}
If you know the type of the exception can be written directly in the catch, for example: ArithmeticException E
You can capture a multiple catch statement after the try.
3. Common anomalies
A, nullpointerexception null pointer exception
B, arithmeticexception arithmetic anomalies
C, arrayindexoutofboundsexception array out of bounds exception
D, numberformatexception numeric format exception
4. Throws key Words
A, when defining a method, you can use the throws keyword declaration, using the method declared by throws to indicate that this method does not handle the exception, and throws it to the caller of the method.
The format is:
public void Tell () throws exception{}
Example:
If an exception occurs, the function that calls the method catches the exception with the corresponding try catch operation.
If there is no action to catch the exception, an error may occur.
5. Throw keyword
The Throw keyword throws an exception that directly throws an instantiated object of the exception class.
1 Try {2 Throw New Exception ("Instantiate exception object"); 3 } Catch (Exception e) {4 System.out.println (e); 5 }6 }
6. Custom Exceptions
A, custom exception directly inherit exception can complete the custom exception class
1 classMyExceptionextendsexception{2 Publicmyexception (String msg) {3 4 Super(msg);//constructor method for calling exception5 }6 }7 Public classHello {8 9 Public Static voidMain (string[] args) {Ten One Try{ A Throw NewMyException ("Custom Exception"); -}Catch(MyException e) { - System.out.println (e); the } - } -}
Java Exception Handling