Rethrow exception in java

后端 未结 7 1552
情书的邮戳
情书的邮戳 2020-12-06 06:41

I have a very simple question about re-throwing exception in Java.

Here is the code snippet:

public static void main(String[] args) throws FileNotFou         


        
7条回答
  •  隐瞒了意图╮
    2020-12-06 07:05

    You are right. Second version is better. Moreover the first version does not make any sense. It does the same except the stack trace of the exception will be "wrong".

    There are the following reasons to "re-throw" exceptions:

    1. If you have something to do before.
    2. If you catch exception of one type and throw exception of other type:

    example:

    try {
       // do something
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    

提交回复
热议问题