Article Directory
- 1. What is an exception
- 2. Classification of abnormalities
- Three, common Exception
-
- 1, java.lang.NullpointerException (Null Pointer Exception)
- 2. java.lang.ClassNotFoundException (the specified class does not exist)
- 3. java.lang.ClassNotFoundExceptio (the specified class does not exist)
- 4. java.lang.IndexOutOfBoundsException (array subscript out of bounds exception)
- 5. java.lang.IllegalArgumentException (method parameter error)
- 6, java.lang.IllegalAccessException (no access permission)
- 7, java.lang.ArithmeticException (mathematical operation exception)
- 8, java.lang.ClassCastException (data type conversion exception)
- 9. java.lang.FileNotFoundException (file not found exception)
- 10. java.lang.ArrayStoreException (array storage exception)
- 11. java.lang.NoSuchMethodException (the method does not exist exception)
- 12. java.lang.EOFException (the file has ended exception)
- 13, java.lang.InstantiationException (instantiation exception)
- 14. java.lang.InterruptedException (aborted exception)
- 15, java.lang.CloneNotSupportedException (does not support clone exception)
- 16, java.lang.OutOfMemoryException (out of memory error)
- 17, java.lang.NoClassDefFoundException (Class definition not found error)
1. What is an exception
In actual development, there are many situations that may cause system errors. For example, equipment errors, unreasonable restrictions, code errors, etc. In order to solve this problem, Java proposes an error capture mechanism called Exception Handing. Under this mechanism, exceptions are divided into two types: ERROR and Exception
2. Classification of abnormalities
We can first draw a simple hierarchy diagram of the abnormal structure:
ERROR level exceptions are often caused by system-level problems, and are usually handed over to JVM for processing. And we need to understand the exception of the Exception level.
Three, common Exception
1, java.lang.NullpointerException (Null Pointer Exception)
Reason: This exception is often encountered. The reason for the exception is that there is a null pointer in the program, that is, an uninitialized object or a non-existent object is called in the program.
It often occurs in the code of creating objects and calling arrays, such as the object is not initialized, or the path when the image is created is wrong, etc. Pair array code
The presence of a null pointer in the array confuses the initialization of the array and the initialization of the array elements. The initialization of the array is to allocate space for the array, and the array elements
Initialization is to assign initial values to the elements in the array
2. java.lang.ClassNotFoundException (the specified class does not exist)
Reason: When trying to convert a String type data to a specified numeric type, but the string does not meet the requirements of numeric data, this exception is thrown
often. For example, when converting String type data "123456" into numeric type data, it can be converted. But if the String type data contains
Non-numeric characters, such as 123*56, will be abnormal when converted to numeric characters. The system will catch this exception and deal with it
3. java.lang.ClassNotFoundExceptio (the specified class does not exist)
Reason: because the name and path of the class are incorrect, usually when the program tries to load a certain class through a string, an exception may be thrown. E.g:
An exception occurs when calling Class.forName(), or calling finaSystemClass() of ClassLoad, or LoadClass()
4. java.lang.IndexOutOfBoundsException (array subscript out of bounds exception)
Reason: Check whether the subscript value of the array or string called in the program exceeds the range of the array. Generally speaking, it is not easy to display the calling array.
It’s wrong, but the implicit call may be wrong. There is another case, the length of the array defined in the program is determined by some specific method, not
As stated in advance, you can first check the length of the array at this time to avoid this exception
5. java.lang.IllegalArgumentException (method parameter error)
For example, if there are three values in the g.setColor(int red, int green, int blue) method, if there are more than 255, this exception will occur.
If this exception exists in the sequence, it is necessary to check whether the parameter passing or parameter value in the method call is wrong
6, java.lang.IllegalAccessException (no access permission)
This exception occurs when the program wants to call a class, but the current method does not have access to the class. If Package is used in the program
This exception may occur
7, java.lang.ArithmeticException (mathematical operation exception)
Such an anomaly occurs when there is an operation such as dividing by zero in a mathematical operation.
8, java.lang.ClassCastException (data type conversion exception)
This exception occurs when trying to force down conversion of an object, but the object is neither convertible nor convertible to an instance of its subclass
9. java.lang.FileNotFoundException (file not found exception)
This exception will be raised when the program opens a file that does not exist for reading or writing. The exception is caused by FileInputStream, FileOutputStream,
The constructor declaration of RandomAccessFile throws out, even if the file being operated exists, it is not accessible for some reason, such as opening a
Only a file with read-only permission and write data to it, the above construction method will still cause an exception
10. java.lang.ArrayStoreException (array storage exception)
An exception will be thrown when trying to store an object of an incompatible type into an Object[] array
11. java.lang.NoSuchMethodException (the method does not exist exception)
When the program tries to create an object through reflection, access (modify or read) a method, but the method does not exist, an exception will be thrown.
12. java.lang.EOFException (the file has ended exception)
When the program encounters the end of the file or stream during the input process, an exception is thrown. So this exception is used to check if the end of the file or stream is reached
13, java.lang.InstantiationException (instantiation exception)
Raised when trying to create an instance of a certain class through the newInstance() method of Class, but the program cannot create the object through the constructor.
The Class object represents an abstract class, interface, array class, and basic type. The class represented by this Class has no corresponding constructor.
14. java.lang.InterruptedException (aborted exception)
This exception is thrown when a thread is in a long-term waiting, sleeping, or other suspended state, while other threads terminate the thread through the interrupt method of Thread.
15, java.lang.CloneNotSupportedException (does not support clone exception)
When the Cloneable interface is not implemented or the clone method is not supported, calling its clone() method will throw the exception
16, java.lang.OutOfMemoryException (out of memory error)
This error is thrown when the available memory is not enough for the Java virtual machine to allocate to an object
17, java.lang.NoClassDefFoundException (Class definition not found error)
This error is thrown when the Java virtual machine or the class loader tries to instantiate a class, but the definition of the class cannot be found