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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

itertools.combinations is used to generate all non-repetitive combinations (order irrelevant) that selects a specified number of elements from the iterable object. Its usage includes: 1. Select 2 element combinations from the list, such as ('A','B'), ('A','C'), etc., to avoid repeated order; 2. Take 3 character combinations of strings, such as "abc" and "abd", which are suitable for subsequence generation; 3. Find the combinations where the sum of two numbers is equal to the target value, such as 1 5=6, simplify the double loop logic; the difference between combinations and arrangement lies in whether the order is important, combinations regard AB and BA as the same, while permutations are regarded as different;

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.

java.lang.OutOfMemoryError: Javaheapspace indicates insufficient heap memory, and needs to check the processing of large objects, memory leaks and heap settings, and locate and optimize the code through the heap dump analysis tool; 2. Metaspace errors are common in dynamic class generation or hot deployment due to excessive class metadata, and MaxMetaspaceSize should be restricted and class loading should be optimized; 3. Unabletocreatenewnativethread due to exhausting system thread resources, it is necessary to check the number of threads, use thread pools, and adjust the stack size; 4. GCoverheadlimitexceeded means that GC is frequent but has less recycling, and GC logs should be analyzed and optimized.

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

The core of mastering Advanced SpringDataJPA is to select the appropriate data access method based on the scenario and ensure performance and maintainability. 1. In custom query, @Query supports JPQL and native SQL, which is suitable for complex association and aggregation operations. It is recommended to use DTO or interface projection to perform type-safe mapping to avoid maintenance problems caused by using Object[]. 2. The paging operation needs to be implemented in combination with Pageable, but beware of N 1 query problems. You can preload the associated data through JOINFETCH or use projection to reduce entity loading, thereby improving performance. 3. For multi-condition dynamic queries, JpaSpecifica should be used
