java异常机制 --throws 和 throw 区别

今天在做Exception复习的时候,关于throws e 和 throw e 的区别不是很清楚,查找了很多资料,特别将其整理出来。

throws e

运用在方法标签后面 格式如下:

 public class Math {
    public int  method01(int i,int j)throws Exception{
     int c =i/j;
     return c;      
    }
}

表示将此method01方法中可能存在的exception 抛出异常给调用此方法的方法,当前方法不做异常的处理

此时要注意,当有上级方法调用method01方法时,就一定需要对mehod01中抛出的异常进行处方法处理,否则上级类中调用method01时会报错;
可以在上级的方法标签后添加 throws e,抛出给上上级(处理代码一),或者上级在调用method01方法处进行try-catch(处理代码二)

当方法层层调用时,需要一层层抛出,如果在main方法中抛出thows e 的话,想当于异常交由jvm处理,也相当于没有抛出异常 ,因为程序会在bug处停止,而我们利用异常机制对异常进行抛出,希望能够在控制台查看到错误信息,确不影响程序运行。

处理代码一:

public class Math {
    public int  method01(int i,int j)throws Exception{
    System.out.println("mehod方法开始");
        int c =i/j;
        System.out.println("mehod方法结束");
     return c;      
    }
}
public class TrowsDamo2 {
  public static void main(String[] args)  {
        Math math = new Math();
        System.out.println("运算开始");
try {
    int div2 = math.method01(15, 0);//此时调用Math类的method01方法
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        System.out.println("运算结束");

    }

执行结果

运算开始
/ by zero
mehod方法开始
运算结束

分析:在main方法中调用了Math类中的methdod01方法,并且method01抛出异常,因为上级方法为main方法,交由jvm处理 
method01方法中,“method方法结束”没有输出,在下层方法中,遇到异常,异常后面的代码不再执行 代码二可以解决此问题。

处理代码二: 使用tr-catch 异常将在cathc块中执行,控制台可以输出“动算结束”

public class Math {
    public int  method01(int i,int j)throws Exception{//抛出异常
    System.out.println("mehod方法开始");
        int c=0;
    try {
             c =i/j;
        } catch (Exception e) {
        System.out.println(e.getMessage()+"---method01异常");
        }
        System.out.println("mehod方法结束");
    return c;       
    }
}
public class TrowsDamo2 {
  public static void main(String[] args)  {
        Math math = new Math();
        System.out.println("运算开始");
try {

    int div2 = math.method01(15, 0);//此时调用Math类的method01方法

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        System.out.println("运算结束");
        }
}

输出结果

运算开始
mehod方法开始
/ by zero---method01异常
mehod方法结束
运算结束

分析

method01方法对外抛出异常,但方法内部使用try-catch进行捕捉异常,异常在catch块已处理,不会再对外抛出异常,所以main方法中的try-catch中未捕捉到异常未打印出异常信息
虽然代码二中,实现了在异常后代码的执行,却没有办法实现异常的上抛。


throw e

一般配合try-catch 使用 
格式如下

public class ThisDemo06{
public static void main(String args[]){
try{
throw new Exception("自己抛着玩的。") ;// 抛出异常的实例化对象
}catch(Exception e){
System.out.println(e) ;
}
}

throw e 实现了在try-catch块中对异常的上抛
在catch块中,throw e后不能再跟代码 ,且cathc块外,后面的代码不被执行
以下错误例子

 catch (Exception e) {
        throw e; //下面这句打印会编译报错,throw后不能加指令
     System.out.println(e.getMessage());

    }finally {
        System.out.println("finally块中必然执行");
        //finally块会执行
    }
    System.out.println("此命令不会执行");
    //throw后,命令不执行

ps:Exception与runtimeException区别

观察以下代码:

package methoud;
public class ThisDemo06{
public static void main(String args[]){
String str = "123" ;// 定义字符串,全部由数字组成
int temp = Integer.parseInt(str) ; // 将字符串变为int类型
System.out.println(temp * temp) ;// 计算乘方
}
};  
  public static int parseInt(String s) throws NumberFormatException

此方法明明使用了throws关键字抛出异常,为什么不用处理,也可以编译通过?

在JAVA异常处理机制中,

1)如果抛出的是EXception的类型,则必须进行try ..catch进行处理。

2)如果抛出的是RuntimeException的类型,则可以不使用try。。catch处理,一旦发生异常之后,将由JVM处理。

为了保证程序的健康性,在有可能出现异常的时候还是老实使用try ..catch处理。

 好了,今天就整理到这里了。

猜你喜欢

转载自blog.csdn.net/ted_cs/article/details/82355549