java异常处理

捕获异常:
使用try…catch…finally…
try中放置可能出现异常的语句
catch中放置出现异常时做的处理
finally中防止不论异常是否出现都要做的处理

eg:

public class demo1 {
    public static void test(){
        String s1 = "1234a";
        try{
            int i1 = Integer.parseInt(s1);
        }catch(NumberFormatException e){
            e.printStackTrace();
        }catch(NullPointerException e){
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        test();
    }
}

运行结果:

java.lang.NumberFormatException: For input string: "1234a"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at com.java1234.chap04.demo1.test(demo1.java:7)
    at com.java1234.chap04.demo1.main(demo1.java:20)
finally

注意:如需写多个类型的异常处理catch,则异常处理类型需按从小到大写,最后一个才是Exception类。

throw和throws关键字:
throws:当前方法不处理异常,而是交给方法的调用进行处理
throw :直接抛出一个异常

eg:

public class demo2 {

    /**
     * 将异常向外抛出(方法的调用)
     * @throws NumberFormatException
     */
    public static void testThrows() throws NumberFormatException{
        String s1 = "1234a";
        int i1 = Integer.parseInt(s1);
        System.out.println(i1);
    }

    /**
     * 直接抛出异常
     * @param a
     * @throws Exception
     */
    public static void testThrow(int a) throws Exception{
        if(a==1){
            throw new Exception("Exception");
        }
        System.out.println(a);
    }


    public static void main(String[] args) {
        try{
            testThrows();
        }catch(Exception e){
            e.printStackTrace();
        }

        try {
            testThrow(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

java.lang.NumberFormatException: For input string: "1234a"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at com.java1234.chap04.demo2.testThrows(demo2.java:11)
    at com.java1234.chap04.demo2.main(demo2.java:30)
java.lang.Exception: Exception
    at com.java1234.chap04.demo2.testThrow(demo2.java:22)
    at com.java1234.chap04.demo2.main(demo2.java:36)

Exception和RuntimeException

Exception是检查型异常,在程序中必须使用try…catch…进行处理,否则会出现编译错误

RuntimeException是非检查型异常,可以不使用try…catch…进行处理,不会出现编译错误,但是如果产生异常,则异常将由JVM进行处理,最好也用try…catch…捕获

自定义异常
在业务开发过程中,我们可以自定义一套关于业务性的异常体系,来满足程序的开发需求,自定义异常要继承自Exception

eg:
自定义异常类:

public class ExceptionExample extends Exception{
    /**
     * 重写Exception父类的方法
     * @param message
     */
    public ExceptionExample(String message) {
        super(message);
    }
}

测试类:

public class TestException {

    public static void test() throws ExceptionExample{
        throw new ExceptionExample("出现自定义异常");
    }

    public static void main(String[] args) {
        try {
            test();
        } catch (ExceptionExample e) {
            e.printStackTrace();
        }
    }
}

运行结果:

com.java1234.chap04.ExceptionExample: 出现自定义异常
    at com.java1234.chap04.TestException.test(TestException.java:6)
    at com.java1234.chap04.TestException.main(TestException.java:11)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值