Checked Exception vs Runtime Exception
Exceptions are special type of events, which can disturb the normal program flow. The name exception comes from “exceptional event”. Throwing an exception is the process of creating an exception object and handing it off to the runtime system. When the runtime system receives the exception object, it will try to find somebody to handle it within the call stack by traversing it in the reverse order (in which the methods were called). The runtime system is successful if it finds a method with an exception handler. Exception handler is a block of code that can officially handle the said exception. If the runtime system finds an appropriate handler, it will pass the exception object to the handler. This is called catching the exception. However, if the exception cannot be handled, the program will terminate. In Java, exceptions inherit from Throwable class. Checked Exceptions are exceptions on which handling is enforced by the compiler. Runtime exceptions are a type of exceptions, which are not checked by the compiler.
What is a Checked Exception?
Checked Exceptions are either objects of the class java.lang.exception or its subclasses (except the java.lang.RuntimeException and its subclasses). Checked exceptions are “checked” at compile time. That means the programmer must either catch or throw these exceptions, or else the compile would complain (causing a compiler error). Because of this reason, many checked exceptions are very well known to programmers. For example, the IOException and its sub classes are checked exceptions, and whenever the programmer is dealing with accessing or modifying a file, compiler checks to make sure that all possible IOExceptions are taken care of by the programmer.
What is a Runtime Exception?
Runtime Exceptions consist of java.lang.RuntimeException and all its sub classes. Theoretically speaking, they serve the same purpose as checked exceptions, and can be thrown or handled like checked exception, but the handling of them is not enforced by the compiler. Therefore, Runtime exceptions belong to the family of unchecked exceptions. NullPointerException, NumberFormatEception, ClassCastException and ArrayIndexOutOfBoundsException are common runtime exceptions in Java.
What is the difference between Checked Exception and Runtime Exception?
Although, both checked exceptions and runtime exceptions are unwanted occurrence during the execution of a program, they have their differences. Checked exception handling is enforced by the compiler, but runtime exceptions are not. Therefore, checked exceptions must to be thrown or handled in order for the code to compile, but there is no such requirement regarding runtime exceptions. Consequently, runtime exceptions belong to unchecked exceptions category along with errors.
Disadvantage with checked exceptions is that the programmer has to handle it even if she does not know how to. So, if the programmer just throws a new exception without wrapping the original, the stack trace belonging to the original exception will be lost. This is where runtime exceptions come in handy. Because all runtime exceptions can be handled in a single place, thus programmers can write less amount of code. On the other hand, since checked exceptions must be caught, there is no surprise for the programmer. She will always know which checked exception could be thrown by a certain method. Contrary to this, various runtime exceptions can be thrown without the knowledge of the programmer.