Java Exception Mechanism

Simple classification:

  • Check exceptions: The most representative check exceptions are those caused by a user error or problem that the programmer could not have foreseen. For example, an exception occurs when a nonexistent file is opened, and these exceptions cannot simply be ignored at compile time.

  • Runtime exceptions: Runtime exceptions are exceptions that can be avoided by the programmer. In contrast to checkability, runtime exceptions can be ignored at compile time.

  • Errors: Errors are not exceptions, but problems outside the programmer’s control. Errors are often ignored in the code. For example, an error occurs when the stack overflows, and they are not checked during compilation.

Anomalous body structure

Error

  • Error class objects are generated and thrown by the Java virtual Machine, and most errors are independent of what the code writer did.

  • Java Virtual Machine running Error (Virtual MachineError), an OutOFMemoryError that occurs when the JVM no longer has the memory resources needed to continue an operation. When this happens, the Java Virtual Machine (JVM) typically selects thread termination;

  • Others occur when the VIRTUAL machine tries to execute the application, such as class definition error (NoClassDefFoundError), connection error (LinkageError). These errors are untraceable because they are outside the control and processing power of the application and are, for the most part, not allowed to occur at runtime.

Exception

  • There is an important subclass RuntimeException in the Exception branch.

    • ArraylndexOutOfBoundsException (array subscript crossing the line)

    • NullPointerException (NullPointerException)

    • ArithmeticExeception

    • MissingResourceException (missing resource)

    • Such as ClassNotFoundException(class not found), which are no-check exceptions that the program can choose to either catch or not handle

  • These exceptions are usually caused by the program logic error, the program should avoid the occurrence of such exceptions from the logical point of view;

  • The difference between an Error and an Exception: An Error is usually a catastrophic Error that the program cannot control or handle. When an Error occurs, the JVM generally terminates the thread. Exceptions can usually be handled by a program, and should be handled in a program.

Exception handling mechanism

  • 5 keywords to handle exceptions:

Try, catch, finally, throw, throws

  • Catching an exception:

  • Throw an exception:

Throws and throws

  • Throws throws are used after the method declaration, followed by the name of the exception class. Throws are used within the method body, followed by the name of the exception object.

  • Difference 2: Throw can throw only one exception object name, while throws multiple exception class names, separated by commas (,).

  • Difference 3: Throw throws an exception and is handled by a statement in the body of the method, while throws an exception and is handled by the caller of the method.

  • Throws an exception. Throw throws an exception. Throws an exception, but throws an exception.

Custom exception

  • Use Java’s built-in exception classes to describe most of the exceptions that occur during programming. In addition, the user can customize the exception. User – defined Exception class, only need to inherit Exception class.

  • Using custom exception classes in a program can be roughly divided into the following steps:

1. Create a custom exception class.

2. Throw the exception object in the method using the throw keyword.

3. If an exception is handled in the method that threw it, you can use the try-catch statement to catch and handle the exception. Otherwise, use the throws keyword in the method declaration to indicate the exception to be thrown to the method caller, and proceed to the next step.

4. Catch and handle the exception in the call of the exception method.

Summary of experience in practical application!!

  • When handling run-time exceptions, use logic to circumvent and assist with try-catch handling.

  • After multiple catch blocks, a catch (Exception) can be added to handle exceptions that might be missed.

  • For uncertain code, you can also add a try-catch to handle potential exceptions.

  • Handle exceptions as much as possible, and don’t simply call printStackTraca () to print output.

  • How to handle exceptions depends on different business requirements and exception types.

  • Try to add finally blocks to release occupied resources. IO~ Scanner~