java exception and exception handling

abnormal

Exception (exception): An exception occurs in the code sequence during operation.

The types of exceptions are
Insert picture description here

1. Try-catch structure

Use try and catch keywords to catch exceptions. The try/catch code block is placed where the exception may occur.

The code in the try/catch code block is called protection code. The syntax for using try/catch is as follows:

try
{
    
    
   // 程序代码
}catch(ExceptionName e1)
{
    
    
   //Catch 块
}

E.g

public static void main(String[] args) {
    
    
        try{
    
    
            int num = 5/0;
            System.out.println(num);
        }catch (ArithmeticException e){
    
    
            System.out.println("除数不能为零");
        }finally {
    
    
            System.out.println("回收代码资源");
        }
        try{
    
    
            String string = null;
            System.out.println(string.length());
        }catch (NullPointerException e){
    
    
            System.out.println("数值不能为空");
        }finally {
    
    
            System.out.println("回收代码资源");
        }
        System.out.println("程序继续执行");
    }

2. Multiple try-catch structure

The situation where a try code block is followed by multiple catch code blocks is called multi-catch.

try{
    
    
   // 程序代码
}catch(异常类型1 异常的变量名1){
    
    
  // 程序代码
}catch(异常类型2 异常的变量名2){
    
    
  // 程序代码
}catch(异常类型3 异常的变量名3){
    
    
  // 程序代码
}

E.g

try {
    
    
    file = new FileInputStream(fileName);
    x = (byte) file.read();
} catch(FileNotFoundException f) {
    
     // Not valid!
    f.printStackTrace();
    return -1;
} catch(IOException i) {
    
    
    i.printStackTrace();
    return -1;
}

3. Throws/throw keywords

If a method does not catch a checked exception, then the method must be declared using the throws keyword. The throws keyword is placed at the end of the method signature.

You can also use the throw keyword to throw an exception, whether it is newly instantiated or just caught.

E.g

import java.io.*;
public class className
{
    
    
  public void deposit(double amount) throws RemoteException
  {
    
    
    // Method implementation
    throw new RemoteException();
  }
  //Remainder of class definition
}
import java.io.*;
public class className
{
    
    
   public void withdraw(double amount) throws RemoteException,
                              InsufficientFundsException
   {
    
    
       // Method implementation
   }
   //Remainder of class definition
}

4. Finally keyword

The finally keyword is used to create a code block that is executed after the try code block.

Regardless of whether an exception occurs, the code in the finally code block will always be executed.

In the finally code block, you can run cleanup types and other finishing statements.

public class ExcepTest{
    
    
  public static void main(String args[]){
    
    
    int a[] = new int[2];
    try{
    
    
       System.out.println("Access element three :" + a[3]);
    }catch(ArrayIndexOutOfBoundsException e){
    
    
       System.out.println("Exception thrown  :" + e);
    }
    finally{
    
    
       a[0] = 6;
       System.out.println("First element value: " +a[0]);
       System.out.println("The finally statement is executed");
    }
  }
}

Finally, you can think of it as recycling resources.

5. Summary

  1. Errors that occur during operation are called exceptions;
  2. Java uses try, catch, throw, throws and finally to handle Java exceptions;
  3. The code to be monitored is written in the try block, the code used to catch and handle exceptions is written in the catch block, and finally the code that must be executed is placed;
  4. To manually raise an exception, you can use the keyword throw. Any exception thrown outside the method must be specified with the throws clause.

Guess you like

Origin blog.csdn.net/s001125/article/details/110489235