Checked vs Unchecked exception

后端 未结 6 558
抹茶落季
抹茶落季 2020-11-27 06:49

I\'ve studied that: With an unchecked exception, however, the compiler doesn\'t force client programmers either to catch the exception or declare it in a throws clause. In f

6条回答
  •  猫巷女王i
    2020-11-27 07:38

    • Checked Exceptions are useful for handling events that occur in the normal operation of a program. An example would be an IOException that is thrown when a file cannot be opened. These exceptions occur even if there is nothing wrong with the program. It is necessary, therefore, to tell the program how to handle the exception.
    • Unchecked exceptions are useful for identifying defects in the code. For instance, a NullPointerException is thrown when a value is read on a null object. Thus an Unchecked Exception represents a problem that requires a manual fix by the programmer. It is reasonable for the program to crash in order to avoid erroneous behavior, so a try-catch block is not required (but might be desirable in order to provide mitigation, such as displaying an error to the user).

提交回复
热议问题