Java: checked exceptions and unchecked exceptions

First, the unusual presentation

  Throwable in Java all errors and exceptions superclass. Java Virtual Machine only throw objects belong to this example (or one sub-class), or throw statement can throw the object. Also, catch clause can only be such parameter type (or one of its subclasses). When in abnormal compiled for the purpose of checking, Throwable and some of its subclasses (except RuntimeException, Error, and their subclasses) are considered checked exception (checked exceptions), also known as abnormal operation, and the rest are unchecked exception (unchecked exceptions).

  It creates a throwable contains a snapshot of the thread execution stack. It may also contain a message string that provides more information about the error. Over time, a throwable can inhibit the propagation of other throwables. Finally, throwable may also contain one reason: the other lead throwable throwable configuration. Record this information is called the causal mechanism linking abnormal, because of itself can have a reason, and so on, resulting in abnormal "chain", each anomaly is caused by another reason.

Two, Java abnormal structure hierarchical graph

   The figure lists only some of the more common abnormalities, as well as the vast majority did not show up, we can see a large family of Java exceptions. Error There is a thing for me, almost did not come across, indicate more serious problems running the application, the operation has nothing to do with the most errors execution of code writers, and represents the code is running JVM (Java Virtual Machine) problems. For example, when the JVM is no longer required memory resources to continue operations, OutOfMemoryError will appear. Exception is an abnormal process itself can handle. That is your usual null pointer exception (NullPointerException), an array out of bounds exception (IndexOutOfBoundsException) and so on.

Third, check the exceptions and unchecked exceptions

  Java's exception (including Exception and Error) divided checked exceptions (checked exceptions) and unchecked exceptions (unchecked exceptions).

① checked exceptions

  The compiler requires you to dispose of exception, the code is not running, the compiler will check your code will not appear abnormal, ask you for possible exceptions must make the appropriate treatment.

  Several inspection exception handling (checked exception) of:

  1.  Continue to throw, negative way, has been thrown into the java virtual machine can be handled by throws exception is thrown.
  2.  With a try ... catch capture

  Note that for checked exceptions must be handled, or must be captured or be thrown. In addition to RuntimeException and its subclasses, and Error, others are checked exceptions.

② unchecked exceptions

  The compiler does not require unusual compulsory disposal, does not check at compile time, one by one to check will make the work more complicated, it can only be checked out at runtime, such as null pointer abnormalities.

  Abnormal (unchecked exception) several processing unchecked:

  1. capture

  2. Continue to throw

  3. Does not deal

  Generally, we do not deal with, because it's hard to judge what will be the problem, handle exceptions and some you can not run, such as a null pointer, you need to find people manually. RuntimeException its subclasses, and errors (Error), it belongs to this type of exception.

 

Guess you like

Origin www.cnblogs.com/magic-sea/p/11741437.html