Home > Java > javaTutorial > body text

Explanation of exceptions and exception handling in java (pictures and text)

不言
Release: 2018-09-18 17:13:48
Original
1742 people have browsed it

This article brings you an explanation (pictures and text) about exceptions and exception handling in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

  1. Overview: Exceptions are errors that occur during the running of a Java program.

  2. How to handle exceptions when they occur? Java provides a solution: exception handling mechanism.

  3. Exceptions in Java can be caused when statements in a function are executed, or they can be thrown manually by the programmer through the throw statement.

  4. As long as an exception occurs in a Java program, an exception object of the corresponding type will be used to encapsulate the exception, and JRE will try to find an exception handler to handle the exception.

  5. There are some common exception classes built-in in JDK, and we can also customize exceptions.

  6. The Throwable class is the top-level parent class of Java exception types. Only if an object is a direct or indirect instance of the Throwable class, it is an exception object and can be recognized by the exception handling mechanism.

Exception classification

Check exception:

(1) The most representative check exception is an exception caused by user error or problem, This is something that programmers cannot predict.
(2) For example, when you want to open a file that does not exist, an exception occurs. These exceptions cannot be simply ignored during compilation.

Run-time exceptions:

(1) Run-time exceptions are exceptions that may be avoided by programmers.
(2) Contrary to checked exceptions, runtime exceptions can be ignored at compile time.

Error:

(1) The error is not an exception, but a problem out of the programmer's control.
(2) Errors are usually ignored in the code.
(3) For example, when the stack overflows, an error occurs, and they cannot be checked at compile time.

Exception classification diagram:

Explanation of exceptions and exception handling in java (pictures and text)

Error and Exception classes

Java standard library is built-in Some common exceptions, these classes have Throwable as the top-level parent class.

Throwable derives the Error class and Exception class.

Error class:

(1) Instances of the Error class and its subclasses represent errors in the JVM itself.
(2) Errors cannot be handled by programmers through code, and Errors rarely occur.
(3) Therefore, programmers should pay attention to the various exception classes under the branch where Exception is the parent class

Exception class:

(1) Exception and its subclasses represent Various unexpected events sent when the program is running.
(2) can be used by the java exception handling mechanism and is the core of exception handling.

Exception handling

The exception handling mechanism allows the program to handle exceptions in a targeted manner according to the exception handling logic preset by the code when an exception occurs, so that the program can return to normal as much as possible and Continue execution and keep the code clear.

In Java, the task of exception handling is to transfer the execution control flow from the place where the exception occurs to a place that can handle this exception.

When writing code to handle exceptions, there are two ways to handle checked exceptions:

(1) Use try...catch...finally statement block processing
(2 ) Use the throws statement in the function signature to hand it over to the function caller caller to solve the

try...catch...finally statement block:

(1) try Put code that may cause exceptions in the block;
(2) If the try is executed and no exception occurs, then execute the finally block code and the code after finally;
(3) If an exception occurs while executing the code in try , will try to match the catch block;
(4) Each catch block is used to handle an exception;
(5) Exception matching is searched from top to bottom in the order of the catch blocks, only the first one will The matched catch block will be executed.
(6) Local variables in the try block, local variables in the catch block, and local variables in finally cannot be shared with each other;
(7) Regardless of whether an exception occurs in the finally block, as long as the corresponding If the try block is executed, it will definitely be executed.
(8) The finally block is usually used for resource release operations, closing files or closing databases, etc.

throws/throw:

(1) If a method does not catch a checked exception, then the method must be declared using the throws keyword.
(2) The throws keyword is placed at the end of the method signature.
(3) throws only declares the exceptions that may occur in the function to the caller, but does not handle the exceptions itself.
(4) The reason for using this kind of exception handling may be that the method itself does not know how to handle this exception, or that it is better for the caller to handle it, and the caller needs to be responsible for the exception that may occur.
(5) You can also use the throw keyword to manually throw an exception, whether it is newly instantiated or just caught.
(6) The throw statement must be followed by an exception object.

The above is the detailed content of Explanation of exceptions and exception handling in java (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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