zoukankan      html  css  js  c++  java
  • try finally 执行顺序

    class Exc{
    	int a;
    	int b;
    }
    
    
    public class Except {
    	@SuppressWarnings("finally")
    	static int   compute (){
    		Exc e = new Exc();
    		e.a = 10;
    		e.b = 10;
    		int res = 0 ;
    		try{
    			res = e.a / e.b;
    			System.out.println("try ……");
    			return res + 1;
    			
    		}catch(NullPointerException e1){
    			System.out.println("NullPointerException occured");
    		}catch(ArithmeticException  e1){
    			System.out.println("ArithmeticException occured");
    		}catch(Exception e3){
    			System.out.println("Exception occured");
    		}finally{
    			System.out.println("finnaly occured");
    		}
    		System.out.println(res);
    		
    		return res+3;
    	}
    	
    	public static void main(String[] args){
    		int b =  compute();
    		System.out.println("mian b= "+b);
    	}
    }
    

      输出:

    try ……
    finnaly occured
    mian b= 2

    结论: 如果没有异常, 则执行try 中的代码块,直到 try 中的 return,接着执行 finally 中的代码块,finally 执行完后 , 回到try 中执行 return 。退出函数。

    class Exc{
    	int a;
    	int b;
    }
    
    
    public class Except {
    	@SuppressWarnings("finally")
    	static int   compute (){
    		Exc e = new Exc();
    //		e.a = 10;
    //		e.b = 10;
    		int res = 0 ;
    		try{
    			res = e.a / e.b;
    			System.out.println("try ……");
    			return res + 1;
    			
    		}catch(NullPointerException e1){
    			System.out.println("NullPointerException occured");
    		}catch(ArithmeticException  e1){
    			System.out.println("ArithmeticException occured");
    		}catch(Exception e3){
    			System.out.println("Exception occured");
    		}finally{
    			System.out.println("finnaly occured");
    		}
    		System.out.println(res);
    		
    		return res+3;
    	}
    	
    	public static void main(String[] args){
    		int b =  compute();
    		System.out.println("mian b= "+b);
    	}
    }
    

      输出:

    ArithmeticException occured
    finnaly occured
    0
    mian b= 3

    结论: 如果try 中有异常, 则在异常语句处,跳转到catch 捕获的异常代码块, 执行完 catch 后,再执行 finally ,跳出 try{}catch{}finally{} ,继续向下执行,不会去执行try中 后面的语句。

    public class Test2 {
    	public static void main(String [] args){
    		System.out.println(com());
    	}
    	
    	@SuppressWarnings("finally")
    	static boolean com(){
    		try{
    			return true;
    		}finally{
    			return false;
    		}
    		
    	}
    }
    

      输出 false

    public class Test3 {
        public static void main(String[] args){
            try{
                System.out.println("try……");
                System.exit(0);
            }finally{
                System.out.println("finally……");
            }
        }
    }

    输出: 

    try……  程序退出,不执行 finally

  • 相关阅读:
    01.网页学习阶段、整站分析、规划
    书签搬运
    如何判断两个链表相交及找到第一个相交点
    Windows平台使用git bash管理github中的工程
    二级指针的操作
    结构体的内存对齐
    大端和小端
    剑指Offer——面试题26:复杂链表的复制
    使用editcap命令将ERF格式转换为pcap格式
    如何在STL的map中使用结构体作为键值
  • 原文地址:https://www.cnblogs.com/z360519549/p/7128587.html
Copyright © 2011-2022 走看看