search
HomeJavajavaTutorialWhat are the common exceptions and their handling in Java programs?

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)