search
HomeJavaJavagetting StartedExceptions and errors in java

Exceptions and errors in java

Nov 14, 2019 am 11:24 AM
java

In Java, all exceptions have a common ancestor Throwable (throwable). Throwable specifies the commonality of any problem in code that can be propagated through a Java application using an exception propagation mechanism.

Exceptions and errors in java

Throwable: There are two important subclasses: Exception (Exception) and Error (Error), both of which are important subclasses of Java exception handling. Each contains a large number of subcategories. The difference between exceptions and errors is that exceptions can be handled by the program itself, while errors cannot be handled.               (Recommended learning: java course)

Error (error): is an error that cannot be handled by the program, indicating a serious problem in running the application. Most errors have nothing to do with actions performed by the code writer and instead represent problems with the JVM (Java Virtual Machine) while the code is running.

For example, Java virtual machine running error (Virtual MachineError), when the JVM no longer has the memory resources required to continue executing the operation, an OutOfMemoryError will occur.

When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to terminate the thread. These errors indicate that the fault occurs in the virtual machine itself or when the virtual machine attempts to execute an application, such as Java virtual machine running error (Virtual MachineError), class definition error (NoClassDefFoundError), etc.

These errors are uncheckable because they are outside the control and processing capabilities of the application program, and most of them are situations that are not allowed to occur when the program is running. For a well-designed application, even if an error does occur, there should be no attempt to handle the exception condition it caused. In Java, errors are described through subclasses of Error.

Exception: is an exception that the program itself can handle. The Exception class has an important subclass, RuntimeException. The RuntimeException class and its subclasses represent errors caused by "common JVM operations."

For example, if you try to use a null object reference, divide by zero, or the array is out of bounds, runtime exceptions (NullPointerException, ArithmeticException) and ArrayIndexOutOfBoundException will be thrown respectively.

Exception (exception) is divided into two categories: Run-time exception and non-run-time exception (compilation exception). The program should handle these exceptions as much as possible.

Runtime exceptions: are all exceptions of the RuntimeException class and its subclasses, such as NullPointerException (null pointer exception), IndexOutOfBoundsException (subscript out-of-bounds exception), etc. These exceptions are unchecked exceptions. You can choose to capture and process it in the program or not.

These exceptions are generally caused by program logic errors, and the program should try to avoid the occurrence of such exceptions from a logical perspective. The characteristic of runtime exceptions is that the Java compiler will not check it. That is to say, when this type of exception may occur in the program, even if it is not caught with a try-catch statement or declared to be thrown with a throws clause, it will Compiled and passed.

Non-runtime exception (compilation exception): is an exception other than RuntimeException, and all types belong to the Exception class and its subclasses.

From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program will not be compiled. Such as IOException, SQLException, etc. and user-defined Exceptions. Generally, no custom checked exceptions are required.

The above is the detailed content of Exceptions and errors 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

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 Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment