java try catch

来源:互联网 发布:淘宝卖家评论 编辑:程序博客网 时间:2024/05/24 05:08
public class AAA {  
 
    public static void main(String[] args) {  
         System.out.println("=============test0==================");  
         System.out.println(test0());  
         System.out.println("===============================");  
         
         System.out.println("=============test0_1==================");  
         System.out.println(test0_1());  
         System.out.println("===============================");  
         System.out.println("=============test0_2==================");  
         System.out.println(test0_2());  
         System.out.println("===============================");  
         
        System.out.println("=============test1==================");  
        System.out.println(test1());  
        System.out.println("===============================");  
 
        System.out.println("=============test1_1==================");  
        System.out.println(test1_1());  
        System.out.println("===============================");  
 
        System.out.println("\n============test2===================");  
        System.out.println(test2());  
        System.out.println("===============================");  
 
        System.out.println("\n============test2_1===================");  
        System.out.println(test2_1());  
        System.out.println("===============================");  
 
        System.out.println("\n============test3===================");  
        System.out.println(test3());  
        System.out.println("===============================");  
 
        System.out.println("\n============test3_1===================");  
        System.out.println(test3_1());  
        System.out.println("===============================");  
    }  
 
    public static String test0() {  
        String a;  
        int b=0;  
        try{  
            b = 8/0;  //(1)  ------0不能当被除数,此行出现异常,try里面的return不会执行
            return "try";    //=========================没有执行======================================
        }catch(Exception e){  
            e.printStackTrace();//(2) //此时程序不会终止,还会继续往下执行
        }  
        a = String.valueOf(b);  //(3)
        return a+b;  //(4)
    }  
    
    
    //try和catch中都有了return
    public static String test0_1() {  
        String a;  
        int b;  
        try{  
            b = 8/0;    //(1) 0不能当被除数,此行出现异常,try里面的return不会执行
            a = String.valueOf(b);  //===================不执行
            return a+b;             //===================不执行
        }catch(Exception e){
            e.printStackTrace(); //(2)
            return "test0_1 出错了";  //(3)
        }  
       // String s="zhaohao";             //try和catch中都有了return,此处加 任何东西  会报错     --------不让加,报错
       // System.out.println("哈哈 ,赵皓");  //try和catch中都有了return,此处加 任何东西  会报错    --------不让加,报错
       //return a+b;                     //try和catch中都有了return,此处加return会报错        --------不让加,报错
    }  
      
    public static String test0_2() {  
        String a;  
        int b=0;  
        try{  
            b = 8/0;  
        }catch(Exception e){  
            b=100;
        }  
        a = String.valueOf(b);     //会执行此处=====================
        return a;                  //会执行此处=====================
    }  
      
    public static int test1() {  
        int m = 3;
        int n = 2;
        int  a = 7;  
        int  b = 8;
        
        int s =0;
        try{
            s=m+n; //(1)
            return s; //(2)先执行这个===(此时s的值已经确认,finally并不会修改它),====再执行finally  ===================(5)结束了,此时才会真正的返回该值
        } catch ( Exception e ) {  
 
        } finally {  
            //对int的更改均无效  
           s=a+b;   //(3)
           System.out.println("do finally 哈哈");  //(4)
        }  
        return s; //==============不会执行===========  
    }
    
   //执行结果:
   // do finally 哈哈
   // 5
    
    
    
 
    public static String test1_1() {  
        String a = "in try中";  
 
        try{  
            return a;  
        } catch ( Exception e ) {  
 
        } finally { //从eclpise报警告可看出,finally里面不建议有return语句  
            a = "in finally中的return的值";  
            System.out.println("test1_1 中的  do finally");  
            return a; //注释掉这句,eclipse将不再警告  
        }  
    }  
 
    //返回结果:
    //test1_1 中的  do finally
    //in finally中的return的值
    
    
    
    public static int test2() {  
        int a = 1;  
 
        try{  
            return a;  
        } catch ( Exception e ) {  
 
        } finally {  
            a = 2;  
            System.out.println("do finally");  
        }  
 
        return a;  
    } //很显然,finally里面更改无效,返回的是a=1  
 
    public static int test2_1() {  
        int a = 1;  
 
        try{  
            return a;  
        } catch ( Exception e ) {  
 
        } finally {  
            a = 2;  
            System.out.println("do finally");  
            return a;  
        }  
    } //很显然,a取finally里面的值,a=2  
 
    //Helper类,将整数转换成字符串  
    static class Helper {  
        int a;  
 
        public String toString() {  
            return String.valueOf(a);  
        }  
    }  
      
    public static Helper test3() {  
        Helper h = new Helper();  
        h.a = 1;  
 
        try{  
            return h;  
        } catch ( Exception e ) {  
 
        } finally {  
            h.a = 2; //对h.a的更改起作用!!  
                    //因为在try里面返回的是一个句柄,它指向的对象的内容 是可以改变的  
            System.out.println("do finally");  
        }  
          
        return h; //这个不会被执行  
    }  
 
    public static Helper test3_1() {  
        Helper h = new Helper();  
        h.a = 1;  
 
        try{  
            return h;  
        } catch ( Exception e ) {  
 
        } finally {  
            h.a = 2; //返回a=2,这个不用说了  
            System.out.println("do finally");  
            return h;  
        }  
    }  
 
 
    /**
     * 总结:
     * return语句,finally里面不建议放return语句,根据需要,可以放在try和catch里面
     *  
     */  
      

0 0
原创粉丝点击