文档详情

中关村北大青鸟浅谈Java异常.doc

发布:2017-02-05约3.58千字共6页下载文档
文本预览下载声明
中关村北大青鸟:浅谈Java异常 北大青鸟中关村校区学习资料 北大青鸟中关村中心(北京中关永信信息技术有限公司)地处中国硅谷中关村的核心地带,是APTECH北大青鸟体系最优秀的软件工程师培训基地之一,也是我国最知名的软件工程师培训学校之一。办学十年之久,综合实力最牛,师资就业双优,连续五年蝉联北大青鸟体系最高荣誉。被总部授予“北大青鸟教学环境标准示范中心”,成为行业典范。 1.Java异常是一个好东西,能让程序员自定义自己的异常处理程序逻辑。Java定义了一个throwable类,用于描述所以可被抛弃的东西。它有两个子类error和exception。 Error指的是合法程序正常情况下无法去捕获的异常, exception则是合理程序正常下也应该尝试着去捕获的导常。 其中exception包含checkedexception与uncheckedexception两类。后者即runtimeexception异常,比如数组越界IndexOutOfBoundsException,空指针NullPointerException,非法参数状态IllegalStateException等。 2.一谈到java异常,也许你会想到try{}catch(Exp){}finally{}这样子的语法。 Try函数体为于业务处理,finally体将为程序执行到最后,做最后的相关处理,如文件关闭,数据库连接关闭等等。 3.异常链 如果java不存在异常链,我们将无法更新精确地定位到异常的具体位置。下面举个例子 (A异常继承B异常 B异常继续C异常 C异常继承运行时异常) publicclassExceptionA extendsExceptionB { publicExceptionA() { System.out.println(exceptionA Construtor); } publicExceptionA(String msg) { super(msg); } } publicclassExceptionB extendsExceptionC { publicExceptionB() { System.out.println(exceptionB Construtor); } publicExceptionB(String msg) { super(msg); } } publicclassExceptionB extendsExceptionC { publicExceptionB() { System.out.println(exceptionB Construtor); } publicExceptionB(String msg) { super(msg); } } 下面以除0异常为测试例子 publicclass main { /** * @param args * @throwsExceptionC */ publicstaticvoidmain(String[]args)throwsExceptionC { try{ second(); }catch(ExceptionCc){ throw c; } } public staticvoidsecond() throws ExceptionC{ try{ first(); }catch(ExceptionBxx){ throw xx; } } public staticvoidfirst() throws ExceptionB{ try{ inti=10/0; }catch(Exceptionxx){ thrownewExceptionA(); } } } 运行结果 如下图: exceptionCConstrutor exceptionBConstrutor except
显示全部
相似文档