`
k_lb
  • 浏览: 802250 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论
  • kitleer: 据我所知,国内有款ETL调度监控工具TaskCTL,支持ket ...
    kettle调度

Exception Rule

阅读更多

Exception

Exception异常处理,是Java的重要特性之一。Exception分为两种,一种是System Exception(系统异常),一种是Application Exception(应用程序异常)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

System Exception(系统异常)继承RuntimeException类,不需要显示的catch或者throw

Application Exception(应用程序异常)继承RuntimeException类之外的Exception,必须显示的catch或者throw

(说明: RuntimeException类是Exception类的子类。)

以下的代码throws所有异常。

void func( …) throws Exception{

}

这样的代码,屏蔽了包括系统异常在内的所有异常。在这样的函数中,调用任何有可能抛出异常的代码,也不会有任何编译错误。而且,调用这个函数的代码必须写成如下:

try{

func(..)

}catch(Exception e){

}

以上的代码能够catch包括系统异常在内的所有异常,一般只出现测试程序中。这种异常处理方法屏蔽了所有异常。建议显示的抛出特定的应用程序异常。代码如下:

void func( …) throws ServletException{

// 如果有其它的异常,必须包装成ServletException 抛出

try{

}catch(SQLException e){

throw new ServletException(e);

}

}

调用这个函数的代码写成如下:

try{

func(..)

}catch(ServletException e){

}

// 如果需要catch包括系统异常在内的所有异常,可以在后面加入如下语句。

catch(Exception e){ }

finally

在涉及到资源释放(比如,数据库资源,文件资源)的函数里,函数的出口数目是一个需要考虑的问题。一般的解决方法是,尽量让这种函数只在函数结束处有一个出口。由于逻辑的复杂性,不可能强求做到这一点。可以使用finally机制来控制。

例如,下面的代码

void func( ..) throws ServletException{

if(...)

return; // won’t go finally block

try{

if(…)

return; // will go finally block

}catch(SQLException e){

throw new ServletException(e): // will go finally block

}catch(IOException e){

return; // will go finally block

}finally{

// release resource

}

}

分享到:
评论

相关推荐

    DROOLS_NPE_rule_extended

    Exception in thread "main" Exception executing consequence for rule "extended" in fr.cea.test: java.lang.NullPointerException at org.drools.core.runtime.rule.impl.DefaultConsequenceExceptionHandler....

    android ndk开发互调示例demo

    android ndk开发互调示例demo:包括java代码调用C,及C调用java。

    p3c-pmd-2.0.0.jar

    rulesets/java/ali-exception.xml,rulesets/java/ali-flowcontrol.xml,rulesets/java/ali-naming.xml,rulesets/java/ali-oop.xml,rulesets/java/ali-other.xml,rulesets/java/ali-set.xml -f text

    Android代码-FragmentTestRule

    Fragment Test Rule Test Fragments in isolation. Download androidTestImplementation '...public void clickButton() throws Exception { onView(withText(R.string.button)).perform(click()); onView(with

    邮件模板中的内联CSS库.zip

    use Symfony\Component\CssSelector\Exception\ExceptionInterface; use TijsVerkoyen\CssToInlineStyles\Css\Processor; use TijsVerkoyen\CssToInlineStyles\Css\Property\Processor as PropertyProcessor;...

    SQL Prompt_9.1.4.4532破解版

    SP-6707 : Rule ST002 no longer detects issue on column_alias=expression syntax. SP-6847 : Added support for WITHIN GROUP order clause on STRING_AGG function. SP-6853 : Fix CTE/Cursor formatting error ...

    SQL Prompt_9.1.3.4467破解版

    Enhancement : Code analysis settings page now displays description for selected rule. SP-6581 : Service broker CONVERSATION_HANDLE and CONVERSATION_GROUP_ID are recognized as reserved keywords. SP-...

    PegaRULES Process Commander 介绍

    through its ability to select the right rule, to research and grab - as needed - the right data, images, and facts based on key factors such as who, what, when, where, and why. In addition, PegaRULES ...

    AcmeRPA.zip

    - If the values are not matching, this should be categorized as a Business Rule Exception, and the queue item should have the status set accordingly; - If the values match, the transaction is ...

    DBIx-Schema-Annotate:DBIx-Schema-Annotate 的只读发布历史

    名称 DBIx::Schema::Annotate - 添加表模式作为对 ORM 文件的注释。... exception_rule =&gt; { # todo } ); # Amon2 + Teng $ carton exec -- perl -Ilib -MMyApp -MDBIx::Schema::Annotate -e 'my $c = MyApp-&gt;b

    java8看不到源码-recruitment-web:招聘网

    java8 看不到源码Java 招聘练习 此 Java 练习旨在培养您在现实世界问题中的概念和解决问题的能力。 本练习的目标是开发一个应用...Exception to that rule are students who are in their first year, who have 15 day

    commons-lang.jar

    org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons....

    org.apache.commons.lang jar包下载(commons-lang3-3.1.jar)

    org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons...

    org.apache.commons.lang jar包下载

    org.apache.commons.lang.time.FastDateFormat$Rule.class org.apache.commons.lang.time.FastDateFormat$StringLiteral.class org.apache.commons.lang.time.FastDateFormat$TextField.class org.apache.commons...

    总结了java容易出错的信息 以及错误信息类型

    总结了java容易出错的信息 以及错误信息类型

    EffectiveC++ and more Effective C++

     ·条款十四:审慎使用异常规格(exception specifications)  ·条款十五:了解异常处理的系统开销  ·条款十六:牢记80-20准则(80-20 rule)  ·条款十七:考虑使用lazy evaluation(懒惰计算法)  ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Another useful rule of thumb: it's typically not cost effective to inline functions with loops or switch statements (unless, in the common case, the loop or switch statement is never executed)....

    Windows读写Ext2/Ext3/Ext4文件系统

    There is one exception to this rule: a file (but not a directory) the driver has created always has cleared "x" permissions, it inherits the "r" and the "w" permissions only. See also section"What ...

    More Effective C++

    Remember the 80-20 rule 条款17:考虑使用 lazy evaluation 085 Consider using lazy evaluation 条款18:分期摊还预期的计算成本 093 Amortize the cost of expected computations 条款19:了解暂时对象的来源 098...

    Algorithmic.Life.Calculative.Devices.in.the.Age.of.Big.Data.1138852

    In what ways are conventional notions of public and private, individual and population, certainty and probability, rule and exception transformed and what are the consequences? How does the search for...

Global site tag (gtag.js) - Google Analytics