What 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!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
