day19 Java learning (day19 (abnormal & IO (File class)))

 Abnormalities (abnormal overview and classification)

   Overview: An error occurred during the operation of emerging Java.

   classification:

            * Throwable 

                * Error (server downtime, database crash ...)

                * Exception     

   Abnormal inheritance system:

            * Throwable 

                * Error 

                * Exception  

                      * RuntimeException (a runtime exception)

 

Abnormal (try ... catch way to handle exceptions)

 try: for detecting an abnormality.

 catch: to capture the exception.

 finally: release of resources

 Exception handling of two ways:   

               1: try...catch...finally

                            * try   catch     

                             * try   catch  finally               

                             * try   finally   

                 2: throws

 try ... catch to handle basic format of the exception:

                          * try...catch...finally       

Abnormal (abnormal manner of throws)

     throws a way to handle exceptions:

                           * When you define a function method, we need to expose a problem processing allows the caller to deal with.

                           * Then identified on the method throws.

     

public  void the setAge ( int Age) throws Exception { // throws identification 
        IF (Age <0 ) {
             // throw an exception 
            the throw  new new Exception ( "Age of not less than 0" ); 
        } the else  IF (Age == 0 ) {
             the throw  new new Exception ( "Age is not equal to 0" ); 
        } the else {
             the this .age = Age; 
        } 
        
    }
example

 

Anomaly (the difference between throw and throws an overview of)

    throw Overview: internal function method appears in some cases, the program can not continue to run, jump when needed, use throw the exception object thrown.

    

    throw and throws difference:

       throw:

                  * Used in vivo, with the exception object name it is.

                  * Can only throw an exception object name.                 

                  * Indicates an exception is thrown, there are ways the body of the statement processing.            

       throws:

                  * Use after the method declaration, with the class name is unusual.

                  * You can throw multiple exceptions class names, separated by commas.                 

                  * Indicates an exception is thrown, there are ways of handling caller.     

Abnormal (features and functions finally keyword)

    finally it features:

                   * Controlled body finally statement will be executed.

                   * Special circumstances: Before performing to finally JVM exited (such as System.exit (0): Exit JVM virtual machine).

    finally the role:

                   * For releasing resources. (Try statement is a statement in the body, can not be used alone)

    Note: Do not write the statement finally returned inside. (If written inside a return statement, then try and catch results will be changed)

 

 

Abnormalities (abnormal compile-time and run difference abnormality)

    Java exceptions are divided into two categories: abnormal compile and run-time exceptions.

    All instances of the class RuntimeException and its subclasses are called abnormal, the other is abnormal compile a runtime exception.

    Compile-time exception:

              * Java program must display processing, otherwise the program error occurs, will not compile. (Such as file not found)

     Runtime exception:

              * No display processing, and can be compiled as an exception handling.

 

Abnormal (common method of Throwable)

               * GetMessage (): Gets exceptions, which returns the string

                * ToString (): Gets exception classes and exceptions, which returns the string

                * PrintStackTrace (): Gets exception classes and exception information, as well as locations of the anomalies in the program, the return value void.

 

Exception (custom exception overview and basic use)

    Custom anomaly Overview:

                 * Inherited from Exception 

                 * Inherited from RuntimeException

 

Abnormalities (abnormal precautions and how to use exception handling)

        Abnormal Notes:

                 * When subclass overrides the superclass method, a method must throw the same subclass of a subclass or parent class.

                 * If the parent class throws more exceptions, when subclasses override the parent class, can only throw the same exception or is it a subset of the parent class subclass can not throw no exceptions.

                 * If the method is overridden no exception is thrown, then the subclass method must not throw an exception. If an exception is thrown, then the subclass can only try, not throws within the sub-category method.

        

        How to use exception handling:

                 * Principle: if the internal processing function can be a problem, with the try, if not treated, the caller referred to treatment, this time with throws.

                 * the difference: 

                             * Follow-up procedures need to continue to try to run

                              * Follow-up program does not continue to run on throws

                 * The JDK is not provided through a corresponding exception, require custom exception.                         

 

finally

Guess you like

Origin www.cnblogs.com/feng0001/p/10947397.html