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

后端 未结 8 1338
轮回少年
轮回少年 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:28
    1. main() ultimately throws all unhandled exceptions, right? So, effectively adding throws FileNotFoundException is redundant.

    2. A throws statement implies nothing about how a method handles an exception. This main() method may indeed handle FileNotFoundException, and then re-throw it.

    So, that throws statement indicates nothing about how any exception is caught and handled. And questions 3 & 4 are only valid if we assume that the exception in question it not handled. Without that assumption, the questions are indeterminate. With that assumption, both 3 & 4 are true!

    The correct answer is to ask the school for a refund for this class - even 6.5 years later.

    Could you please forward this teacher's contact information? I would like to have a word with them.

    0 讨论(0)
  • 2021-01-01 10:31

    Dude, a little late, but answer is Number 3.

    Number 1 is false because it is not handling FileNotFoundException

    Number 2 is false for the same reason.

    Number 3 is true. If a FileNotFoundException is thrown, the main method will terminate.

    Number 4 is false. It would not terminate in case of ANY exception. It would terminate only in case of unchecked exception or FileNotFoundException. If there are not other checked exceptions declared in the 'throws' clause, it means they are being handled within the method.

    0 讨论(0)
提交回复
热议问题