abnormal
Throwable
Error
- Definition:
malignant irreparable error, only by modifying the code to avoid errors, the system level
Example: define an array int [] arr = new int [ 1024 * 1024 * 1024];
case reported memory overflow exception java.lang. OutOfMemoryError, this anomaly associated with the system, if you want to avoid this exception, the source code can only be modified by the way
Exception
- Definition:
repairable benign anomaly, the programmer can be corrected by way of correction codes (try catch process, without modifying the source code), so that the normal operation of the program
When checked exception: exception when compiling
- Inherited exception class, an exception is thrown and then compile time, you need to try catch handling, processing or throw, has been an exception will be thrown up into the main method, referred to jvm virtual machine processing
Runtime exceptions: runtime anomalies
- RunTimeException class, Exception subclass, not throw compile time, runtime exception is thrown
Abnormal keywords
throw
- In the method body thrown
if the exception was thrown method body compile abnormal, abnormality need to try a throw {} catch () {} treatment, or treatment in the method declaration throws
throws
- In the method throws an exception statement
may be a plurality of exception is thrown, with between abnormal, separated by commas, abnormalities parent child relationship method bodies throw the thrown exception to the parent
try{} catch{} finally{}
try {}
is placed statement exception may occur- catch (Exception for receiving object) {}
- the catch (Eeception E) {
// perform abnormality occurrence process, typically do logging / print exception information / operation continuously thrown
} - the catch () {} treatment principle:
statement first try abnormality may occur thrown exception object, using the received Exception try thrown exception object, and {}, make the appropriate treatment
- the catch (Eeception E) {
finally {}
statement will be executed, the general operation of resources for closing, if the finally {}, return place, then the method returns the return value is always finally
3 ways exception trapping
Three kinds of exception trapping
plurality 1. exception, were treated
the try {
} the catch () {
}
the try {
} the catch () {
}
the try {
} the catch () {
}
an exception, try catch corresponds to a
plurality of abnormal 2 , once captured, a plurality of processing
try {
} catch () {
} catch () {
}
catch () {
}
a try to capture a plurality of abnormality, a plurality of multi-catch should be
noted: when the try that may be thrown while the child comprising abnormal parent class, the class symbol sub catch attention abnormal positional relationship
when the catch of a subclass should be placed above the parent abnormal
reasons: when the exception occurs in the try parent class can not be used to receive the objects of a subclass
when abnormality occurs try the subdivision It is the parent class object may be used to receive a
multi-state, only the subclass object upcast parent object is assigned subclass object)3. A plurality of abnormality, once captured, one process
the try {
} the catch () {
}
the try that may be thrown in a common parent abnormality
Jvm virtual machine to detect abnormal movements
- Jvm common virtual machine vivo method for detecting an abnormality
- Creating an exception object exception object content (Exception information: namely abnormal package name, class name, cause of the abnormality, abnormal position)
- If the method throws an exception in there try catch statement, try catch statement is executed, if unusual statement throws, the exception object is created in the method of shifting the method invocation, a try catch process, if there is no deal with the try catch up the exception object thrown up, thrown until the main method, referred to Jvm virtual machine processing
- Jvm virtual machine receives the abnormality in the main method
- The exception object is printed to the console in red
- Stop program execution
Abnormal child the parent class inheritance
- Subclass and superclass can only throw the same exception or abnormal subclass inherits the parent class or when not throw an exception
that is: when a subclass to inherit a class that has thrown the subclass can only do the following several actions
- Throw the same exception and the parent class
- Thrown parent subclass
- Nothrow
- Other non-parent can not throw an exception class
that is either not throw, throw only throw parent or parent abnormal abnormalities subclasses
Abnormal use
When program development, when the user of the method are generally required to pass parameters define a parameter error, the user determines the parameters passed for compliance, is empty
The static method using Object RequireNonNull (), the method determines whether the parameter object is null
Object.RequireNonNull (Object) // If the object is empty, throw a NullPointException ()
底层:判断对象是否为空,若为空,抛出一个NIullPointException,不为空,返回一个对象
Overloaded methods: RequireNonNull (obj, abnormal reasons) // If the object is empty, throw a NullPointException (Abnormal)
Throwable Method 3
String getMesage () // Print Abnormal
String toString () // Print abnormal package name. Abnormal class name +
catch中捕获的异常对象 Exception e 打印 e时调用的就是 toString()方法
String prinitStackTrance () // Print abnormal package name. Abnormal class name + + abnormal position
控制台打印调用的就是printStackTrance()方法
Custom exception class
- step
- Exception realize
that exception has thrown nature, nature can throw Throwable is unique feature of this system, this is the only system of class in order to be throw and throws Abnormality information defines
two constructors:
a null constructor
XxxException () {
Super ();
}
a constructor parameter with abnormality information
XxxException (Message) {
// display used in the constructor Supper (abnormality information ), abnormality information is transmitted into the
Super (message)
}Use
by throw thrown in the statement may appear abnormal in the possible exceptions
exception thrown when compiling abnormalities:
abnormal statements need may arise to try and catch methods of treatment, or a statement of the method throws an exception
when thrown the abnormal is abnormal run-time:
when not special treatment, procedure calls, Jvm virtual machines automatically identify- Custom compile-time anomaly
/ **Custom exception class
* /
public class ExceptionTest01 the extends Exception {
Private int value;
public ExceptionTest01 () {
Super ();
}
public ExceptionTest01 (String msg, int value) {
// custom exception information passed to the parent class msg constructor
Super (MSG);
this.value = value;
}
public int the getValue () {
return value;
}
}
public class ExceptionTest02 {
public static void main (String [] args) throws ExceptionTest01 {
int I = div (. 1, - 1);}
/**- Custom compile-time exception in the statement throw, throws in the method must be declared
- @param the
- @param b
- @return
ExceptionTest01 @throws
* /
public static int div (A int, int B) {throws ExceptionTest01
IF (B <0) {
throw new new ExceptionTest01 ( "appears divisor is a negative number", b); // throw keyword by manual throw a custom exception object.
}
Return A / B;
}
abnormal run custom
public class NoScoreException the extends a RuntimeException {
// null argument constructor
public NoScoreException () {
Super ();
}
// have a parameters configured
public NoScoreException (String Message) {
Super (Message);
}
}
public class Student {
Private String name;
Private int Score;
// null argument constructor
public Student () {
Super ();
}
// C) provided with a reference configuration;
// i setXxx method used to name and the score assignment.
Public Student (String name, int score) {
the setName (name);
setScore (score);
}
// D) providing setter and getter methodspublic String getName() {
return name;
}public void setName(String name) {
this.name = name;
}int getScore public () {
return score;
}
. // I in setScore (int score) method
public void setScore (int score) {
// 1. First, determine if the score is negative, it is thrown NoScoreException, exception information : fraction can not be negative: XXX.
IF (score <0) {
the throw new new NoScoreException ( ": No negative score:" + score);
}
. 2. // score is then assigned to the members
this.score = score;
}
}
- Custom compile-time anomaly
- Exception realize