package edu.xcdq;
public class Demo01 {
public static void main(String[] args) {
int divisor = 100;
int dividend = 30;
try {
System.out.println(divisor / dividend);
} catch (Exception e) {
e.printStackTrace();
System.out.println("捕捉到一个异常");
}finally {
System.out.println("不论如何都会执行这里的代码");
}
System.out.println("哈哈哈哈哈哈哈哈哈");
}
}
package edu.xcdq;
import java.util.Scanner;
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("算数异常");
}
}
}
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Demo03 {
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]);
//Array Index Outof Bounds Exception 数组 索引 超出 边界 异常
//Input Mismatch Exception 输入 不匹配 异常
//Arithmetic Exception 数学数字 异常
}catch (IndexOutOfBoundsException| InputMismatchException|ArithmeticException e){
System.out.println("数组越界异常");
System.out.println("数据格式不正确异常");
System.out.println("算数异常");
System.out.println("以上异常中的一个");
}
}
}
package edu.xcdq;
public class Demo04 {
public static void main(String[] args)throws Exception {
/*try{
setSex("变性人");
}catch (Exception e){ //调用者处理异常
e.printStackTrace();
System.out.println("非男非女");
} */
setSex("afsfas");
}
public static void setSex(String sex) throws Exception { //声明异常
if (!(sex.equals("男")||sex.equals("女")) ){
throw new Exception("处理不了的异常,扔出去"); //抛出异常
}
}
}
package edu.xcdq;
public class Demo05 {
public static void main(String[] args){
try{
setSex("双性人");
}catch (Exception e){
System.out.println("调用者说处理过了");
}
}
public static void setSex(System sex)throws SexException{
if (!(sex.equals("男")||sex.equals("女"))){
throw new SexException("发现一个不对劲的");
}
}
}