What does it mean when the main method throws an exception?

后端 未结 8 1399
轮回少年
轮回少年 2021-01-01 09:31

I\'m reviewing a midterm I did in preparation for my final exam tomorrow morning. I got this question wrong, but there\'s no correct answer pointed out, and I neglected to a

8条回答
  •  没有蜡笔的小新
    2021-01-01 10:14

    Although an answer has already been chosen for this question, I think that the correct answer to your test is number 3:

    1. The main method should simply terminate if the FileNotFoundException occurs.

    Because it says should, not would.
    I mean, the code in the method can catch any exception, both checked and unchecked, and either, swallow, recover from or rethrow it — or it could not catch them at all.

    But if it catches a FileNotFoundException without ultimately (re)throwing it, it is breaking its own contract: any caller won't see the exception as stated in the method signature, because the method handled it (maybe even the compiler could complain in that case, saying that the exception is never thrown, but I not sure here).

    I think your midterm was posing a question about design rather than about a specific implementation.

提交回复
热议问题