Java- acquaintance abnormal

1. Abnormal

1.1 Throwable类:

  1. Throwable class is all exceptions (Exception) and error (Error) superclass.

  2. Throwable class has two sub-categories, namely Exception and Error:
    Exception: exception generated program is running, it can be treated.
    Error: JVM is wrong, but this time the program is not executed, can not be processed, can only try to avoid such errors.

  3. Throwable class common method:
    Throwable (): constructor with no arguments, storing abnormality information and the error is null.
    Throwable (String message): There are construction parameters, error messages and abnormal storage of message.
    String getMessage (): Gets the error or exception Throwable class information currently stored in the object.
    String toString (): returns an exception error or a brief description of the current.
    void printStakeTrace (): to show the causes and consequences of errors or exceptions.

1.2 What is an exception? Why do you want to handle exceptions?

  1. An exception (Exception) is a program of instructions could lead to interrupted program run among
  2. If you want the program after the exception occurs, the program still can be completed normally, then we need to deal with the anomalies

1.3 unusual processing formats:

try {
	//可能发生异常的语句
} catch(异常类型 对应异常类型的对象) {
	//发生对应异常后的异常处理操作
} finally {
	//finally代码块内的代码,不论是否出现异常,都会执行
}

1.4 Exception Handling Process

  1. If the program which abnormality occurs, it automatically by the JVM according to the type of exception, an object instance of the class corresponding to the exception
  2. If this time program which do not have any exception handling operation, then instantiate an object of this class will be an exception to the JVM the default handler, and the default JVM's approach is to print exception information, then interrupt program execution
  3. If the program exists among the exception handling mechanism, for example, try-catch statement, it will catch an exception class objects generated by the try statement
  4. After capture the exception class object, with each catch statement after the try to match, if the matching succeeds, then the corresponding catch is processed, if no match is successful, the sequence continues to match the back of the catch, without any catch match is successful, this will be time then to the JVM default handling
  5. Regardless of whether there is abnormal finally statement, execution will be back, if not unusual at this time, after completing execution finally, will continue with the rest of the code, but this time if there is an exception, not been able to deal with, that is no corresponding catch match It will finally statement before execution, but after the execution finally, will be forwarded to the JVM the default handler, the exception information output, and interrupt program execution

1.5 Summary of catching exceptions

  1. an abnormality occurs in the try block from the start position, other code after the code block will not be executed
  2. A plurality of code blocks may have abnormalities, multiple catch blocks may be used for processing different categories
  3. After completion of exception trap handler can continue to run

1.6 exception thrown

throws Keyword:

  1. throws keyword is mainly used in the method definition, this method does not indicate that abnormal processing, to be invoked at processing
  2. At the user level call throws keyword modified method, be sure to use exception handling exception handling operation, which is mandatory process, you can use keyword throws an exception thrown in the main method, but the difference is, this time is represented thrown up, that is, to deal with the default JVM

throw Keywords:

  1. throw keyword is mainly used within the method, before all exceptions are instantiated automatically by operation of the JVM, use thorw keywords you can take the initiative of throwing an exception class object you want

About 1.7 RuntimeException

  1. Clearly defined in Java, for RuntimeException exception type can have a choice to deal with, if not processed, the processing to the default JVM
  2. Common RuntimeException exception:
  • NumberFormatException
  • ClassCastException
  • NullPointException
  • ArithmeticException
  • ArraysIndexOutOfBoundsException

Please explain the difference between Exception and RuntimeException:

1.RuntimeException is a subclass of Exception
2. The mandatory exception is processed Exception defined, and defined RuntimeException exception handling can be selective, if not treated to the default handling JVM

Released five original articles · won praise 5 · Views 210

Guess you like

Origin blog.csdn.net/cccccv_/article/details/104468217
Recommended