Exception handling

  • 1. Java exception class:

Throwable

Both are subclasses of Throwable:

    • 1.Exception (exception): It is an exception that the program itself can handle.
    • 2.Error (error): It is an error that the program cannot handle. These errors indicate that the failure occurred in the virtual machine itself, or when the virtual machine was trying to execute an application, and generally does not require program processing.
    • 3. Checked exceptions (exceptions that the compiler requires must be handled):

      Except for Error, RuntimeException and its subclasses, other Exception classes and their subclasses are all checkable exceptions. The characteristic of this kind of exception is that the Java compiler will check it, that is, when such an exception may occur in the program, either catch it with a try-catch statement, or declare it with a throws clause, otherwise the compilation will not pass. .

    • 4. Unchecked exceptions (exceptions that the compiler does not require to handle): including runtime exceptions (RuntimeException and its subclasses) and errors (Error).

  • 2、throw、throws、try、catch

  • -throw is used to throw exceptions.
    • The throws keyword can declare the exception to be thrown by the method on the method, and then throw the exception object through throw inside the method.
    • try is used to detect whether an exception occurs in the enclosed statement block, if there is an exception, throw an exception and execute the catch statement.
    • cacth is used to catch exceptions thrown from try and handle them.
    • The finally block is what is executed regardless of whether there is an exception or not.
  • 3. Exception capture in java uses try{}catch{}finally{}. The try block is the code block where exceptions may occur, the catch block is the processing code block for possible exceptions, and finally is executed regardless of whether an exception occurs or not. The code block, try and catch are essential, finally is not.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325068512&siteId=291194637