exception (abnormal)

  throw keyword
    role:
        You can use the keyword throw throw method specified in the exceptions specified
    using the format:
        throw new new xxxException ( "the cause abnormal production of");
    Note:
        1.throw keywords must be written in the internal method
        2. throw new keywords behind the object must be a subclass of exception exception or objects
        3.throw keyword specified exception object is thrown, we must deal with this exception object
            throw keyword is behind the creation of RuntimeException RuntimeException or a subclass object we can not deal with the default JVM to handle (print exception object, interrupt the program)
            the throw keyword behind the creation is to compile an exception (error when writing code), we must deal with this exception, or throws, or try .. .catch

 

 throws keyword : The first way of exception handling, to others deal with
    the role:
        When the internal method throws an exception object, then we must deal with this exception object
        can handle the exception using the throws keyword object will declare the exception object thrown to the caller method of treatment (they do not deal with, and give them treatment), and ultimately to the JVM process -> interrupt processing
    using the format: use in a method declaration
        modifier return type method name (parameter list) throws AAAExcepiton, {... BBBExcepiton
            the throw new new AAAExcepiton ( "causes");
            the throw new new BBBExcepiton ( "causes");
            ...
        }
     Note:
        1.throws keyword must write a statement in the method of the
        2.throws keyword behind declared Exception exception must be either a subclass of Exception
        internal method 3. If more than one exception object is thrown, then throws back must also declare more exceptions
            if there are more the exception object thrown parent child relationship, the parent declared directly abnormal class to
        4. call a statement of the method throws an exception, we must deal with the declaration of abnormal
            or continue to use the throw statement throws, to call the method Treatment, and ultimately to the JVM
            Either try ... catch to handle exceptions yourself

 

try ... catch: a second embodiment of the exception handling, exception handling their
    format:
        try {
            possible exception code
        } catch (Exception define a variable for receiving the try thrown exception object) {
            exception handling logic, then abnormal exception object, how to deal with exception object
            generally in operation, will information to a log exception
        }
        ...
        the catch (exception class name variable name) {

        }
    Note:
        1.try may throw a plurality of exception object, it can be used to handle these exceptions catch plurality of objects
        2. If the try abnormality occurs, the exception processing logic will be executed in the catch, the catch completes the processing logic to continue execution try .. after the code of .catch
          the code after an exception if there is no try, then it will not execute the processing logic catch the exception, after executing the code in the try, continue to try ... catch

 

 

 

    finally block
     format:
        try {
            possible exception code
        } catch (Exception define a variable for receiving the try thrown exception object) {
            exception processing logic that, after the abnormal exception object, how to deal with exception object
            generally work in the information recording exception will log a
        }
        ...
        the catch (exception class name variable name) {

        } {the finally
            whether abnormalities are executed
        }
     Note:
        1.finally not be used alone, it must be used with the try
        2 .finally generally used for resource release (recycling), regardless of whether the program is abnormal, the last to be released resources (IO)

 

 

Throwable class defines three exception handling method
                 String getMessage () Returns a short description of this throwable.
                 String toString () Returns a string message of this throwable detail.
                 void printStackTrace () JVM print exception object, by default this method, the exception information is the most comprehensive print

 


** How exception classes: **

1. compile a custom exception: custom class and inherits from `java.lang.Exception`.
2. Customize a runtime exception classes: custom class and inherits from `java.lang.RuntimeException`.

Guess you like

Origin www.cnblogs.com/lee18/p/11524979.html