异常处理及try/catch/finally/return 的执行顺序:

一、
Java分为checked异常(try、catch,自动提醒)和unchecked异常(系统不自动提醒),比较简单,不再详细赘述。
此处说一下几种常见unchecked异常的处理,要手动排查:

1、NullPointException:空指针异常
解决方法:
if(x!=null)
{
…..;
}

2、ArithmeticException:0做除数
解决方法:
if(x!=0)
{
…..;
}

3、ClassCastException:类型转换错误
解决方法:
if(object ob instanceof Car)
{
Car c=(Car)ob;
….;
}

4、ArrayIndexOutOfBoundsException:数组下标越界
解决方法:
if(i

package cn.ldedu;

public class testTryCatch {
    public static void main(String[] args) {
        testTryCatch tt=new testTryCatch();
        System.out.printf("return is  %s",tt.test());
    }

    public String test(){
        try{
            System.out.println("aaa");
            int i=5/0;         //发生异常,跳转到catch
            System.out.println("bbb");
            return "try";
        }catch(ArithmeticException e){
            System.out.println("catch");
//          e.printStackTrace();

            return "catch";
        }finally{
            System.out.println("finally");
            return "finally";
        }
    }
}

截图如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值