On the principle of exception handling JAVA

First, the unusual classification

1, Throwable java.lang package is a special class to handle exceptions. It has two subclasses, i.e., Error and Exception, which are used to process two exceptions.

2, Throwable class is the class of the entire system abnormalities "parent class", of course, ultimately traced in the end of course, be attributed to the parent class Object class.

Throwable class implements the Serializable interface, represents the Throwable can be serialized, inherited from the Object class, his subclass mainly Error and Exception class also has a StackRecorder class .

3, Exception is a major subclass of Throwable. Exception includes sub-classes, some of which correspond to the subclass exception of running Java program, the other part is compiled abnormal (red packet).

4, common runtime exception:

  1. NullPointerException - a null pointer reference exception
  2. ClassCastException - type cast exception.
  3. IllegalArgumentException - passing illegal argument exception.
  4. ArithmeticException - arithmetic anomaly
  5. ArrayStoreException - stored to the array type is not compatible with the declared target abnormal
  6. IndexOutOfBoundsException - subscript bounds exception
  7. NegativeArraySizeException - Create an array of size erroneous negative anomalies
  8. NumberFormatException - abnormal digital format
  9. SecurityException - Security Exception
  10. UnsupportedOperationException - unsupported operating anomalies

Two, java handling of the exception

If a problem occurs, java based exception classes described problems, creates an object (instance), then the object that is the problem of shifting on a specific step of:

method at a specific abnormality -> main master method -> Virtual Machine JVM -> the location and cause of abnormality is printed on the console.

Third, the exception handling

1. The problem can handle myself out

try-catch

try{

Code // exception may occur

} Catch (// to arrest abnormal) {

// exception handling mode

}

PS:return

Conclusion, try, catch, finally block the return of the priority from low to high, try to execute the statement in return before, if you encounter an exception, the code prior to the return catch statement is executed, the last execution finally block, finally If there is a statement block in return, then the program will return in advance, if not, catch block return statement is returned, if no exception is encountered, then executed directly in the statement block finally, see if there return finally statement block We decided to return results.

Reference: https: //www.cnblogs.com/mfrank/p/7895660.html

2. The question he can not deal with

throws

If their definition of a person class, member variables are age, int type, set the required age must otherwise it will throw new Exception ( "your age incorrectly, please re-enter") between 0-150, and on the class mark throws Exception, inform the caller;

When another class Person p = new Person (); p.setAge (-10) will be reported abnormal compile time, need to continue to throw up. (The PS; throw new RunTimeException if red is not reported, see below)

throw: the exception object is thrown to the caller

throws: just a statement of the method, there is an exception to inform the caller of this method.

3. custom exception:

It is to a standard exception classes, based on exception class name can analyze the causes of the problem lies;,

Find a class that inherits Exception, or RuntimeException.

example:

person class

The caller class

Normal print information; (can not see the cause of the problem)

Custom exception class

Print custom exception exception:

Guess you like

Origin www.cnblogs.com/fengtangjiang/p/11105007.html