Java exception handling

I. Introduction abnormal

FIG structural abnormalities

Throwable is the class, Exception and Error have inherited this class, because the system error is an internal error or resource exhaustion error, this rarely occurs, but also not allowed to be thrown in error Exception exception is divided into RuntimeException (Run an abnormal) and IOException (other abnormalities), divided into two exceptions rule is: as part of a program error caused an exception RuntimeException (runtime exception), such as the wrong type conversion, array bounds, access to this type of null pointer, and like I / O errors such problems caused by abnormal belonging to other anomalies;
in Java, the error class and the class RuntimeException exceptions referred to unchecked (non-checked exception) all the other exceptions referred checked (checked exception); checked exception is an exception must be dealt with, if not addressed, the compiler will not let through
non-checked exception compile time no problem, you can not deal with, the virtual address these issues, the problem is usually at run time, these abnormal usually because the array bounds, access null pointers to these issues
when throwing an exception can be thrown Throwable, and It must be a catch handler at the time of the call. But this design method to do well, because they do not know the type of throw in the end what kind of specific issues, can not be targeted treatment

Second, the two ways of exception handling

2.1 throw

One way to tell the compiler will not only need to return to what value, but also to tell what the compiler error may occur, the method should be in his first statement of the Ministry so that may be thrown, so you can probably throw those checked exception from this method reflects the header ;
difference throws and throw the

  • throws
    a method declaration in the back, with the exception class name that
    can talk to more than one exception class names, separated by commas
    represent an exception is thrown by the caller of the method to deal
    throws a possibility of abnormal said they did not these exceptions must occur
  • throw
    used in vivo, with the exception object name that
    can only throw an exception object name
    represents an exception is thrown, the statements in vivo treatment
    throw an exception is thrown, throw it must perform some kind of an exception thrown
  • throws an exception Note:
    when subclasses override the parent class method, the subclass method must throw the same exception or abnormal parent subclasses.
    If the parent class throws more exceptions, when subclasses override the parent class, can only throw the same exception or is he a subset of the parent class subclass can not throw no exceptions
    of any kind does not throw an exception if the parent , then the subclass can not throw any exceptions, if there is a sub-class method exception occurs, then the subclass can only try, not throws

In exception handling, you should know how to capture those exceptions process, but will those who do not know how to deal with exceptions thrown in;

2.2 try...catch

  • try .... catch treatment program

    Each has an abnormal write a try ... catch .. write a try, multiple catch

   try{
               ...
          }
        catch(异常类名 变量名) {
                ...
              }
        catch(异常类名 变量名) {
               ...
            }

  • try ... manner exception handling:

    Having identified the problem in a try inside, jvm will help us generate an exception object, then the object and catch the exception inside the anomaly match. If the exception object is an abnormal type of information processing will be executed inside the catch. The emergence of a new exception handling scheme after jdk7.0

             try{
                ...
             }
            catch(异常名1 | 异常名2 | ...  变量 ) {
                ...
           }

    The advantage of this approach is that the code is simple, it is also apparent lack of this method for different exception processing method is the same, but if if encountered in the development of the same type of exception, and have the same the processing method, then you can use this exception processing method, and this feature is used only subclass relationship exists between the exception that the relationship is not 2 parent class and subclass of an abnormal and abnormality.

  • finally clause:

    Finally clause handling mechanism: after performing a try-catch statement, a finally statement is executed, and whether there try section is not performed, even if the return statement is executed try-catch statement will be executed in the finally statement, One situation is the exception, if the system is performed in the try-catch statement exit code, i.e. system.exit (0), then finally will not be executed, but all of the statements below.

Third, custom exception class

In the program, problems sometimes encountered anomalies can not be described and treated with a number of standard class, this time we can define a custom exception class, custom exception class inherits Exception, or inherit a subclass of Exception, custom to give a specific exception class exception handling operation, general custom exception class has two constructors, a constructor with no arguments is, an abnormality information is configured with the detailed description, the following is a custom exception class when there has been some unusual when you can throw a custom exception class

Guess you like

Origin www.cnblogs.com/blackmlik/p/12077638.html