In Java Error and Exception

Exception and Error are inherited Throwable class in Java only instance of type Throwable can be thrown (throw) or capture (catch), which is the basic type of exception handling mechanism.

Exception and Error reflects the Java platform designers classification of different abnormal situations.

Exception is run properly, you can expect the unexpected situation, could and should be caught and dealt with accordingly.

Error means that under normal circumstances, is unlikely to happen, most of Error will cause the program (such as the JVM itself) in a non-normal, non-recoverable state. Since the non-normal circumstances, it is not easy to capture not need, such as common OutOfMemoryError and the like, are subclasses of Error.

Exception checking may be divided into (the checked) and not checked exception (an unchecked) abnormalities, abnormalities can be performed explicitly capturing process source code, which is part of the compiler checks.

Can not check Error, is not Throwable Exception. Abnormalities is not abnormal, similar to a so-called run NullPointerException, An ArrayIndexOutOfBoundsException the like, can usually be avoided encoding logic errors, according to the specific needs to determine whether to capture and not mandatory at compile time.

 

Contact Error and Exception of
inheritance structures: Error and Exception are all inherited from Throwable, RuntimeException inherited from Exception.

Error RuntimeException and its subclasses referred unchecked exceptions (Unchecked exception), the other becomes abnormal by abnormalities (Checked Exception).

Error and Exception difference
Error class generally refers to a virtual machine-related problems such as system crashes, the virtual machine error, insufficient memory space, the method call stack overflow. As java.lang.StackOverFlowError and Java.lang.OutOfMemoryError. For this type of error, Java compiler not to check them. For applications such errors lead to disruption, relying on the program itself can not be recovered and prevention, encounter such errors, we recommend the program to terminate.

Exception class represents an exception can handle, you can capture and possible recovery. When such an exception should be treated as an exception, the program resumes, and should not be free to terminate an exception.

 

 

 

Two basic principles of exception handling

 

First, try not capture similar Exception such a common anomaly, but should catch specific exceptions, here is Thread.sleep () throws InterruptedException. This is because in the daily development and cooperation, we have the opportunity to read the code is often more than write code, software engineering is the art of collaboration door, so we have an obligation to make their code to visually reflect as much information, and generalities Exception and the like, just hide our purpose. In addition, we must ensure that the program does not capture the abnormal we do not want to capture. For example, you may prefer to RuntimeException is diffused out, rather than be captured. Furthermore, unless the thought, do not capture or Throwable Error, making it difficult to ensure that we can properly handles OutOfMemoryError.

 

Second, do not swallow (swallow) exception. This is something abnormal processing should pay special attention, because it may lead to very strange situation difficult to diagnose. Swallow abnormal, often based on the assumption this code may not occur, or feeling ignore the exception does not matter, but do not make this assumption in production code! If we do not throw out an exception, or there is no output to the log (Logger) and the like, the program may end in an uncontrolled manner in the subsequent code. No one can easily determine exactly where an exception is thrown, and what is the cause of the anomaly.

 

 

OutOfMemoryError memory overflow generally occurs in the case of application memory space is no more release.

//java.lang.OutOfMemoryError  -Xmx150m
try {
    byte[] b = new byte[1024*1024*600];
} catch (OutOfMemoryError e) {
    e.printStackTrace();
}

Run-time, set the maximum heap memory jvm 150m, 600m application at this time of memory, and therefore will be reported error

java.lang.OutOfMemoryError: Java heap space

 

java.lang.StackOverflowError
stack overflow error. This error is thrown when an application calls the recursive hierarchy too deep and causes a stack overflow.

public static void main(String[] args) {
    method();
}
 
public static void method() {
    while (true) {
        method();
    }
}

Unlimited number of recursive calls appear

Exception in thread "main" java.lang.StackOverflowError

 

 

Common exceptions;

ArrayIndexOutOfBoundsException array subscript bounds exception,

ArithmaticException arithmetic abnormalities such as divide by zero

NullPointerException null pointer exception

IllegalArgumentException invalid parameter error

 

Guess you like

Origin www.cnblogs.com/alter888/p/11229782.html