An exception is an event that occurs during the execution of a program and interrupts the normal instruction flow of the executing program. To be able to handle runtime errors in a program in a timely and efficient manner, you must use exception classes

Exception classes can improve readability, reliability, and maintainability if used correctly, but can have negative effects if used improperly

Java exception Architecture

Java exceptions force the user to consider the robustness and security of the program. Exception handling should not be used to control the normal flow of a program. Its main function is to catch exceptions that occur during program running and handle them accordingly

All Exception types in Java are subclasses of java.lang.Throwable, divided into two types: Error and Exception

Error

An unhandled error in the program indicates that a serious error has occurred in the running application, and the JVM will terminate the thread if such an exception occurs

Such as:

Exception

  • Runtime exception:

    Such exception implementations are the RuntimeException class and its subclasses

    Runtime exceptions do not have to be try-catch enforced in code

    Such exceptions are usually caused by program logic errors and should be avoided as much as possible

  • Non-runtime exception

    Exception classes that inherit Exception in addition to runtime exceptions

    Non-runtime exceptions must be try-caught in the program. If they are not handled, the program will fail to compile

Custom exception

Custom exceptions need to inherit from the Exception class

public class WarnException extends Exception {

    public WarnException(String message) {
        super(message); }}Copy the code

Testing:

public static void test(a) throws WarnException{
    try {
         int i= 1/0;
    }catch (Exception e){
        throw new WarnException("Custom exception"); }}Copy the code

Exception information:

Exception Handling Process

Catch exception:

try{}catch (Exception e) {
	e.printStackTrace();
} finally{}Copy the code
  • Execution process:

    In a normal try-catch, the execution order is try-finally

    If an exception occurs ina try-catch, the execution order is try-catch-finally

Catch multiple exceptions:

try{}catch(WarnException we){
  
}catch(Exception e){
  
}finally{}Copy the code

A try block followed by multiple catch blocks is called multiple capture

Throw an exception:

public static void test(a) throws WarnException{
    try {
         int i= 1/0;
    }catch (Exception e){
        throw new WarnException("Custom exception"); }}Copy the code

Throws exceptions on methods use the throws keyword. If a method does not catch a checking exception, the method must throw the exception using the throws keyword

Throw an exception within a method using the throw keyword

Common abnormal

Error:

  • NoClassDefFoundError: Class definition error
  • OutOfMemoryError: Memory overflow error
  • StackOverflowError: StackOverflowError
  • UnknownError: Indicates unknown errors

RuntimeException/ RuntimeException:

  • NullPointerException: NullPointerException
  • NumberFormatException: The number format is abnormal
  • ClassCastException: Type conversion exception
  • IndexOutOfBoundsException: index cross-border anomalies

Non-runtime exception:

  • IOException: INDICATES an I/O exception
  • SQLException: SQL exception
  • FileNotFoundException: File found exception
  • ClassNotFoundException: No class exception was found