Java Exception 异常机制 (1)--throws 和 throw 区别

 

 

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方法中抛出throws 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("运算结束");

    }

执行结果

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

分析:在main方法中调用了Math类中的methdod01方法,并且method01抛出异常,在main方法中进行try-catch,对异常进行处理。(ps:如果此时main方法也对异常进行上抛 thows e 的话,程序会将异常交由jvm处理,程序会报错)
method01方法中,“method方法结束”没有输出,在下层方法中,遇到异常,异常后面的代码不再执行 代码二可以解决此问题。

处理代码二: 

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块中不再对异常进行处理 在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后,命令不执行

特别说明:

  • 当层层调用时,如果是都是使用在方法签名后使用throws抛出异常,下层代码中,任何一层出现bug,会在bug处抛出异常,bug代码后的指令将不会执行。
  • 使用try-catch块中,throw抛出异常,throw后的代码也将不会再执行

以下为例子

    public class Math {
    public int  method01(int i,int j){//抛出异常
    System.out.println("mehod方法开始");
        int c=0;
    try {
             c =i/j;
        } catch (Exception e) {

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

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

    }

}

 

public class TrowsDamo5 {
  public static void main(String[] args)  {

            try {
                new TrowsDamo5().grandSonMethod();

            } catch (Exception e) {
                System.err.println("rer");
            }
        System.err.println("stop");

    }
  public void grandSonMethod() throws Exception{
    Math math = new Math();
        System.out.println("运算开始");

     try {
         int div2 = math.method01(15, 0);//此时调用Math类的method01方法
        } catch (Exception e) {
            System.err.println("imfool");
            throw e;
        }

        System.out.println("运算结束");

  }

}  

 

运算开始
mehod方法开始
/ by zero
finally块中必然执行
imfool
stop

这个代码稍微复杂点,我使用流程图来描述

Java Exception 异常机制 (1)--throws 和 throw 区别_第1张图片

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处理。 

 

Java Exception 异常机制 (1)--throws 和 throw 区别_第2张图片

你可能感兴趣的:(Java,Exception,Exception,throws,throw,throws,throw)