Home > Java > javaTutorial > body text

What are the common exceptions and their handling in Java programs?

王林
Release: 2023-06-03 22:16:42
forward
1473 people have browsed it

1. Common exceptions in JDK

1. Common exception summary diagram

2. Classification of exceptions in java

The Throwable class has two direct subclasses:

(1) Exception: The problem can be caught

(2) Error: System error , usually handled by the JVM

3. Category of caught exceptions

(1) Check exception: Exception class derived from Exception, must be caught or declared to be thrown again Out

(2) Runtime exception: refers to those exception classes derived from RuntimeException. This kind of exception object can be thrown at any time using the throw statement throw new ArithmeticException(...);

2. Reasons for the exception

1. The user entered illegal data.

2. The file to be opened does not exist.

3. Network communication is disconnected

4. JVM memory overflow

3. Several common exceptions in java programs and the reasons for this exception

1.java.lang.NullpointerException(NullpointerException)

The common reason for this exception is that there is a null pointer in the program, that is, an uninitialized call is made. or non-existent object.

These codes often appear in object creation and array calls, such as when the object is not initialized or the image path is wrong. The null pointer appearing in the array code

is confusing the initialization of the array with the initialization of the array elements. The initialization of the array is to allocate space to the array, and the

initialization of the array element is to assign initial values ​​to the elements in the array

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

Cause: When trying to convert a String type data to a specified numeric type, but the string does not meet the requirements of numeric data, this exception is thrown

often. When converting data from String type to numeric type, for example, the operation of converting "123456" to numeric type is feasible. However, if the String type data contains

non-numeric characters, such as 123*56, an exception will occur when converting to numeric type. The system will catch this exception and handle it

3. java.lang.ClassNotFoundExceptio (the specified class does not exist)

The root of this problem lies in the class Incorrect names and paths may cause exceptions when the program attempts to load a class via a string. For example:

Exception occurs when calling Class.forName(), or calling ClassLoad’s finaSystemClass(), or LoadClass()

4, java.lang.IndexOutOfBoundsException (under array Subscript out-of-bounds exception)

Cause: Check whether the subscript value of the array or string called in the program exceeds the range of the array. Generally speaking, it is not easy to display the calling array.

Such a mistake, but the implicit call may make an error. 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. , at this time, you can check the length of the array first to avoid this exception

5, java.lang.IllegalArgumentException (method parameter error)

For example, g.setColor (int red, int green, int blue) three values ​​in this method, if there are more than 255, this exception will occur. If this exception exists in the program, you need to check the method call. Is there an error in parameter transfer or parameter value?

6, java.lang.IllegalAccessException (no access permission)

When the program wants to call a class, but the current This exception will occur if the method does not have access rights to the class. If Package is used in the program

This exception may occur

7. java.lang.ArithmeticException (mathematical operation exception)

This exception occurs when operations such as division by zero occur in mathematical operations.

8, java.lang.ClassCastException (data type conversion exception)

When trying to force down conversion of an object, but the object cannot be converted Or cannot be converted to an instance of its subclass, this exception will occur

9, java.lang.FileNotFoundException (file not found exception)

When the program opens a This exception will be thrown when reading or writing a file that does not exist. This exception is thrown by the constructor declaration of FileInputStream, FileOutputStream,

RandomAccessFile. Even if the file being operated exists, it is inaccessible for some reason, such as opening a

with only read-only permissions. file and write data to it, the above construction method will still cause an exception

10, java.lang.ArrayStoreException (array storage exception)

When trying to convert a type of When an object of an incompatible type is stored in an Object[] array, an exception will be thrown

11, java.lang.NoSuchMethodException (there is no exception in the method)

When the program tries to Create an object through reflection and access (modify or read) a method, but if the method does not exist, an exception will be thrown.

12. java.lang.EOFException (end of file exception)

When the program encounters the end of the file or stream during the input process, an exception is thrown. So this exception is used to check if the end of file or stream is reached

13. java.lang.InstantiationException (instantiation exception)

When trying to create an instance of a class through the newInstance() method of Class, but the program cannot pass the constructor Raised when the object is created.

Class object represents an abstract class, interface, array class, basic type. The class represented by this Class does not have a corresponding constructor.

14, java.lang.InterruptedException (aborted exception)

When a thread is in a long-term wait, sleep or other paused state, and at this time other This exception is thrown when the thread terminates the thread through Thread's interrupt method.

15. 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

16, java.lang.OutOfMemoryException (out of memory error)

Thrown when the available memory is insufficient for the Java virtual machine to allocate to an object The error

17, java.lang.NoClassDefFoundException (class definition error not found)

When the Java virtual machine or class loader attempts to instantiate a class, and This error is thrown when the definition of the class cannot be found

The above is the detailed content of What are the common exceptions and their handling in Java programs?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template