zoukankan      html  css  js  c++  java
  • 异常处理

    Java异常类层次结构图:

    示例:

    异常类

    package PackName;
    
    public class CTestException 
    {  
        public CTestException() 
        {  
        }  
      
        public boolean testEx() throws Exception 
        {  
            boolean ret = true;  
            try
            {  
                ret = testEx1();  
            } 
            catch (Exception e)
            {  
                System.out.println("testEx, catch exception");  
                ret = false;  
                throw e;  
            } 
            finally 
            {  
                System.out.println("testEx, finally; return value=" + ret);  
                return ret;  
            }  
        }  
      
        public boolean testEx1() throws Exception
        {  
            boolean ret = true;  
            try
            {  
                ret = testEx2();  
                if (!ret) 
                {  
                    return false;  
                }  
                System.out.println("testEx1, at the end of try");  
                return ret;  
            }
            catch (Exception e)
            {  
                System.out.println("testEx1, catch exception");  
                ret = false;  
                throw e;  
            } 
            finally 
            {  
                System.out.println("testEx1, finally; return value=" + ret);  
                return ret;  
            }  
        }  
      
        public boolean testEx2() throws Exception 
        {  
            boolean ret = true;  
            try {  
                int b = 12;  
                int c;  
                for (int i = 2; i >= -2; i--)
                {  
                    c = b / i;  
                    System.out.println("i=" + i);  
                }  
                return true;  
            } 
            catch (Exception e)
            {  
                System.out.println("testEx2, catch exception");  
                ret = false;  
                throw e;  
            } 
            finally
            {  
                System.out.println("testEx2, finally; return value=" + ret);  
                return ret;  
            }  
        }  
     }  
    

      

    入口函数

    import PackName.CTestException;
    
    public class CVar 
    {
    	public static void main(String args[])
    	{
    		CTestException testException = new CTestException();  
            try
            {  
               testException.testEx(); 
            }
            catch (Exception e)
            {  
                e.printStackTrace();  
            }  
    	}
    }
    

    运行结果

    i=2
    i=1
    testEx2, catch exception
    testEx2, finally; return value=false
    testEx1, finally; return value=false
    testEx, finally; return value=false

    我喜欢一无所有,这样就只能一步一步的创造世界...
  • 相关阅读:
    linux命令大全
    【转载】nginx的五种负载算法
    nginx服务器安装及配置文件详解
    查看linux系统核数
    利用nginx实现分流
    ntohs, ntohl, htons,htonl的比较和详解
    Nginx反向代理之HTTP 请求头中的 X-Forwarded-For
    javascript中 for in 、for 、forEach 、for of 、Object.keys().
    vue里面路由传参的三种方式
    vue中ref在input中详解
  • 原文地址:https://www.cnblogs.com/riordon/p/3865625.html
Copyright © 2011-2022 走看看