In addition to the runtime exceptions and non-runtime exceptions mentioned in the previous section, there are many common exceptions, such as:
Arithmetic exception: ArithmeticExecption
NullPointerException: NullPointerException
Class conversion exception: ClassCastException
Negative array subscript exception: NegativeArrayException
Array subscript out-of-bounds exception: IndexOutOfBoundsException
Exception that violates security principles: SecurityException
Access permission exception: IllegalAccessException
End of file exception: EOFException
File not found exception: FileNotFoundException
String to number exception: NumberFormatException
Operation database exception: SQLException
Input and output exception: IOException
Method not found exception: NoSuchMethodException
…
This section mainly introduces the following five anomalies:
Null pointer exceptions are also often encountered when programming. The occurrence of this exception means that "the program encountered a null pointer". Simply put, it means that an uninitialized object or an object that does not exist is called. This error often occurs when calling In the operation of arrays, a common mistake made by people who are just beginning to learn programming is to confuse the initialization of the array with the initialization of the array elements.
The initialization of the array is to allocate the required space to the array, and the elements in the initialized array have not been instantiated and are still empty, so if you want to call it, you need to initialize each element.
Data type conversion errors, such as:
String temp=abc;
If it is set to int temp, an error will be reported because their types are different, but if it is set to object temp, it will be fine because object is their parent class.
We often encounter this exception when operating arrays. The exception means "array subscript out of bounds". Most of the current programs have operations on arrays, so when calling an array, you must check carefully to see if you are calling Is the subscript beyond the range of the array? Generally speaking, it is less likely to cause such errors when calling directly with constants, but it is easy to make such errors when calling variables. In another case, the length of the array defined in the program is determined by certain methods. The decision is not declared in advance. At this time, it is best to check the length of the array to avoid this exception.
The explanation of this exception is "no access rights." This exception occurs when the application wants to call a class, but the current method does not have access rights to the class. Pay attention to this exception when using packages in the program.
This exception will generally occur when reading and writing files. For example, if you want to read a file from the disk to a program you wrote, if the file does not exist on the hard disk, the Java virtual machine will report this exception.