1.Java异常处理try、catch 和 finally的使用

一、说明 try 、catch 、finally 运行顺序

package com.imooc;
/**
 * 说明 try catch finally 运行顺序
 */
  public class Demo1 {

    public static void main(String[] args) {
      Demo1 demo1 = new Demo1();                
      demo1.test();                                     //-----------第一步:引用test()方法
      System.out.println("回到main方法");                        //----------第六步:回到main程序
    }

    public void test(){
      try{                                           //--------第二步:进入try
        int a = 100;
        int b = 10;
        while(b > -1){
            b--;
            int result = a/b;       //当发生异常(b等于0时),立即跳到 catch 语句块   --------第三步:发生异常
        }
      }catch(Exception e){             //Exception 是所有异常的父类     --------第四步:跳转到catch,进行异常处理
        System.out.println("发生异常,跳到catch");       }finally{          System.out.println("运行到finally");              //----------第五步:
从catch运行到finally
       }      

    }  
   }

运行结果:

发生异常,跳到catch
运行到finally
回到main方法

/**
 * 二、说明 try catch finally 返回值问题
 */

1> 有catch 无 finally ,catch 中有返回值,test()最后有返回值
public class Demo1 {

    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        
        String message = demo1.test();

            System.out.println("返回的信息是:"+message);
    }
    
    public String test(){
        try{
            int a = 100;
            int b = 10;
            while(b>-1){
                b--;
                int result = a/b;          
            }
        }catch(Exception e){                
            return "返回catch";
        }
        return "返回test()程序运行到最后";          //注意:test()方法如果没有这句,会报错
    }

}

运行结果:

返回的信息是:返回catch

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

返回值并不是     "返回test()程序运行到最后"    而是 catch  中的return

------------------------------------------------------------------------------------------------------------------------------------------------------------

注意:return "返回test()程序运行到最后"; 这句必须加上        

//test()方法如果没有这句,会报错如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

This method must return a result of type String

 

2> 有catch 无 finally ,catch 中有返回值,test()最后有返回值

而如果catch 中并没有返回值: 返回内容就是   "返回test()程序运行到最后"  

 

3>有catch ,有 finally ,catch 中有返回值,finally 中有返回值,test()最后没有返回值

package com.imooc; 
public class Demo1 {
    public static void main(String[] args) { 
        Demo1 demo1 = new Demo1(); 
        String message = demo1.test(); 
        System.out.println("返回的信息是:"+message); 
    } 
     public String test(){ 
        try{ 
            int a = 100; 
            int b = 10; 
            while(b>-1){ 
                b--;     
                int result = a/b; 
            } 
        }catch(Exception e){          
            return "返回catch";     
        }finally{ 
            return "返回finally";               //finally  会将 catch 中的返回值替换
        } 
    } 
}     

运行结果:

返回的信息是:返回finally

 

注意,如果有finally,并且其中有return 值,就不能加上这句    return "返回test()程序运行到最后";

会报错,

4> 有catch ,有 finally ,两者中均无返回值,test()最后有返回值,最后返回
test()最后的返回值(在try-catch-finally之外的)

总结:
在一个有返回值的方法中使用try-catch-finally 时:
1. 如果catch 和 finally中均无返回值,则需要在它们(以下的它们就是这个结构之外,test方法内)的外面加上return +(某个值,依据返回类型而定)
2. 如果catch 中有返回值,finally 中无返回值,则需要在它们的外面加上
return +(某个值,依据返回类型而定),而返回内容是 catch 中的返回值
3. 如果catch 中无返回值,finally中有返回值,则它们后面不能再加return (...) 或者别的输出语句,会报错 ,返回内容是finally 中的返回值
4. 如果catch 中无返回值,有finally ,而其中返回值,则它们后面必须再加return (...) ,另外也可再加上别的输出语句,返回内容是 后面加上 的return 中的值





posted @ 2016-05-29 00:13  MatthewBlog  阅读(160)  评论(0)    收藏  举报

页脚