本文介绍了酷还是笨? Catch(异常[NamingException,CreateException] e)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些代码,我注意到异常处理中的模式让我想到了:

  try {

// do stuff ... throws JMS,Create and NamingException

} catch(NamingException e){

log1(e);
rollback();
doSomething(e)
} catch(CreateException e){

log1(e);
rollback();
doSomething(e)
}

其中JMSException将处理一些



是否只是写:

 尝试{

// do stuff ... throws JMS,Create and NamingException
} catch异常[NamingException,CreateException] e){

log1(e) ;
rollback();
doSomething(e)
}

而不是把它放在助手方法:

  try {

// do stuff ... throws JMS,Create and NamingException
} catch(NamingException e){

helper_handleError1(e)
} catch(CreateException e){

helper_handleError1(e)
}请注意,我想要传播原始JMSException的stacktrace,而且我不会感觉到创建一个新的JMSException与第三个catch子句:)



任何艰难?这是一个极端的情况,只会污染Java的语法,或只是一个很酷的东西来添加?

解决方案

他们在考虑Java 7的这种类型的扩展。



请参阅:


I was writing some code, and I notice a pattern in the exception handling that got me thinking:

try{

        // do stuff... throws JMS, Create and NamingException

} catch (NamingException e) {

        log1(e);
    rollback();
        doSomething(e)
} catch (CreateException e) {

        log1(e);
    rollback();
        doSomething(e)
}

Where JMSException would be handle some where up in the stack.

Would it be to just write:

try{

        // do stuff... throws JMS, Create and NamingException
} catch Exception[NamingException, CreateException] e) {

        log1(e);
    rollback();
        doSomething(e)
}

instead of putting it in tu a helper method:

try{

        // do stuff... throws JMS, Create and NamingException
} catch (NamingException e) {

        helper_handleError1(e)
} catch (CreateException e) {

        helper_handleError1(e)
}

Notice that I want to propagate stacktrace of the original JMSException, and I don't "feel like" creating an new JMSException with a third catch clause :)

Any toughs? Is this an extreme situation that would only pollute the syntax of Java, or just a cool thing to add?

解决方案

They are considering an extension of this type for Java 7.

See: http://tech.puredanger.com/java7#catch

这篇关于酷还是笨? Catch(异常[NamingException,CreateException] e)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:56