English 中文(简体)
在主函数中,在试块块后, 扔出一个例外, 我如何让它继续执行下一个试块?
原标题:In Main function, after a try block throws an exception how can I make it keep on execute the next try block
  • 时间:2012-05-23 02:28:27
  •  标签:
  • java

在主函数中,在试区块扔出一个例外后,函数应该中断。我的问题是,我如何让它继续执行到下一个试区块。下面我举一个例子:

public static void main(String [] s){
    ABC aBC = new ABC();
    try {
        aBC.execute();
    } catch (Exception e) {
        _log.error(ErrorCodeEnum.ERROR,
                "XXXXXXX!!! in " + new Date(),e);
    }
    BCD bCD = new BCD();
    try {
        bCD.execute();
    } catch (Exception e) {
        _log.error(ErrorCodeEnum.ERROR,
                "YYYYYYYYYYY!!! in " + new Date(),e);
    }
}
问题回答

代码应该按现在的写法运作,即如果第一个街区抛出并有例外,第二个街区仍应执行。

然而,如果你真的想捕获一切,你不妨考虑捕获 throwable ,而不是 Expendion ,如果你真的想捕获一切。特别是,>java.lang.Error 不是例外的子类,而是可丢弃的子类。

只需确定,您就可以在 最终 块中将要执行的语句包装如下:

ABC aBC = new ABC();

try
{
    aBC.execute();
}
catch (Exception e)
{
    _log.error(ErrorCodeEnum.ERROR, "XXXXXXX!!! in " + new Date(), e);
}
finally
{
    BCD bCD = new BCD();

    try
    {
        bCD.execute();
    }
    catch (Exception e)
    {
        _log.error(ErrorCodeEnum.ERROR, "YYYYYYYYYYY!!! in " + new Date(), e);
    }
}

finally 区块中的语句将被执行,无论外部 try 区块是否有例外。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签