Java Interview Question
Use this intermediate question and answer to prepare for your next Java interview.
What are exceptions? What is the difference between checked and unchecked exceptions?
Exceptions are errors in a program which disrupt the normal flow of code execution.
Checked exceptions are those which can be expected by the program due to various conditions, such as bad user input. These are handled using a try/catch block, and the flow of code execution is still in control of the programmer. If checked exceptions are not handled or explicitly declared using
throws, then it will result in a compiler error at the line where there is a possibility for it to be thrown.
Unchecked exceptions are those which are not expected by the programmer, and so they come up at Runtime. They are also called Runtime Exceptions. Although it is possible to handle unchecked exceptions, it is not recommended, as they are caused by an unexpected sequence of events which may indicate a deeper issue in the program. Unchecked exceptions can be left unhandled without compiler errors.