java自学笔记8:异常简介

一.

异常简介:
异常处理的作用
java异常体系结构简介
处理异常
try-catch以及try-catch-finally
抛出异常
自定义异常
异常链

有异于常态,和正常情况不一样,有错误出现。
阻止当前方法或作用域,称之为异常

Throwable:Error Exception

Error:虚拟机错误(VirtualMachineError),线程死锁(ThreadDeath)

Exception:编码,环境,用户操作输入出现问题 非检查异常(RuntimeException) 检查异常

这里写图片描述

这里写图片描述

二.Java中使用try-catch-finally实现异常处理

try-catch:
try{
//一些会抛出异常的方法
//终止执行,程序的控制权将交给catch块中的异常处理程序
}catch(Exception e){
//处理该异常的代码块
}

这里写图片描述

使用多个catch块处理多种异常

这里写图片描述

编写try-catch语句块时,要注意先小后大,先子类后父类的

这里写图片描述

使用try-catch-finaly

这里写图片描述

三.通过案例学习使用

package com.vishuo.www;

public class TryCatchTest {
    public static void main(String[] args) {
       TryCatchTest tct = new TryCatchTest();
       int result = tct.test();
       System.out.println("test()方法,执行完毕!返回值为:"+result);

       int result2 = tct.test2();
       System.out.println("我想大声告诉你!test2执行完毕!!"+result2);
    }

    /*
     * divider(除数) result(结果) try-catch捕获while循环
     * 每次循环,divider减一,result=result+100/divider
     * 如果:捕获异常,打印输出“抛出异常了!!!”,返回-1
     * 否则:返回result
     */
    public int test() {
        int divider = 10;
        int result  =100;
        try{
            while(divider >-1){
                divider--;
                result = result +100/divider;
            }
            return result;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("循环抛出异常了!!!");
            return -1;
        }
    }

    /*
     * divider(除数) result(结果) try-catch捕获while循环
     * 每次循环,divider减一,result=result+100/divider
     * 如果:捕获异常,打印输出“抛出异常了!!!”,返回 result=999;
     * 否则:返回result
     * finally:打印输出:“这是finally!!哈哈!!”,同时打印输出result的值
     */

    public int test2(){
        int divider = 10;
        int result  =100;
        try{
            while(divider >-1){
                divider--;
                result = result +100/divider;
            }
            return result;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("循环抛出异常了!!!");
            return result =999;
        }finally{
            System.out.println("这是finally!!哈哈!!");
            System.out.println("我是result!!我的值是:"+result);
        }
    }
}

输出结果:

    java.lang.ArithmeticException: / by zero
    at com.vishuo.www.TryCatchTest.test(TryCatchTest.java:25)
    at com.vishuo.www.TryCatchTest.main(TryCatchTest.java:6)
循环抛出异常了!!!java.lang.ArithmeticException: / by zero
    at com.vishuo.www.TryCatchTest.test2(TryCatchTest.java:49)
test()方法,执行完毕!返回值为:-1

    at com.vishuo.www.TryCatchTest.main(TryCatchTest.java:9)
循环抛出异常了!!!
这是finally!!哈哈!!
我是result!!我的值是:999
我想大声告诉你!test2执行完毕!!999

四.java中的异常抛出以及自定义异常

throw:将产生的异常 抛出(动作)
throws:声明将要抛出何种类型的异常(声明)
public void 方法名(参数列表) throws 异常列表{
//调用会抛出异常的方法或者:throw new Exception();
}

这里写图片描述

这里写图片描述

虽然java标准类库中提供了很丰厚的异常种类,但是难免会遇到实际业务中需要用到一些java类库中没有的异常种类,所以需要自定义异常概念

class 自定义异常类 extends 异常类型{
}

自定义异常可以继承自java自带的异常库,直接继承父类Exception或者其子类

package com.vishuo.www;

public class DrunkException extends Exception{

    public DrunkException(){

    }

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

五.java中的异常链

代码演示:

package com.vishuo.www;

public class ChainTest {

    /*
     * test1():抛出“喝大了”异常 test2():调用test1(),捕获“喝大了“异常,并且包装成运行时异常,继续抛出 main方法中,调用
     * test2(),尝试捕获test2()方法抛出的异常
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ChainTest ct = new ChainTest();
        try {
            ct.test2();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public void test1() throws DrunkException {
        throw new DrunkException("喝车别开酒");
    }

    public void test2() {
        try {
            test1();
        } catch (Exception e) {
            // TODO: handle exception
            //RuntimeException newExc = new RuntimeException("司机一滴酒,亲人两行泪");
            //newExc.initCause(e);
            //简单点的方式:
            RuntimeException newExc = new RuntimeException(e);
            throw newExc;
        }
    }
}

六.经验总结

1.处理运行时异常时,采用逻辑去合理规避同事辅助try-catch处理
2.在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常
3.对于不确定的代码,也可以加上try-catch,处理潜在的异常
4.尽量去处理异常,切忌只是简单的调用printStackTrace()去打印输出异常
5.具体如何处理异常,要根据不同的业务需求和异常类型去决定
6.尽量添加finally语句块去释放占用的资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值