Java_ common abnormalities and treatment

1 Overview

        Each packet of Java class library are defined in [] exception classes, these classes are all Throwable class subclasses, belongs to the class Throwable java.lang package, this package will be automatically incorporated java program for each runtime; class Throwable there are two subclasses, error JVM class is wrong, IOException class is the output class input, RuntimeExceptio runtime class is a class;

Common Error type:

Arithmetic exception categories: ArithmeticException null pointer exception classes: NullPointerException

Type cast exception: ClassCastException negative array subscripts exception: NegativeArrayException

Cross-border abnormal array subscript: ArrayIndexOutOfBoundsException security breach anomaly: SecturityException

End of file an exception: EOFException File not found an exception: FileNotFoundException

String into a number abnormalities: NumberFormatException operation database exception: SQLException

Input and output Abnormal: IOException method is not abnormal to find: NoSuchMethodException

2, detailed

1, a null pointer exception classes: a NullPointerException

  Interpretation of this exception is "caught in a null pointer program", simply put, is called uninitialized object or the object does not exist. This error often appear in the picture is created, call the array of these operations, such as uninitialized pictures, or pictures to create a path errors when. Null pointer array operation occurs, in many cases, some students just beginning to learn programming common mistakes that the initialization and initialize the array element of the array confused. Initialize the array is allocated space for an array of needs, and the array after initialization, whose elements did not instantiated, is still empty, so it needs to initialize each element.
  Often in the jsp programming: if (. Request.getParameter ( "username ") equals ( "xxx")), out.println (session.getAttribute ( "record")) and so on. The solution to this problem is to be sentenced in the air before use comparison: 

  if (request.getParameter(“username”)!=null){ 
        if (request.getParameter(“username”).equals(“xxx”)){
            …
        }
   }

2, the specified class does not exist:. The java.lang a ClassNotFoundException

  This exception was originally developed in many other development environments programmer JB, JB under the package on the issue WTk compiled under recurring, unusual explanation is "the specified class does not exist", where the main test

Worry about the name and class path is correct can, if it is done under the program JB package, usually the default plus Package, so go after the next pay attention to the WTK path Package plus.

3, math anomaly: the java.lang. ArithmeticException

  This unusual interpretation is "math exception", such as abnormal procedures such as divide by zero appeared in such operations will be out of this anomaly, we should look carefully check their procedures related to the number of

Local school operations, the formula is not something wrong.

4, array subscript out of range:. The java.lang ArrayIndexOutOfBoundsException

  This exception I believe many of my friends are often encountered, unusual interpretation is "array index out of bounds", the program now has most of the operation of the array, so be sure to carefully check when calling the array, look subscript own call is not beyond the scope of the array, in general, display (i.e., directly labeled with a constant current) call is not easy with such errors, but implicit (i.e., represented by a variable subscript) calls often wrong, there is a , the length of the array is defined in the program is determined by some particular method, not prior notice, this time, it is best to check out the length of the array, in order to avoid this anomaly.

5. The method of parameter error:. The java.lang IllegalArgumentException


  This unusual interpretation is "parameter error method", in many ways J2ME library of such errors will lead, in some cases, such as volume control volume parameter method if this will be written in a negative anomaly, another example g .setColor (int red, int green, int blue) values ​​of the three methods, if there are more than 255 will also appear this exception, so if it is found that abnormal, we need to do is rush to check the method call parameter passing is not an error.

6, there is no access to:. The java.lang IllegalAccessException

  This unusual interpretation is "no access", when the application to call a class, but that is the current method does not have access to this class there will be the exception. In the case of the program with a Package to pay attention to this anomaly.

7、java.lang.IncompatibleClassChangeError


  Incompatible changes in the wrong. When the method is performed depends class definition incompatible changes occurred, the exception is thrown. Usually in the revised declaration defines certain classes of applications to run directly without recompile the entire application case, it is apt to cause the error.


8、java.lang.InstantiationError

  Examples of errors. The exception is thrown when an application attempts an abstract class or interface configured Java new operator time.

9、java.lang.LinkageError

  Link error. The error and all its subclasses indicate that a class depends on some other class, after class being compiled, the dependent class changed its class definition without recompiling all the classes, and thus lead to error.


10、java.lang.StackOverflowError


  Stack overflow error. This error is thrown when an application calls the recursive hierarchy too deep and causes a stack overflow.

  There are many other anomalies, which are not enumerated. But to explain, a qualified programmer, you need to have a considerable understanding and appropriate solution to a common problem in the program, otherwise just stay in the writing process without changing the program, it will greatly affect their development . You can really explain Coding standard level of understanding is one very important piece of the exception. About a full description of anomalies, can be found at api years. We do not have them put everything back in the brain, as long as the abnormal to have some common impression, know where to find almost the same when you actually have, after all, Java programmers the most painful is its knowledge capacity too much, and we will encounter new bugs and new problems and new knowledge every day, so one of their own grasp of queuing abnormal ability is the most important.

reference:

https://www.cnblogs.com/zeyuxi/p/9046137.html

https://blog.csdn.net/little_oranges/article/details/81197267

 

 

 

Guess you like

Origin blog.csdn.net/jiahao1186/article/details/91959101