Getting to know Java exceptions and handling

Java
exceptions are essentially program errors, including errors during compilation and errors during runtime

Errors during compilation include incorrect pairing of parentheses, missing semicolons, incorrectly written keywords

Errors during runtime include calling a method with an empty object reference, array subscript out of bounds, divisor is 0, and normal conversion during type conversion

Exception classification
Throwable
The exception in Java is described by Throwable and its subclasses.

Error

System internal errors and resource exhaustion errors

  • OutOfMemoryError when JVM memory resources are exhausted
  • StackOverFlowError that occurs when the stack overflows
  • Class definition error NoClassDefFoundError

These errors occur in the virtual machine itself or when the virtual machine tries to execute the application. They are outside the control and processing capabilities of the application. Once they occur, the Java virtual machine generally chooses to terminate the thread.

Exception

present in the program itself, can handle

Checked Exception and Unchecked Exception

The Java language specification classifies all exceptions derived from the Error class or the RuntimeException class as checkable exceptions, and the Java compiler will check them. If they are not processed, they cannot be compiled.

RuntimeException

Including NullPointException, ArrayIndexOutOfBoundsException, ArithmeticException and ClassCastException

IO Exception (IOException)
SQL Exception (SQLException)
Exception Handling
Throw Exception + Catch Exception

Five keywords: try, catch, finally, throw, throws

try…catch、try…catch…finally、try…finally

The processing scheme for throwing an exception object:

  1. Include the throw statement through try...catch – throw it yourself and handle it yourself

try{ exception that may occur }catch(exception type exception name (variable)){ code for exception handling }catch(exception type exception name(variable)){ code for exception handling }... [finally{ release resource code; }] Example:









public class TryDemoTwo {
    
    
    public static void main(String[] args) {
    
    
        // TODO Auto-generated method stub
        int result=test();
        System.out.println("one和two的商是:"+ result);
    }
    public static int test(){
    
    
        Scanner input=new Scanner(System.in);
        System.out.println("=====运算开始=====");
        try{
    
    
            System.out.print("请输入第一个整数:");
            int one=input.nextInt();
            System.out.print("请输入第二个整数:");
            int two=input.nextInt();
            return one/two;
        }catch(ArithmeticException e){
    
    
            System.out.println("除数不允许为零");
            return 0;
        }finally{
    
    
            System.out.println("=====运算结束=====");
        //    return -100000;
        }
    }
}

Notice:

  • catch cannot exist independently of try.
  • There cannot be no content in the catch
  • It is not mandatory to add a finally block after the try/catch.
  • There cannot be neither a catch block nor a finally block after the try code.
  • The less try there is, the better.
  • No code can be added between try, catch, finally blocks.
  • The code in finally will eventually be executed (except for JVM exit)
  • If the program may have multiple exceptions, multiple catches are required to catch them.
  • If the exception is at the same level, it doesn’t matter who catches it first.
    If there is a superior-subordinate relationship between exceptions, the superior needs to be placed behind.
  1. Through throws, the method declares the type of exception thrown – who calls and who handles it – the caller can handle it by himself or continue to throw it up. At this time, you can throw the same type or parent class as the throw object, not a subclass
public static void main(String[] args) {
    
    
        try{
    
    
            int result = test();
            System.out.println("one和two的商是:" + result);
        }catch(ArithmeticException e){
    
    
            
        }catch(InputMismatchException e){
    
    
            
        }catch(Exception e){
    
    
            
        }
        int result2=test();
    }
public static int test() throws ArithmeticException,InputMismatchException{
    
    
        Scanner input = new Scanner(System.in);
        System.out.println("=====运算开始=====");
        System.out.print("请输入第一个整数:");
        int one = input.nextInt();
        System.out.print("请输入第二个整数:");
        int two = input.nextInt();
        System.out.println("=====运算结束=====");
        return one / two;
    }

Guess you like

Origin blog.csdn.net/qq_42490860/article/details/114786745