Exception: Some exceptions are caused by user errors, some are caused by program errors, and others are caused by physical errors.
Exception handling keywords: try, catch, finally, throw, throws
Notes:
Abnormal classification:
grammar:
try{//The code block that needs to be listened to} catch (exception type exception name /e) {//handle the code block that catches the error that the try listens to throw throw exception name /e; //thorw means throwing exception throw new exception type ("custom");} Finally{//The statements in the finally block will be executed regardless of whether the exception occurs or not}Modifier return value method name() throws Exception type{ //throws is only used to declare exceptions, and whether to throw is determined by the method caller//code block}Code example: (try and catch and finally)
public class ExceptionTest {public static void main(String[] args) {Scanner input=new Scanner(System.in); try{ // Listen to the code block int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ System.out.println("Only enter numbers"); } catch(ArithmeticException e){ System.out.println("The denominator cannot be 0"); } catch(Exception e){ //Exception is the parent class System.out.println("Other exceptions have occurred"); } finally{ // Regardless of whether an exception occurs, System.out.println("Program end"); } }}Code example: (throw keyword)
import java.util.InputMismatchException;import java.util.Scanner;public class ExceptionTest {public static void main(String[] args) {Scanner input=new Scanner(System.in); try{ //listen code block int a=input.nextInt(); int b=input.nextInt(); double sum=a/b; System.out.println(sum); } catch(InputMismatchException e){ //catch (Exception name of exception type) System.out.println("Only enter numbers"); throw e; //Top the exception caught by catch//throw new InputMismatchException(); Same as above} catch(ArithmeticException e){ System.out.println("The denominator cannot be 0"); throw new ArithmeticException("The denominator is 0 throws an exception"); //Top the ArithmeticException exception} catch(Exception e){ //Exception is the parent class of all exceptions System.out.println("Other exceptions have occurred"); } finally{ //No matter whether there is an exception or not, finally will be executed System.out.println("Program End"); } }}Code example: (throws)
public class Throws {int a=1;int b=0;public void out() throws ArithmeticException{ //Declare the exception that may be thrown, there may be multiple exceptions separated by commas{ //Supert code block int sum=a/b;System.out.println(sum);}catch(ArithmeticException e){System.out.println("Denominator cannot be 0");} finally{ //No matter whether an exception occurs, it will be executed finally System.out.println("Program end");}}public static void main(String[] args){Throws t=new Throws();t.out(); //Call the method throw new ArithmeticException("The denominator is 0 and throws an exception"); //Does the called method decide whether to throw an exception/* * The second throw method*///ArithmeticException a=new ArithmeticException("The denominator is 0 and throws an exception"); //throw a;}}