Common code exceptions (continuously updated)

Common code exceptions (continuously updated)

Table of contents

Common code exceptions (continuously updated)

1. java.lang.NullPointerException (null pointer exception)

2. java.lang.ClassNotFoundException (cannot find this class exception)

3. java.lang.ArithmeticException (arithmetic exception)

4. java.lang.ArrayIndexOutOfBoundsException (array index out of bounds exception)

5. java.lang.IllegalArgumentException (parameter exception)

6. java.lang.IllegalAccessException (no access exception)

7. java.lang.IncompatibleClassChangeError (incompatible class change error)

8. java.lang.InstantiationError (instantiation exception)

9. java.lang.LinkageError (connection error)

10. java.lang.StackOverflowError (stack overflow)

11. java.lang.OutOfMemoryError (memory overflow exception)


1. java.lang.NullPointerException (null pointer exception)

Null pointer exception: Simply put, it is calling an uninitialized object or an object that does not exist. This error often occurs in operations such as creating pictures and calling arrays, such as uninitialized pictures, or wrong paths when creating pictures, etc. wait. Null pointers appear in array operations. In many cases, it is a common mistake made by friends who are just starting to learn programming, that 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 for the array, and the elements in the initialized array are not instantiated and are still empty, so each element needs to be initialized (if it is called).

//使用一下判断方式有效控制空指针异常
//判断String aa是否为hello
String aa = hello;
if("hello".equals(aa)){}

2. java.lang.ClassNotFoundException (cannot find this class exception)

Error message: The class package could not be found

  1. First of all, the corresponding jar package is not added in the project file. Check whether there is a jar package in the dependencies.
  2. If there is a jar package. Double-click to open it. If the prompt is damaged, you need to re-download and re-import
  3. The final reason is that the jar package of the maven project has not been published to the deployed project file. No lib dependencies are generated in the built-in tomcat

3. java.lang.ArithmeticException (arithmetic exception)

The explanation of this exception is "mathematical operation exception". For example, if there is an operation such as division by zero in the program, such an exception will occur. For this kind of exception, everyone should check carefully the places involving mathematical operations in your program. Is there something wrong with the formula?

4. java.lang.ArrayIndexOutOfBoundsException (array index out of bounds exception)

I believe that many friends have often encountered this exception. The explanation of the exception is "array subscript out of bounds". Now most of the programs have operations on arrays, so when calling arrays, you must check carefully to see the subscript you call. Is it beyond the scope of the array? Generally speaking, it is not easy to make such a mistake when calling explicitly (that is, directly using a constant as a subscript), but it is often wrong to call implicitly (that is, using a variable to represent a subscript). There is also a The situation is that the length of the array defined in the program is determined by some specific method, not declared in advance. At this time, it is best to check the length of the array first to avoid this exception

5. java.lang.IllegalArgumentException (parameter exception)

The explanation of this exception is "the parameter of the method is wrong". Many methods in the J2ME class library will cause such an error in some cases. For example, if the volume parameter in the volume adjustment method is written as a negative number, this exception will occur. Another example is g .setColor(int red, int green, int blue) in this method, if there are three values ​​exceeding 255, this exception will also occur, so once we find this exception, what we have to do is to quickly check the method call Is there an error in the parameter passing.

6. java.lang.IllegalAccessException (no access 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 Package in the program.

7. java.lang.IncompatibleClassChangeError (incompatible class change error)

Incompatible class change error. This exception is thrown when an incompatible change has occurred to a class definition on which the executing method depends. Generally, this error is easily caused when the declaration definition of some classes in the application is modified without recompiling the entire application and running directly

8. java.lang.InstantiationError (instantiation exception)

Instantiation error. This exception is thrown when an application attempts to construct an abstract class or interface through Java's new operator.

9. java.lang.LinkageError (connection error)

Link error. This error and all its subclasses indicate that a class depends on other classes. After the class is compiled, the dependent class changes its class definition without recompiling all classes, thus causing an error.

10. java.lang.StackOverflowError (stack overflow)

Stack overflow errors are generally exceptions thrown when the level of call is too deep when recursion is used. The reason is: the recursive function is called infinitely, the function exists in the stack memory of the virtual machine in the form of a stack frame, and the stack frame is always created, resulting in a stack overflow.

11. java.lang.OutOfMemoryError (memory overflow exception)

  1. Check your code for infinite loops or recursive calls.
  2. Check for large loops that repeatedly generate new object entities.
  3. Check whether there is a query to obtain all the data in the query to the database. Generally speaking, if you fetch 100,000 records into memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to cause problems. After going online, there are more data in the database, and a single query may cause memory overflow. Therefore, try to use pagination for database queries.
  4. Check whether collection objects such as List and MAP are not cleared after use. Collection objects such as List and MAP will always have references to objects, so that these objects cannot be recycled by GC

Or the JVM memory setting is too small, you can increase the jvm memory, and optimize the code, the Java heap is used to store object instances, as long as the objects are continuously created, and there is a reachable path between the GC Roots and the object to avoid the garbage collection mechanism If these objects are cleared, a memory overflow exception will occur after the number of objects reaches the maximum heap capacity limit. (Excerpted from an in-depth understanding of the java virtual machine), so always new String() will not cause heap memory overflow.

java.lang.OutOfMemoryError: PermGen space exception handling in tomcat

The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of ​​memory. This memory is mainly used by JVM to store Class and Meta information. When Class is loaded by Loader, it will be placed in PermGen space, and it stores class instances. The Heap area of ​​(Instance) is different. GC (Garbage Collection) will not clean up the PermGen space during the running of the main program, so if there are many CLASS in your application, PermGen space errors are likely to occur. This error is common in When the web server pre compiles the JSP. If you use a large number of third-party jars under your WEB APP, and its size exceeds the default size of jvm (4M), then this error message will be generated. Solution: Manually set the MaxPermSize size modification

 

References - Detailed Explanation of Memory Overflow Errors

Guess you like

Origin blog.csdn.net/a34651714/article/details/102834548