• 异常处理


    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

    我喜欢一无所有,这样就只能一步一步的创造世界...
  • 相关阅读:
    《Effective C#中文版:改善C#程序的50种方法》读书笔记
    WPF总结
    定义集合属性(WPF)
    c#只读字段和常量的区别,以及静态构造函数的使用 .
    编程方法的参数
    关于事件的点滴总结
    线程间的消息(或数据)传递
    OOP的感悟
    VS2010快捷键大全----养成良好的习惯
    UTF-8里包括GB2312
  • 原文地址:https://www.cnblogs.com/riordon/p/3865625.html
Copyright © 2020-2023  润新知