laitimes

Briefly talk about the principle of Java exceptions

author:Not bald programmer
Briefly talk about the principle of Java exceptions
The exception mechanism refers to how the program handles errors when they occur. Specifically, the exception mechanism provides a safe channel for the program to exit. When an error occurs, the process of program execution is changed, and control of the program is transferred to the exception handler.

There are three types of program errors:

  • Compilation error
  • Runtime error
  • Logical errors

(1) Compilation error is because the program does not follow the syntax rules, and the compiler can find and prompt us the cause and location of the error by itself, which is also the most common problem that everyone encounters when they are new to programming languages.

(2) The runtime error is caused by the fact that the runtime environment finds an operation that cannot be performed while the program is being executed.

(3) Logical errors are caused because the program is not executed in the expected logical order. An exception is an error that occurs when a program is running, and exception handling is to handle and control these errors.

1 Anomaly classification

Briefly talk about the principle of Java exceptions

In Java, there are two types of exceptions: checked and unchecked (i.e., exceptions that must be caught and exceptions that don't have to). By default, all exceptions must be caught.

2 Anomalous Principles

Code that uses exceptions:

class ExceptionExampleOriginal {

    public static void main(String[] args) {
        System.out.println("main 方法开始");
        try {
            System.out.println("main 调用前");

            method1();

            System.out.println("main 调用后");
        }
        catch (RuntimeException e) {
            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("main 方法结束");
    }

    public static void method1() {
        System.out.println("method1 开始");
        method2();
        System.out.println("method1 结束");
    }

    public static void method2() {
      System.out.println("method2");
      String s = "消息:未知异常";
    throw new RuntimeException(s);

    }
}           

A rough representation of the principle:

public class ExceptionExample {


    private static Exception exception = null;

   public static void main(String[] args) {
        System.out.println("main 方法开始");

        System.out.println("main 调用前");

        method1();

        if (exception == null) {
            System.out.println("main 调用后");
        }
        else if (exception instanceof RuntimeException) {
            RuntimeException e = (RuntimeException) exception;
            exception = null;
            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("main 方法结束");
    }

    public static void method1() {
        System.out.println("method1 开始");
        method2();
        if (exception != null) return;
        System.out.println("method1 结束");
    }

    public static void method2() {
        System.out.println("method2");
        String s = "消息:未知异常";
        exception = new RuntimeException(s);
        return;
    }
}           

In the first example, a number of methods are called in turn. In method2, an exception is deliberately created and thrown (creating an error). ”

“ method2。 Instead of creating an exception, you create a RuntimeException object, save it as a static variable exception, and then exit the method immediately using a return statement. ”

"In method1, after calling method2, we check for exceptions. If there is an exception, method1 ends immediately. In Java, after each method call, such a check is performed indirectly. ”

"In the second instance, using the main method shows roughly what happens when an exception is caught using a try-catch structure. If there are no anomalies, then everything will continue to work as normal. If there is an exception and it is of the same type as specified in the catch statement, it will be processed. ”