I. What is an exception?
Java in the run phase of the problem, or error is an exception.
Two. Unusual Inheritance relationships
It can be seen that all exceptions are inherited by Throwable, but in the next layer, they are divided into: Error and exception. Error is mainly in the direction of the JVM, such as insufficient memory, and the request stack depth exceeds the maximum depth. (This is usually the case when you are waiting for death), you only need to consider the exception under exception when designing a Java program.
Three. Write your own exception class
Writing an exception class must inherit from the corresponding exception class, preferably by selecting an exception class that has a similar meaning.
Packagecom.cjm.exception;/*** My first Exception class * *@authorXiao Ming **/ Public classMyfirstexceptiontext { Public Static voidMain (string[] args)throwsmyfirstexception { for(inti = 0; I < 3; i++) { if(i = = 2) { Throw NewMyfirstexception ("You threw out an exception that you wrote yourself.")); } } }}classMyfirstexceptionextendsException { Publicmyfirstexception () {} Publicmyfirstexception (String str) {Super(str); }}
Four. Catching exceptions
Form:
try{
Code that may appear to be abnormal
}catch (exception of type 1) {
Processing mode
}
catch (exception of type 2) {
Processing mode
}
Packagecom.cjm.exception;/*** Use Try catch to catch exceptions * *@authorXiao Ming **/ Public classtrycatchexception { Public Static voidF ()throwsmyfirstexception, mysecoexpection {inti = 1; if(i = = 0) { Throw NewMyfirstexception ("Catch Your first exception"); } Else { Throw NewMysecoexpection ("Catch Your second exception"); } } Public Static voidMain (string[] args) {Try{f (); } Catch(myfirstexception e) {e.printstacktrace (); } Catch(mysecoexpection e) {e.printstacktrace (); } }}classMysecoexpectionextendsException { Publicmysecoexpection () {} Publicmysecoexpection (String str) {Super(str); }}
Five. Log
ImportJava.util.logging.Logger;/*** Log * *@authorXiao Ming **/ Public classmydaily { Public Static voidMain (string[] args) {Try { Throw Newmythrexception (); }Catch(mythrexception e) {System.err.println (e); } }}classMythrexceptionextendsexception{Private StaticLogger logger= Logger.getlogger ("My first log"); Publicmythrexception () {StringWriter trace=NewStringWriter (); Printstacktrace (NewPrintWriter (trace)); Logger.severe (Trace.tostring ()); } }
Six. Finally block
The fianlly statement is bound to be executed, regardless of whether an exception is thrown in the try block.
try{
Code that may appear to be abnormal
}catch (exception of type 1) {
Processing mode
}
catch (exception of type 2) {
Processing mode
}
finally{
Statements that are bound to be executed
}
Example:
Packagecom.cjm.exception;/*** Finally block * *@authorXiao Ming **/ Public classFinally { Public Static voidMain (string[] args) {Try{System.out.println ("My no exception!" "); }Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }finally{System.out.println ("This step is bound to be executed!" "); } }}
Packagecom.cjm.exception;/*** Finally block * *@authorXiao Ming **/ Public classFinally { Public Static voidMain (string[] args) {Try { Throw NewMyfirstexception ("with exception thrown"); }Catch(myfirstexception e) {//Todo:handle ExceptionE.printstacktrace (); }finally{System.out.println ("This step is bound to be executed!" "); } }}
I
Exceptions in Java