Home > Java > javaTutorial > Java exception handling graphic examples

Java exception handling graphic examples

零下一度
Release: 2017-07-23 10:34:14
Original
1321 people have browsed it

Java Exception Handling

Exceptions are some errors in the program, but not all errors are exceptions, and errors can sometimes be avoided.

For example, if your code is missing a semicolon, the result will be an error java.lang.Error; if you use System.out.println(11/0), then you are because If you divide by 0, a java.lang.ArithmeticException will be thrown.

There are many reasons for exceptions, which usually include the following categories:

The user entered illegal data.

The file to be opened does not exist.

The connection is interrupted during network communication, or the JVM memory overflows.

Some of these exceptions are caused by user errors, some are caused by program errors, and others are caused by physical errors. -

To understand how Java exception handling works, you need to master the following three types of exceptions:

Checked exceptions: The most representative checked exception is user error Or problems cause exceptions that the programmer could not foresee. For example, when trying to open a file that does not exist, an exception occurs. These exceptions cannot be simply ignored at compile time.

Run-time exceptions: Run-time exceptions are exceptions that may be avoided by programmers. In contrast to checked exceptions, runtime exceptions can be ignored at compile time.

Error: Error is not an exception, but a problem out of the programmer's control. Errors are often ignored in code. For example, when the stack overflows, an error occurs that cannot be checked during compilation.

1.Inheritance relationship

## 2.Error

An error that cannot be handled occurs when the program is running. Once it occurs, the JVM terminates execution.

3.Exception

Exception is an error that occurs when the program is compiled and run. Once it occurs, the JVM will tell the programmer to handle it. If handled properly, the program can still be executed. This is different from Error. Once an Error occurs, it cannot be handled and the program will terminate immediately.
is divided into two types:

  • #Runtime exception: occurs at runtime, RuntimeException class and subclasses. No processing is required during compilation, it occurs during the runtime phase. Common ones include NullPointerException\StringIndexOutOfBounds \ClassCastException \ArrayIndexOutOfBounds.

  • Compile-time exception (checked exception): an exception that must be handled when writing the code. If not handled, compilation will not pass. Compile-time exceptions have handling measures set up during the code compilation stage, so even if an exception occurs, it may not affect the normal operation of the program. Therefore, some systems regard compile-time exceptions as normal situations. Processing, for example, Spring submits normally when a compile-time exception occurs by default. Explicitly throwing exceptions in the program is equivalent to the exceptions that must be handled during the compilation stage, and is a compile-time exception. Common ones include ClassNotFoundException\FileNotFounException\ParseException\SQLException\IOException. The basis for dividing the two types of exceptions is the actual probability of occurrence. The JVM treats exceptions with a high probability of occurrence as checked exceptions, requiring programmers to write code

  • Processing, while the runtime exception JVM believes that the probability of occurrence is very low and does not require programmers to handle it.

4.try...catch...finally...

is used to capture inside the method and handle exceptions.

  • #Put the statement block where an exception may occur after try. When an exception occurs, the try statement block stops executing, jumps to the catch statement block, and the catch statement block executes.

  • After completion, the code following the catch statement block will continue to be executed.
  • The finally statement block will be executed regardless of whether an exception occurs, and the code to close the resource is usually placed in it.

  • 5. Custom exception

Inherit Exception and throw an exception under specified conditions "throw MyException(xxxx )", the purpose is to reflect the program running status in detail.

The above is the detailed content of Java exception handling graphic examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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