java in a variety of common abnormalities

A variety of common exceptions

In a program note that if you pay attention, the program throws exceptions are: java.lang.ArithmeticException. This exception is in the package lang already defined. In lang package also defines some of our very common abnormalities in the following table:

These anomalies can remember the above requirements, the program throws an exception, you know what the problem is happening procedure. We can give an example to explain. code show as below:

public  static  void main (String [] args) { 
        Cal CAL = null ; // no new object is 
        the try {
             int Result cal.div = (10, 0); // where it throws a null pointer exception 
            System.out.println ( "results:" result +); // phrase is not performed 
        } the catch (exception E) { 
            e.printStackTrace (); // can print using the exception information printStackTrace method. 
        } 
    }

 

finally block is not necessary, so we did not finally block, we print exception information by abnormal printStackTrace method, so the last console input:

 

 

Second, the abnormal direct inheritance

Class inheritance relationship between the abnormal follows:

The figure we need to explain a few

1, Throwable class has two direct subclasses: Error is a class, a class is Exception. Error class we talked about earlier, and is the java runtime internal error of the system, such as memory overflow, this point does not need our programmers care about, care about there is no way. We need relationships Exception. He said the program is running in error, you can use try ... catch ... capture.

2, system-defined exceptions are mostly inherited RuntimeException, have seen the class name, is a runtime exception, Above us a null pointer exception, divide by zero exceptions, and exceptions are cross-border data its subclasses.

Guess you like

Origin www.cnblogs.com/weibanggang/p/11184694.html