2021-04-27异常处理

Main

···
package edu.xcdq;

public class Main {

public static void main(String[] args) {
    int divisor = 10 ;
    int dividend = 0 ;
    try{
        System.out.println(divisor / dividend);//ArithmeticException    算数异常
    }catch (Exception e){
        e.printStackTrace();
        System.out.println("捕获到一个异常");
    }finally {
        System.out.println("不管怎都会执行这个代码");
    }


    System.out.println("哈哈");
}

}

···

Demo01

package edu.xcdq;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:07
 */
public class Demo01 {
    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 e){
            System.out.println("数组越界异常");
        }catch (InputMismatchException e){
            System.out.println("数据格式不正确异常");
        }catch (ArithmeticException e){
            System.out.println("算数异常");
        }
    }
}

Demo02

package edu.xcdq;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:15
 */
public class Demo02 {
    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]);
                // Array Index OutOf Bounds Exception 数组、索引 超出 边界 异常
                // Input Mismatch Exception  输入 不匹配 异常
                // Arithmetic Exception 数字数字 异常
            }catch (ArrayIndexOutOfBoundsException |InputMismatchException |ArithmeticException e){
                System.out.println("数组越界异常");

                System.out.println("数据格式不正确异常");

                System.out.println("算数异常");
            }
        }
    }


Demo03

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:31
 */
public class Demo03 {
    public static void main(String[] args) throws Exception{       //继续向上声明,不处理
        /*try {
            steSex("双性人");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非男非女");
        }*/
        steSex("afwarf");
    }
    public static void steSex(String sex) throws Exception{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
           throw new Exception("处理不了的异常,扔出去");  //抛出异常
        }
    }
}

Demo04

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:46
 */
public class Demo04 {
    public static void main(String[] args){
        try {
            steSex("双性人");
        }catch (Exception e){
            System.out.println("调用者说处理过了");
        }
    }
    public static void steSex(String sex) throws SexException{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new SexException("发现一个不对劲的");  //抛出异常
        }
    }
}

SexException

package edu.xcdq;

/**
 * @qvthor liuwenzheng
 * @date 2021/4/27 15:44
 */
public class SexException extends Exception {
    public SexException(){
    }
    public SexException(String message){
        System.out.println(message);
        System.out.println("自定义的异常,知道非男飞女,但是没办法处理");
        System.out.println("..........");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容