Home  >  Article  >  Java  >  What are the commonly used exception handling in JAVA projects?

What are the commonly used exception handling in JAVA projects?

PHPz
PHPzforward
2023-05-04 13:40:061044browse

 1. Null pointer exception (java.lang.nullpointerexception)

This situation usually occurs when string variables are not initialized, arrays are not initialized, class objects are not initialized, etc. Another situation is that when the object is empty, you did not determine whether it is a null value. I made this mistake in the previous web exercises, so in order to avoid this situation, in addition to checking whether it is initialized, if necessary Add an if statement to determine whether it is null.

 2. The specified class does not exist (java.lang.ClassNotFoundException)

One of the reasons for this error is the lack of packages. In this case, just download and import the corresponding The package is enough; when we have imported the package and this error is reported again, we need to open our own editor to adjust the settings; when using tomcat, first check whether the jar is imported into the lib .

  3. Exception when converting string to number (java.lang.NumberFormatException)

This error is when non-numeric characters appear in the string and are converted to numbers. An exception occurs; in addition, this error will also occur if the conversion of a string to a number exceeds the range of the type (such as string to int and string to double). The solution to this problem is to check the string before converting.

  4. Array subscript out-of-bounds exception (java.lang.IndexOutOfBoundsException)

As the name suggests, the array element you want to retrieve is not defined in the array, such as defining An array a with a length of 5 is obtained. When you want to access the elements of a[6], you will definitely make an error. To solve this kind of problem, we need to pay attention to the length of the array. Sometimes in order to reduce the waste of space, we will use the dynamic array construction method. At this time, when operating on the array, it is recommended to use length to get the array length first, so as to avoid errors.

 5. Mathematical operation exception (java.lang.ArithmeticException)

This error will be reported when the divisor is 0. Solution: avoid the divisor being 0. This error is interpreted as "an abnormal operation condition occurs." In addition to the case where the divisor is 0, there may be other abnormal situations. The specific situation will be analyzed in detail.

6. No access permission (java.lang.IllegalAccessException)

Permission problem, just pay attention to the access permission when the program accesses a method (public/private) , this kind of error easily occurs when using packages.

 7. Method parameter error (java.lang.IllegalArgumentException)

When calling a method with parameters, please pay attention to whether the parameters passed are correct.

  8. Data type conversion exception (java.lang.ClassCastException)

This error is prone to occur when performing forced type conversion. Please perform the type conversion before conversion. Identify and avoid mistakes.

 9. File not found exception (java.lang.FileNotFoundException)

This error will be reported when the program tries to open a non-existent file for read and write operations. , usually issued by the constructor declaration of FileInputStream, FileOutputStream, and RandomAccessFile. Even if the file exists but cannot be accessed for some reason, this error will be reported.

  10. Array storage exception (java.lang.ArrayStoreException)

If you store a string type variable in an int type array, an error will be reported. The solution is Find out the type when saving the object, or perform a type conversion before saving.

11. Method does not exist exception (java.lang.NoSuchMethodException)

The method to be called by the program does not exist. Solution: Do not call or construct its methods.

 12. File ended exception (java.lang.EOFException)

This exception is raised when the end of the file or stream is encountered during program input. This exception is used for Checks if the end of file or stream is reached.

 13. Instantiation exception (java.lang.InstantiationException)

Exception caused by the class being unable to instantiate through the constructor when creating a new object. Solution: Constructor.

  14. Interrupted exception (java.lang.InterruptedException)

The error reported when another thread is terminated through the interrupt method of Thread of other threads. Solution: First, throw it directly without processing; second, catch the exception, call the interrupt method again, and reset the interrupt status to true.

15. Clone exception is not supported (java.lang.CloneNotSupportedException)

If the clone method is called without implementing the Cloneable interface, this error will be reported; if the class does not Cloneable interface is supported, and this error will also occur when calling. Solution: Implement the Cloneable interface.

 16. Input and output exception (IOException)

This exception is a branch of Exception, which usually occurs when reading and writing file data.

  17. Error (java.lang.Error)

The base class for all errors, used to identify serious program running problems. Usually the cause is a series of problems when accessing external resources, and the solution needs to focus on accessing external resources.

The above is the detailed content of What are the commonly used exception handling in JAVA projects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete