Novice abnormal Started

abnormal

  • Program is not normal execution error. Exception class

  • Abnormalities parent class: Throwable , Throwable another subclass of Error and Exception .

  • Abnormal Category:

  • Throwable : the following error of the parent class.

    • Error : ( Error unusually severe than the general error usually appears as a virtual machine ) , Error type of error can not be resolved must be thrown capture!

    • Exception

      • A RuntimeException ( a runtime exception ): compiler, not mandatory process, appears not runtime → abnormalities.

        • Arithmetic exception, array subscript bounds, null pointer exception,

      • Compile an exception ( except RuntimeException rest are compile an exception ) : You must deal with, either capture or thrown. General syntax error, the error will be subject to inspection during compilation → abnormal . Must promptly deal with!

  • Common exceptions

    • Error: memory overflow error (recursive call between two methods) ;

  • Exception handling

    • Whether error Error or abnormal Exception are to be processed. There are two ways to handle exceptions:

      • Throws throws and capture try catch

  • throws : Usage: after method parameter list after the method declaration, left brace ({) before use throws declaration to be thrown. throws exception classes, support for multiple simultaneous throws an exception, that is, throws exception classes 1, [ exception classes 2] .... If there are multiple methods need to throw an exception, you can use a maximum abnormal parent class declaration ( Exception ) directly throws Exception;

  • Unusual precautions and how to use exception handling:

    • Abnormal way how to use:

      • 1 inside) method aberrant try catch processing: This method can be called normal.

      • 2 there is an exception thrown (on) a method declaration throws ): have to deal with this method when the method is called.

      • If there is a statement on the method throws an exception, it should be noted rewriting

        • Subclasses override the abnormal method when the subclass of exceptions thrown small range than the range of the parent class, or equal to the range, or not abnormal.

        • Role is to: declare the method / kind of exception can be thrown compile or run time. As long as there is abnormal process, whether it is abnormal operation, or compile an exception, where the program will stop, and throws an exception.

  • Exceptions, may not declare an exception can be thrown clear statement run on the method declaration Runtime: Notes. Compile an exception must be handled. Either throw (throws) or capture (the try the catch) .

  • The unusual nature: running exceptions thrown, in fact, is the object of an exception class method declaration.

  • throw

    • In the method body, which can be clearly demonstrated throw an exception. It can be accurately controlled in any place other than throwing an exception.

    • Syntax: throw exception object ( in the form of anonymous objects new exception classes ()) , if the throw exception object is thrown back abnormal, you can declare an exception to be thrown in the method declaration is running, you can not declare. If you throw throw a compile-time exception object, you must force handle the exception ( statement throws or capture ) .

  • throw and throws similarities and differences

  • 1 different) position: the throw in the method body, throws in the method declaration.

  • 2 different) role: the throw after throw statement is in addition to the exception object, throws after the declaration of the class class name is unusual.

  • 3 ) different way of writing: the throw only write an exception, after throws after multiple objects can write the name.

  • try catch

    • Another unusual approach, called catch the exception, in the line items on the extensive use, because the display exception is thrown, the program will stop at the anomaly, it will affect the program functionality, but catching exceptions can be done even if there are abnormal but program will continue to execute. Usually the catch exceptions can be made exception log abnormality, the abnormality central record captured, then subsequent modification.

    • Try catch to handle exceptions allow normal program execution, but try {} code, then if there is an abnormality, the try code after the error is not performed, as shown:

    • grammar:

       

      • try{

        The code may appear abnormal

        } catch ( to capture exception class name object name ) {

        After the capture of dry matter.

        } [catch ( to capture exception classes 2 object name 2) {

          After the capture of dry matter.

          }]

  • If you try exceptions thrown not catct abnormal behind to be captured, it will continue to throw an exception, the program will stop.

  • If you try may throw multiple exceptions, you can write multiple catch simultaneously capture multiple exceptions . But write multiple catch trouble, you can directly capture the biggest exception Exception . Capture Exception after an exception, which can not capture a subclass write, because the last Exception abnormality has substantially all exception capture

publicstaticvoid main(String[] args) {

try {

// the exception codes may appear

System.out.println(1/0);

}catch(ArithmeticException ari){

System.out.println(ari);

. The System OUT .println ( ARI .getMessage ()); // print abnormality information / abnormality information is written to the log

// print an error message

ari .printStackTrace ();

// get the main reason , the framework will often see Casuse , in the Cause frequently used when writing project, an error will be very detailed

Throwable Masg = ari.getCause();

System. out .println ( Mask );

} // a finally {} finally to be executed, you can not write write

}

  • Custom exception

  • 1) Create a custom exception class

  • 2) inherited exception class ( Exception , RuntimeException , Throwable ( the largest class throws an exception, which is below Exception) inheritance which will have the characteristics. For example inherited RuntimeException abnormal characteristics and its subclasses have running.

  • 3) Write belt (String) constructor arguments for the abnormality information through the super (messags) is transmitted to the parent class .

    • With a throw thrown program will directly stop, it is proposed to use try catch the exception, but when catching exceptions due to the custom exception and there is no similar divide is defined as 0 , subscript out of bounds such as automatic method throws an exception, so the need to capture their own exceptions thrown like this:

      In this set method is called, it will capture the following exception

      public void setAge ( int age ) {

      if(age >150) {

      try {

      // If the age is greater than 150 , the following sentence, will throw an exception

      thrownew AgeGT150Exception("");

      // capture AgeGt150Exception exception class

      }catch(AgeGT150Exception ag) {

      // operation after capture

      ag = new AgeGT150Exception("年级大于150");

      System.out.println(ag);

      }

      }elseif(age < 0) {

      try {

      thrownew AgeLT0Exception("");

      }catch(AgeLT0Exception ag) {

      ag = new AgeLT0Exception("年龄小于0");

      System.out.println(ag);

      }

      }else {

      this.age = age;

      }

      }


      //异常类

      publicclassAgeGT150Exceptionextends RuntimeException{

      public AgeGT150Exception(String massge) {

      super(massge);

      }

      }

Guess you like

Origin www.cnblogs.com/interflow/p/11233210.html