JAVA异常处理

/**
 * @author Endless
 *
 */

public class UseExpection {
    public static void main(String[] args) {
        UseExpection ex = new UseExpection();
        try {
            ex.Ex();
        } catch (Exception e) {
            System.out.println("main, End of Catch");
            e.printStackTrace();
        }
    }

    public UseExpection() {

    }

    @SuppressWarnings("finally")
    boolean Ex() throws Exception {
        boolean ret = false;
        try {
            ret = Ex1();
        } catch (Exception e) {
            e.printStackTrace();
            ret = false;
            System.out.println("Ex,End of Catch  ");
            Exception xxx = new Exception("Ex 233");
            throw xxx;
        } finally {
            System.out.println("Ex, finally; return value=" + ret);
            return ret;
        }

    }

    private boolean Ex1() throws Exception {
        boolean ret = true;
        try {
            ret = Ex2();
            if (!ret) {
                return false;
            }
            System.out.println("Ex1,end of Try");
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Ex1,end of Catch");
            ret = false;
            Exception xxx = new Exception("Ex1 233");
            throw xxx;//注掉finally{}代码块儿将使此语句失效
        }
        // finally {
        // System.out.println("Ex1 ,end of Finally");
        // return ret;
        // }
    }

    private boolean Ex2() throws Exception {
        boolean ret = true;
        try {
            int b = 12;
            int c;
            for (int i = 2; i >= -1; i--) {
                c = b / i;
                System.out.println("i=" + i + "c=" + c);
            }
            return true;
        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println("Ex2,End of Catch");
            ret = false;
            Exception xxx = new Exception("Ex2 233");
            throw xxx;
        }
        // } finally {
        // System.out.println("Ex2, End of Finally");
        // return ret;
        // }
    }
}

这里写图片描述



自定义异常

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

//自定义异常
public class MyScanner {
    private static Scanner in;

    public static void main(String[] args) {

        sss();

    }

    private static void sss() {
        Integer x = 0;
        Integer y = 0;
        in = new Scanner(System.in);
        System.out.println("请输入一个数字");
        try {
            x = in.nextInt();
            System.out.println("请输入一个整数:");
            y = in.nextInt();
            System.out.println("x/y=" + x / y);
        } catch (InputMismatchException e) {
            try {
                throw new InputMismatchException("必须输入一个int值范围的数字");
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        } catch (Exception e) {
            if (y == 0) {
                try {
                    throw new Exception("除数不能为零!");
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
            }
        }
    }
}

这里写图片描述


这里写图片描述


这里写图片描述



public class MyException extends Exception {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        try {
            throw new MyException("自定义异常!");
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    public MyException() {
        super();
    }

    public MyException(String message) {
        super(message);
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值