Home  >  Article  >  Java  >  What are the exceptions in java

What are the exceptions in java

angryTom
angryTomOriginal
2019-11-12 15:44:274179browse

What are the exceptions in java

What are the exceptions in java

When we write java programs, we will always encounter various errors. , the following summarizes common exception errors for you. (Recommended tutorial: java tutorial)

1. java.lang.nullpointerexception

Everyone must encounter this exception often, and the explanation of the exception It's "the program encountered a null pointer". Simply put, it means calling an uninitialized object or a non-existent object. This error often occurs in operations such as creating pictures and calling arrays. For example, the picture is not initialized, or the picture is not initialized. Wrong path when creating etc. The occurrence of null pointers in array operations is often a mistake made by friends who are just beginning to learn programming, that is, they 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 each element needs to be initialized (if it is to be called)

2. java.lang.classnotfoundexception

This exception often occurs when many programmers who originally developed in jb and other development environments compile packages under jb under wtk. The problem is that the explanation of the exception is "the specified class does not exist". Here we mainly consider whether the name and path of the class are correct. If the package is made under jb, package is usually added by default, so transfer After downloading wtk, be sure to add the package path.

3. java.lang.arithmeticexception

The explanation of this exception is "mathematical operation exception". For example, an operation such as division by zero will occur in the program. For such an exception, everyone should carefully check the mathematical operations involved in their program to see if there is something wrong with the formula.

4. java.lang.arrayindexoutofboundsexception

I believe many friends have often encountered this exception. The explanation of the exception is "array index out of bounds". Now in the program Most of them operate on arrays, so when calling an array, you must check carefully to see if the subscript you are calling exceeds the range of the array. Generally speaking, it is not easy to display (that is, directly use a constant as the subscript) call. Such an error occurs, but implicit (that is, using a variable to represent the subscript) call often causes errors. Another situation is that the length of the array defined in the program is determined by some specific methods and is not declared in advance. This It is best to check the length of the array first to avoid this exception.

5. java.lang.illegalargumentexception

The explanation of this exception is "method parameter error". Many methods in j2me class libraries will in some cases Causes such an error. For example, if the volume parameter in the volume adjustment method is written as a negative number, this exception will occur. Another example is the three values ​​​​in the g.setcolor(int red, int green, int blue) method. If there are more than 255 This exception will also occur, so once we find this exception, what we have to do is to quickly check whether there is an error in the parameter passing in the method call.

6. java.lang.illegalaccessexception

The explanation of this exception is "no access permission". When the application wants to call a class, but the current method does not This exception will occur when accessing this class. Please pay attention to this exception when using packages in the program.

There are many other exceptions, so I won’t list them one by one. What I want to explain is that a qualified programmer needs to have a considerable understanding of common problems in the program and corresponding solutions, otherwise If you just stop at writing programs without changing them, it will greatly affect your own development. All descriptions of exceptions can be found in the API.

Arithmetic exception class: ArithmeticExecption

Null pointer exception class: NullPointerException

Type cast exception: ClassCastException

Array negative subscript exception: NegativeArrayException

Array subscript out-of-bounds exception: ArrayIndexOutOfBoundsException

Violation of security principles exception: SecurityException

File ended exception: EOFException

File not found exception: FileNotFoundException

String to number conversion exception: NumberFormatException

Operation database exception: SQLException

Input and output exception: IOException

Method not found exception: NoSuchMethodException

java.lang.AbstractMethodError

Abstract method error. Thrown when the application attempts to call an abstract method.

java.lang.AssertionError

Assertion error. Used to indicate an assertion failure.

java.lang.ClassCircularityError

Class circular dependency error. When initializing a class, this exception is thrown if a circular dependency between classes is detected.

java.lang.ClassFormatError

Class format error. Thrown when the Java virtual machine attempts to read a Java class from a file and detects that the contents of the file do not conform to a valid format for the class.

java.lang.Error

mistake. Is the base class for all errors and is used to identify serious program operation problems. These problems usually describe some unusual situation that should not be caught by the application.

java.lang.ExceptionInInitializerError

Initializer error. Thrown when an exception occurs during the execution of a class's static initializer. Static initializer refers to the static statement segment directly included in the class.

java.lang.IllegalAccessError

Illegal access error. This exception is thrown when an application attempts to access or modify a field (Field) of a class or call its method, but violates the visibility declaration of the field or method.

