The so-called anomaly is the abnormal situation that occurs when the program is running;
1. Abnormal system:
Throwable:
1) Error: usually refers to the JVM has a major problem such as: The running class does not exist or memory overflow, etc., this exception is not handled by the programmer
2) Exception: Some of the conditions that occur at run time can be handled by try,catch,finally. For example, when the division is calculated by dividing the divisor 0, the parameter value is the type does not match.
Java exceptions are actually a description of the anomalies and encapsulate them as objects
Java in the design of the anomaly system, will be prone to exceptions are encapsulated into the object
The following code will divide the divisor by 0 at run time, and Java will throw a corresponding error at runtime
1 Public classDemo {2 Public Static voidMain (string[] args) {3SYSTEM.OUT.PRINTLN ("Begin");4Divide (17/0);5System.out.println ("Ending");6 }7PublcStatic voidDivideintAintb) {8 intC= A/b;9 }Ten}
Errors that are thrown at runtime are as follows
1 begin 2 Exception in thread "main" java.lang.ArithmeticException:/by Zero3at exception. Exceptiomdemo03.divide (exceptiomdemo03.java:10)4at exception. Exceptiomdemo03.main (Exceptiomdemo03.java:6)
Java.lang.Object successor java.lang.Throwable successor java.lang.Exception successor Java.lang.RuntimeException Successor Java.lang.ArithmeticException
The exception type thrown here is ArithmeticException, and the view API indicates that this is a class that inherits from exception.
2. Five key words for handling exceptions
Try, Catch,finally,throw, throws
Try { // Possible exception code catch(Exception class object) { // statement handling the exception type }[finally] { / / code that is bound to execute / / Catch block uses System.exit (1) except }
Try Catch finally
3. Class Throwable
| Method Summary |
Throwable |
fillInStackTrace()
Populated in the exception stack trace. |
Throwable |
getCause()
Returns the cause of this throwable, or returns if the cause is not present or unknown null . |
String |
getLocalizedMessage()
Create a localized description of this throwable. |
String |
getMessage()
Returns a detailed message string for this throwable. |
StackTraceElement[] |
getStackTrace()
Provides programmatic access printStackTrace() to stack trace information by output. |
Throwable |
initCause(Throwable cause)
Initializes the cause of this throwable to the specified value. |
void |
printStackTrace()
Output this throwable and its trace to the standard error stream. |
void |
printStackTrace(PrintStream s)
Outputs this throwable and its traces to the specified output stream. |
void |
printStackTrace(PrintWriter s)
Outputs this throwable and its traces to the specified printwriter. |
void |
setStackTrace(StackTraceElement[] stackTrace)
Sets the getStackTrace() stack trace element that will be returned by and printStackTrace() output by the and related methods. |
String |
toString()
Returns a short description of this throwable. |
The following three methods beginners need to Master
String getMessage () • Gets the exception information that returns the string. String toString () • Gets the exception class name and exception information, returning a string. (Figure for the result of the program call GetMessage () and the ToString () method, you can see that getmessage () returns the information of the exception, and ToString () returns not only the exception information, but also the exception class name); void Printstacktrace () • Print the tracking information of the exception on the stack; (The result returned by Printstacktrace (), which returns the position of the exception 5, 11 rows)
Java Exception Preliminary