An overview of the exception mechanism

The exception mechanism is what a program does when something goes wrong. Specifically, the exception mechanism provides a safe channel for a program to exit. When an error occurs, the flow of program execution changes and control of the program transfers to the exception handler.

There are three kinds of program errors: 1. 2. A runtime error occurs. 3. The logic is incorrect. (1) Compilation error is because the program does not follow the grammar rules, the compiler can find itself and remind us of the reason and location of the error, this is the most common problem when you are new to programming language. (2) Runtime errors are caused by the runtime environment discovering operations that cannot be performed when the program is executing. (3) Logical errors are caused by the program not executing in the expected logical order. Exceptions are errors that occur when the program is running, and exception handling is to handle and control these errors.

Abnormal classification

In Java, there are two kinds of exceptions: checked and unchecked (that is, exceptions that must be caught and exceptions that need not be caught). By default, all exceptions must be caught.

Exception principle

Code that uses exceptions:

class ExceptionExampleOriginal
{


    public static void main(String[] args)
    {
        System.out.println("Main method starts");
        try
        {
            System.out.println("Before calling Main");

            method1();



            System.out.println("After calling main");
        }
        catch (RuntimeException e)
        {


            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("End of main");
    }

    public static void method1(a)
    {
        System.out.println(Method1 "start");
        method2();

        System.out.println("Method1 end");
    }

    public static void method2(a)
    {
      System.out.println("method2");
      String s = "Message: Unknown exception";
    throw newRuntimeException(s); }}Copy the code

A rough representation of the principle

public class ExceptionExample
{
    private static Exception exception = null;

   public static void main(String[] args)
    {
        System.out.println("Main method starts");


        System.out.println("Before calling Main");

        method1();

        if (exception == null)
        {
            System.out.println("After calling main");
        }
        else if (exception instanceof RuntimeException)
        {
            RuntimeException e = (RuntimeException) exception;
            exception = null;
            String s = e.getMessage();
            System.out.println(s);
        }
        System.out.println("End of main");
    }

    public static void method1(a)
    {
        System.out.println(Method1 "start");
        method2();
        if(exception ! =null) return;
        System.out.println("Method1 end");
    }

    public static void method2(a)
    {
        System.out.println("method2");
        String s = "Message: Unknown exception";
        exception = new RuntimeException(s);
        return; }}Copy the code
“In the first example, several methods are called in turn. inmethod2“Intentionally creates and throws an exception (creates an error).”
method2. The exception is not created, it is createdRuntimeExceptionObject and save it as a static variableexception, and then usereturnStatement immediately exits the method.”
“In themethod1In the callmethod2After that, we checked to see if there was anything abnormal. If there is an exception, thenmethod1End immediately. In Java, this check is performed indirectly after every method call.”
“In the second instance, using the main method shows roughly what happens when an exception is caught using the try-catch structure. If there are no exceptions, everything will continue to work normally. If there is an exception and it is of the same type as specified in the catch statement, it will be handled.”