java.lang.IncompatibleClassChangeError

Incompatible class change error. This exception is thrown when an incompatible change occurs in the class definition on which the method being executed 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 then running it directly.

java.lang.InstantiationError

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

java.lang.InternalError

Internal error. Used to indicate that an internal error has occurred in the Java virtual machine.

java.lang.LinkageError

Linkage 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 the error condition.

java.lang.NoClassDefFoundError

Class definition not found error. This error is thrown when the Java virtual machine or class loader attempts to instantiate a class and cannot find the definition of the class.

java.lang.NoSuchFieldError

Field does not exist error. This error is thrown when an application attempts to access or modify a field of a class that does not have a definition for that field.

java.lang.NoSuchMethodError

There is no error in the method. This error is thrown when the application attempts to call a method of a class that does not have a definition for the method.

java.lang.OutOfMemoryError

Out of memory error. This error is thrown when the available memory is insufficient for the Java virtual machine to allocate an object.

java.lang.StackOverflowError

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

java.lang.ThreadDeath

The thread ends. This error is thrown when the stop method of the Thread class is called to indicate the end of the thread.

java.lang.UnknownError

Unknown error. Used to indicate that an unknown serious error has occurred in the Java virtual machine.

java.lang.UnsatisfiedLinkError

Unsatisfied link error. Thrown when the Java virtual machine does not find a native language definition of a class that declares a native method.

java.lang.UnsupportedClassVersionError

Unsupported class version error. This error is thrown when the Java virtual machine tries to read a certain class file, but finds that the major and minor version numbers of the file are not supported by the current Java virtual machine.

java.lang.VerifyError

Verification error. This error is thrown when the validator detects an internal incompatibility or security issue in a class file.

java.lang.VirtualMachineError

Virtual machine error. Used to indicate a situation where a virtual machine has been destroyed or has insufficient resources to continue operations.

java.lang.ArithmeticException

Arithmetic condition exception. For example: integer division by zero, etc.

java.lang.ArrayIndexOutOfBoundsException

Array index out-of-bounds exception. Thrown when the index into the array is negative or greater than or equal to the array size.

java.lang.ArrayStoreException

Array storage exception. Thrown when an object of non-array declared type is stored in an array.

java.lang.ClassCastException

Class cast exception. Suppose there are classes A and B (A is not the parent class or subclass of B), and O is an instance of A, then this exception is thrown when O is forced to be constructed as an instance of class B. This exception is often called a cast exception.

java.lang.ClassNotFoundException

Cannot find class exception. This exception is thrown when the application attempts to construct a class based on a class name in string form, but cannot find the class file with the corresponding name after traversing the CLASSPAH.

java.lang.CloneNotSupportedException

Clone exception is not supported. When the Cloneable interface is not implemented or the clone method is not supported, calling its clone() method will throw this exception.

java.lang.EnumConstantNotPresentException

There is no exception in the enumeration constant. This exception is thrown when an application attempts to access an enumeration object by name and enumeration type, but the enumeration object does not contain a constant.

java.lang.Exception

Root exception. Used to describe the situation the application wishes to capture.

java.lang.IllegalAccessException

Illegal access exception. This exception is thrown when the application attempts to create an instance of a class, access the class properties, or call the class method through reflection, and the definition of the class, attribute, method, or constructor cannot be accessed at that time.

java.lang.IllegalMonitorStateException

Illegal monitoring status is abnormal. This exception is thrown when a thread attempts to wait for the monitor of an object (O) that it does not own or notifies other threads to wait for the monitor of the object (O).

java.lang.IllegalStateException

Illegal state exception. This exception is thrown when a method is called in the Java environment and application before it is in a legal calling state.

java.lang.IllegalThreadStateException

Illegal thread state exception. When the county is not in the legal calling state of a certain method and the method is called, an exception is thrown.

java.lang.IndexOutOfBoundsException

Index out-of-bounds exception. This exception is thrown when the index value of a sequence is less than 0 or greater than or equal to the sequence size.

java.lang.InstantiationException

Instantiation exception. This exception is thrown when trying to create an instance of a class through the newInstance() method, and the class is an abstract class or interface.

java.lang.InterruptedException

Interrupted exception. This exception is thrown when a thread is in a long-term waiting, sleeping or other suspended state, and other threads terminate the thread through Thread's interrupt method.

The above is the detailed content of What are the exceptions in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn