Classification and treatment of abnormal Exception

First, the exception:  
1: incorrect classification
          1) Error: error (uncontrollable), generally refers to abnormal systems, such as blue screen, memory overflow, jvm operating environment there is a problem.
          2) Exception: exception is controllable, programmers can write programs to solve the error.   
          3) both the parent class is the Throwable;
2: control the exception:
          Exception: is the parent class for all exceptions, the following can be derived which many subtypes.
3: exception handling mechanism.
      When programming, a piece of code may be some exceptions. We should for this
           1) exception handling. How to deal with it? 
               The syntax is as follows: - Solution
                try {
                    capture snippet abnormality information appears, and then packaged as an exception object // may
                    now exception try {} block placed in,
                 } the catch (Exception type declarations - references to capture the exception object) {
                   // handle exception
               } the finally {
                             / * try whether abnormality occurs in this part of the code will be executed.
                              It is generally used for closing and releasing operations.
                                try finally occurred in execution will return statements
                             * /
               } 
                2) in the process: We will block exception may occur into the try {},
                          ①jvm will try {} checks the code,
                                    if there is an abnormality, jvm will exception information into the corresponding exception object,
                                    and then transferred to the catch () {} is processed code block, execute business logic in {}.
                                    Finally finally executes the code module.
                          Try {} ② If no abnormality occurs, and finally the finally also the logic module. 
 
         . 4: catch block:
                    number ①: may have one or more, 
                    ② the order of execution: top to bottom catch block exception types may be used in the same level;
                        if there are a plurality of parent-child relationship exception types, then abnormal child should first write, write parent exception.
                      (When Father first to write an exception, the compiler does not pass) before processing sub-type of exception, then the parent type handle exceptions.

       5: finally block:
                    ①finally there is no abnormality code block are executed, the final implementation.
                    ② commonly used to shut down certain operations, removing temporary files;
                    ③ no matter try or not an exception when the program has return keywords, perform return, then execute the finally block
        6: throw: thrown objects.
                    Concept: Sometimes, the capture program exceptions, do not want to process the catch block.
            It will be thrown out, thrown to the caller (who call this method who is the caller)
            to the caller to deal with (need try / catch), main in the best directly handle exceptions.   

        7: throws:
                     When you define a method, you must declare the type of the exception object to be thrown
                      throws: call notification is an abnormal type declaration to be processed, the exception type defined at the method declaration throws.
                      The throw: thrown objects, throw to the caller (method), so that the caller process.
                  
                         If the throw is abnormal, you can not run throws
                         if the throw is a compile-time exceptions, you must use throws
                         throw a new new exception object:
                                         throw new new NullPointerException ();


         8: abnormal Category:
                   ① checked exception (compile-time anomaly)
                            REG: IO abnormal
                   ② non-checked exception (a runtime exception)
                            REG:
                                    NullPointerException // null pointer exception
                                    ArrayIndexOutOfBoundsException // array index bounds exception
                                    StringIndexOutOfBOundsException // string index beyond the scope of cross-border
                                    ClassCastException // class cast exceptions
                                    NumberFormatException // digital abnormal
                                    IllegalArgumentsException // illegal argument exception
                 not to ClassNotFoundException as a runtime exception
           when an exception throw objects at run time, the method does not require a statement of the throws, because the compiler ignores run abnormal.
           When the compiler throw an exception, you must declare this type throws an object at the method.
        9: When the method of the parent class exception statement, declaration subclasses may not override methods may be declared;
               parent class declared method is not abnormal, abnormality not declare subclass;
        10: custom exception. Only need to inherit Exception, a system can provide a constructor.

Second, a subclass inherits the parent class
  subclasses When overriding inherited methods, if the parent class declares exception:
          1) a method subclasses may not declare the exception;
          2) may be declared in the parent class;
          3) exception subtype may declare parent process anomalies;
             * can not declare the parent class for exception supertype
           example: public Double Distance () throws exception {
           // block  
                 }
           4) if no parent class exception statement, sub class methods can not declare an exception

Guess you like

Origin www.cnblogs.com/hkgov/p/11751518.html