1.8 JAVA abnormal handling and classification
Abnormal either because of user error caused some procedural errors caused, because there are other physical errors.
Three kinds of exception: were checked exception (general exception), abnormal (non abnormality check) operation, the error
Throwable subclass is Error and Exception error class exception classes,
Exception IOException class and subclass RuntimeException class. All exception classes is frominherit class java.lang.Exceptionsubclasses.
Details frame as shown in FIG.
A, Error error for error indication runtime environment.
JVM memory overflow. In general, the program does not recover from the error.
Two, Java non-checked exception ( a runtime exception )
Errors and non abnormalities not check, usually for the JVM exception or error.
abnormal | description |
---|---|
ArithmeticException | When the exceptional arithmetic condition occurs, Thrown. For example, an integer "divide by zero" throws an instance of this class. |
ArrayIndexOutOfBoundsException | Access abnormal thrown when an array with an illegal index. If the index is greater than or equal to the array size of the negative, the index is invalid index. |
ArrayStoreException | Trying to store the wrong type to an object thrown when an array of anomalies. |
ClassCastException | When you try to cast an object is not an instance of a subclass Thrown. (Conversion type rotor parent class exception) |
IllegalArgumentException | Thrown to indicate that passed an illegal or incorrect parameter to the method. |
IllegalMonitorStateException | Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other objects are waiting for the monitor without owning the specified monitor thread. |
IllegalStateException | Signal is generated when the illegal or inappropriate time to call the method. In other words, the Java environment, or Java application is not in an appropriate state of the requested operation. |
IllegalThreadStateException | Thread is not in an appropriate state for requesting thrown required abnormal operation. |
IndexOutOfBoundsException | Indicating an ordered index (e.g. sorted array, a string, or vector) when thrown out of range. (Array bounds) |
NegativeArraySizeException | If the application tries to create an array with negative size, Thrown. |
NullPointerException | When an application attempts to use where an object is required null when Thrown (null pointer exception) |
NumberFormatException | When the application tries to convert a string into a numeric type, but that the string can not be converted into the appropriate format, the thrown exception. (ParseException, resolve anomalies) |
SecurityException | Thrown by the security manager to indicate a security violation. |
StringIndexOutOfBoundsException | This exception by the String method throws, or indication index is negative, or greater than the size of the string. |
UnsupportedOperationException | When the requested operation is not supported Thrown. |
Three, Java is defined in the java.lang package checked exception (general exception) class.
This exception may be part of the investigation, as program exceptions must be in the program capturing operation .
abnormal | description |
---|---|
ClassNotFoundException | When an application tries to load a class, you can not find the corresponding class Thrown. (FileNotFindException, could not find the file) |
CloneNotSupportedException | When you call Object class clone method clone an object, but the object's class can not be achieved Cloneable when the interface Thrown. |
IllegalAccessException | Deny access to a class, Thrown. |
InstantiationException | When you try to use the Class class newInstance you create an instance of a class method, designated class object because it is an interface or an abstract class can not be instantiated Thrown. |
InterruptedException | A thread is interrupted by another thread, throw the exception. |
NoSuchFieldException | Requested variable does not exist |
NoSuchMethodException | The method requested does not exist |
Common abnormal method generally have:.. 1 the getMessage () Returns detailed information about the occurrence of abnormality,
2. getMessage () Returns detailed information about the occurrence of abnormal
3. toString () using getMessage () returns the result of a cascade of class names.
4. printStackTrace () print toString () and stack layers to result System.err, i.e., error output stream.
Fourth, catch the exception
4.1 Use try and catch keywords can catch the exception. try / catch block is placed where the exception may occur.
Multi try / catch
try { file = new FileInputStream(fileName); x = (byte) file.read(); } catch(FileNotFoundException f) { // Not valid! f.printStackTrace(); return -1; } catch(IOException i) { i.printStackTrace(); return -1; }
4.2 throws catch the exception
If a method does not capture a checked exception, then the method must use the throws keyword to declare. throws keyword in the method signature of the tail.
4.3 finally keyword
finally keyword to create a code block after the code block is executed try.
Whether abnormality occurs, finally block code will always be executed.
In a finally block, clean type and the like can run ending statements rehabilitation nature.
finally block appears in the final catch blocks, syntax is as follows: