java errors and exceptions

Source: http: //blog.csdn.net/wuwenxiang91322/article/details/10346337

 

as java object to handle the exception, and defined as a base class for all exceptions java.lang.Throwable superclass.

 

1. Throwable

Throwable class is the superclass exception or error, it has two subclasses: Error and Exception.

 

2. Error

 

3. java abnormal

java provides two mechanisms for an exception: Exception (Checked Exception) (RuntimeException) and malfunctions compile time.

Abnormal (RuntimeException) refers to abnormal java compiler can compile-time checking, these anomalies must try catch, that is, it must be declared and capture. When our common IO abnormalities and abnormal sql belongs to compile exception.

Abnormal (Checked Exception) refers to abnormal function itself is not the cause of run-time, for example, it is unusual because the caller passed parameters cause of this anomaly need not be declared not need to capture, virtual opportunity to take over the program allows users to modify the code to stop .

 

RuntimeException exceptions in the java.lang package, the following are common RuntimeException exception:

A ClassCastException (class cast exception when trying to cast an object is not an instance of a subclass, the exception thrown)

An IndexOutOfBoundsException (Throws array bounds, indicating an ordering index (e.g. sorted array, a string, or a vector) is out of range)

NullPointerException (null pointer, when an application attempts to use null object where needed Thrown)

ArrayStoreException (data access exception, inconsistent operation array type, an array of trying to throw the wrong type of objects are stored in an object exception)

BufferOverflowException (abnormal operation of IO)

IllegalArgumentException (Thrown to indicate that passed an illegal or incorrect parameter to the method)

 

4. The capture and processing of abnormality

java exception handling involves five keywords: try, catch, finally, throw, throws.

(1) try: placed inside the code may throw an exception.

(2) catch: corresponding to a code block and exception types, handle exceptions, can have multiple catch blocks.

(3) finally: try to open mainly for recycling of resources (database connections, network connections, disk file), exception mechanism always guarantee that the finally block is always executed. Only fianlly block execution until after the completion of execution try to block or catch the return or throw, etc. termination statement.

(4) throw: for a practical throw exception, may be used alone as the statement, a specific exception objects thrown.

(5) throws: used in the method signature, to declare the method may throw an exception.

 

5. throw and the difference between throws

A method for internal body throw for throwing an exception Throwable. Exceptions Exception type, it should also declare the method header method that might be thrown if the compiler throws. The caller of the method must also check processing throw. If all else layers of the abnormal throwing acquired will eventually JVM process, is to print the exception message and stack information.

The method throws an external method declaration section body, the method used to declare some abnormality might be thrown. Only when the compiler throws an exception, which must be addressed before the caller or re-throw the exception. When the caller of the method of inability to handle the exception, it should continue to throw, rather than a general print stack information about the catch to be reluctant to deal with.

Reference documents:

https://blog.csdn.net/wuwenxiang91322/article/details/10346337

https://blog.csdn.net/huhui_cs/article/details/38817791

 

Guess you like

Origin www.cnblogs.com/cyydmlrs520/p/11350047.html