Java中throws..throw的使用与说明

throws语句   

throws总是出现在一个函数头中,用来标明该成员函数可能抛出的各种异常。对大多数Exception子类来说,Java 编译器会强迫你声明在一个成员函数中抛出的异常的类型。如果异常的类型是Error或 RuntimeException, 或它们的子类,这个规则不起作用, 因为这在程序的正常部分中是不期待出现的。 如果你想明确地抛出一个RuntimeException,你必须用throws语句来声明它的类型。  

throw语句   

throw总是出现在函数体中,用来抛出一个异常。程序会在throw语句后立即终止,它后面的语句执行不到,然后在包含它的所有try块中(可能在上层调用函数中)从里向外寻找含有与其匹配的catch子句的try块。

以下为简单使用代码实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

package net.form1;

public class Throws {

    public static void main(String[] args) {

        Throws thr = new Throws();

        thr.compute();

    }

     

    //定义可能会抛出异常的方法

    //如果调用该方法,需要使用 try..catch处理异常

    public void divider(int b,int c)throws Exception{

        if(b == 0 || c == 0){

            throw new Exception("除数不能为0");//抛出一个异常

        }else{

            System.out.println("相数相除的结果为:"+ b / c);

        }

    }

     

    //可以处理异常的情况

    public void compute(){

        try{

            this.divider(0, 3);

        }catch (Exception e) {

            //捕获divider方法出现的异常

            System.out.println(e.getMessage());

        }

    }

     

    //处理不了异常的情况

    public void noset()throws Exception{

        //...该方法如果处理不了异常,在次申明 throws ,将由调用noset()的方法处理异常

        this.divider(0, 3);

    }

     

}

自定义异常类

1

2

3

4

5

6

7

8

9

10

11

12

package co.form1;

public class DrunkException extends Exception {

    public DrunkException(){

         

    }

     

    public DrunkException(String message){

        super(message);

    }

}

使用自定义异常类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package cn.form1;

public class ChainTest {

    /**

     * test1():抛出“喝大了”异常

     * test2():调用test1(),捕获“喝大了”异常,并且包装成运行时异常,继续抛出

     * main方法中,调用test2(),尝试捕获test2()方法抛出的异常

     */

    public static void main(String[] args) {

        ChainTest ct = new ChainTest();

        try{

            ct.test2();

        }catch(Exception e){

            e.printStackTrace();

        }

    }

    public void test1() throws DrunkException{

        throw new DrunkException("喝车别开酒!");

    }

     

    public void test2(){

        try {

            test1();

        catch (DrunkException e) {

            // TODO Auto-generated catch block

            RuntimeException newExc = 

                new RuntimeException(e);

//          newExc.initCause(e);

            throw newExc;

        }

    }

}

Exception:在程序中必须使用try...catch进行处理。

RuntimeException:可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理。

对于RuntimeException的子类最好也使用异常处理机制。虽然RuntimeException的异常可以不使用try...catch进行处理,但是如果一旦发生异常,则肯定会导致程序中断执行,所以,为了保证程序再出错后依然可以执行,在开发代码时最好使用try...catch的异常处理机制进行处理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程工人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值