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

    我喜欢一无所有,这样就只能一步一步的创造世界...
  • 相关阅读:
    第一次c++团队合作项目第三篇随笔
    第一次c++团队合作项目第二篇随笔
    第一次c++团队合作作业期间第一篇随笔
    电梯调度程序
    给我留下深刻印象的三位老师
    一个带有富文本功能的记事本
    我也忘了第几次团队作业
    关于复数输入输出的一点见解
    c++团队作业工作笔记
    又要开始新的征程了hhh(这次内容比较感兴趣)
  • 原文地址:https://www.cnblogs.com/riordon/p/3865625.html
Copyright © 2011-2022 走看看