package edu.xcdq;
/**
* @author yuhuicun
* @date 2021/4/27 14:49
*/
public class Demo01 {
public static void main(String[] args) {
int divisor = 10;
int dividend = 0;
//System.out.println(divisor/dividend);//ArithmeticException 算数异常
try{
System.out.println(divisor/dividend);//ArithmeticException 算数异常
}catch (Exception e){
e.printStackTrace();
System.out.println("捕获一个异常");
}
System.out.println("哈哈哈哈");
}
}
Demo01
package edu.xcdq;
import java.util.Scanner;
/**
* @author yuhuicun
* @date 2021/4/27 15:07
*/
public class Demo02 {
public static void main(String[] args) {
int [] a = new int[2];
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int j = scanner.nextInt();
try{
a[0] = i;
a[2] = j;
System.out.println(a[0]/a[2]);
}catch (IndexOutOfBoundsException e){
System.out.println("数组越界异常");
}catch (NumberFormatException e) {
System.out.println("数据格式不正确异常");
}catch (ArithmeticException e ){
System.out.println("算数异常");
}
}
}
Demo02
package edu.xcdq;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @author yuhuicun
* @date 2021/4/27 15:15
*/
public class Demo03 {
public static void main(String[] args) {
int[] a = new int[2];
Scanner scanner = new Scanner(System.in);
try {
int i = scanner.nextInt();
int j = scanner.nextInt();
a[0] = i;
a[2] = j;
System.out.println(a[0] / a[2]);
} catch (ArrayIndexOutOfBoundsException | InputMismatchException | ArithmeticException e) {
System.out.println("数组越界异常");
System.out.println("数据格式不正确异常");
System.out.println("算数异常");
System.out.println("以上异常中的一个");
}
}
}