1. Difference between error and exception
2. Difference Between exception and runtimeexception
3. Try {...} catch (...) {...} finally {...} execution order
Public Class X {
Public static void main (string [] ARGs ){
// Error and exception have the same parent class throwable
// "Error is a subclass of throwable. Its appearance indicates a serious problem. General applicationsProgramNo error should be caught unless there is a reason. This is usually a very unusual situation"
// Although error can be captured, programmers are generally not recommended to do this.
Throwerror (); // No processing is performed.
Try {
Throwerror ();
} Catch (error ex) {// to capture, it must be an error and cannot be exception
System. Out. Print ("B ");
} Finally {
System. Out. Print ("C ");
}
// Exception is also a subclass of throwable. It is divided into runtime exceptions and checked exceptions by checked | unchecked. Both exceptions are subclasses of exception.
// Checked exception, which must be handled by the programmer. It must be either thrown to the previous layer (declared in the throws Statement defined by the method) or handled by try {} catch () {}.
// If no exception is processed, the compilation fails.
// Throwexception (); // compilation Error
Try {
Throwexception ();
} Catch (exception e ){
//
} Finally {
//
}
// Runtime exception runtimeexception is unchecked, common except 0 exception, null pointer exception
Throwruntimeexception ();
}
Public static void throwerror (){
Throw new error ();
}
Public static void throwexception () throws exception {
Throw new exception ();
}
Public static void throwruntimeexception (){
Throw new runtimeexception ();
}
}