Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checked vs Unchecked Exceptions in Java

Tags:

I am having some problems with understanding the differences between checked and unchecked exceptions in Java.

  1. Firstly, checked exceptions are supposed to look for abnormalities during compile time. Examples provided in different sources cite database connectivity, file handling as some of them, while unchecked exceptions are supposed to look for errors on the programmer's part, like indexing beyond the range of an array, etc.

Shouldn't it be the other way round? I mean, database connectivity is done during run-time, right? Same goes for file-handling. You don't open a file-handle during compile time, so why a possible error on that is looked for during compile-time? On the other hand, indexing an array beyond its range is already done in the program, which can be checked during compile time (if the abnormal index is supplied by user during run-time, then it's okay for it to be a run-time problem). What am I missing here?

2 Secondly, how can RunTimeException, itself being unchecked, subclass Exception, which is checked? What does this signify?

I found an example in Herbert Schildt's book explaining the usage of checked exceptions:

class ThrowsDemo {    public static char prompt(String str)       throws java.io.IOException {   System.out.print(str + ": ");   return (char) System.in.read();   }   public static void main(String args[]) {     char ch;     try {       ch = prompt("Enter a letter");     }     catch(java.io.IOException exc) {      System.out.println("I/O exception occurred.");      ch = 'X';     }     System.out.println("You pressed " + ch);     } } 

Is the throws clause necessary here? Why can't I do it just normally with a try-catch statement like this (sorry I don't know how to simulate an IO Exception, so couldn't check it myself!):

class ThrowsDemo {    public static char prompt(String str)  {      System.out.print(str + ": ");      return (char) System.in.read();   }   public static void main(String args[]) {     char ch;     try {       ch = prompt("Enter a letter");     }     catch(java.io.IOException exc) {      System.out.println("I/O exception occurred.");      ch = 'X';     }     System.out.println("You pressed " + ch);     } } 
like image 815
SexyBeast Avatar asked Dec 23 '12 14:12

SexyBeast


People also ask

What is the difference between checked and unchecked exception in Java?

Difference Between Checked and Unchecked Exceptions in JavaA checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.

What is the main difference between a checked and unchecked exception?

Checked Exceptions are checked at runtime of the program, while Unchecked Exceptions are checked at the compile time of the program. Checked Exceptions and Unchecked Exceptions both can be created manually. Checked Exceptions and Unchecked Exceptions both can be handled using try, catch and finally.

What are the checked exceptions in Java?

Checked Exceptions For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist. Java verifies checked exceptions at compile-time. Some common checked exceptions in Java are IOException, SQLException and ParseException.

What is checked exception example?

Checked exception example A checked exception in Java represents a predictable, erroneous situation that can occur even if a software library is used as intended. For example, if a developer tries to access a file, the Java IO library forces them to deal with the checked FileNotFoundException.


2 Answers

CheckedException needs to be handled by the caller, Unchecked exception don't.

So, when you design your application you should take in mind what kind of exceptional situation you are managing.

For example, if you design a validation method that checks the validity of some user input, then you know that the caller must check the validation exception and display the errors to the user in a nice looking way. This should be a checked exception.

Or, for those exceptional conditions that can be recovered: imagine you have a load balancer and you want notify the caller that one of the "n" servers is down, so the caller must recover the incident re-routing the message to another server; this should be a checked exception, because it is crucial that the caller (client) tries to recover the error, and don't just let the error to break the program flow.

Instead, there are many conditions that should not happen, and/or should instead break the program. For example, a programming error (like division by zero, null pointer exception), a wrong usage of an API (IllegalStateException, OperationNotSupportedException), an hardware crash, or just some minor situation that are not recoverable (lost connection to a server), or a doomsday :-) ; in those cases, the normal handling is to let the exception reach the most outer block of your code that displays to the user that an unpredictable error has occurred and the application can't do nothing to continue. It's a a fatal condition, so the only thing you can do is to print it to the logs or showing it to the user in the user interface. In those cases, catching the exception is wrong, because, after catching the exception you need to manually stop the program to avoid further damages; so it could be better to let some kind of exception "hit the fan" :)

For those reasons there are some exceptions that are Unchecked also in the JRE: OutOfMemoryError (unrecoverable), NullPointerException (it's a bug that needs to be fixed), ArrayIndexOutOfBoundsException (another bug example), and so on.

I personally think that also SQLException should be unchecked, since it denotes a bug in the program, or a connection problem to the database. But there are many examples where you get exception that you really don't have any clue in how to manage (RemoteException).

The best way to handle exceptions are: if you can recover or manage the exception, handle it. Otherwise let the exception pass out; somebody else will need to handle. If you are the last "somebody else" and you don't know how to handle an exception, just display it (log or display in the UI).

like image 110
Luigi R. Viggiano Avatar answered Oct 02 '22 08:10

Luigi R. Viggiano


  1. you do not need to declare unchecked exceptions in a throws clause; but you must declare checked exceptions;
  2. RuntimeException and Error, and all of their subclasses (IllegalArgumentException, StackOverflowError etc), are unckecked exceptions; the fact that RuntimeException is unchecked, unlike other Throwable subclasses, is by design;
  3. there is no such thing as "compile time exceptions".

More generally, it is considered that unchecked exceptions are thrown in the event of either JVM errors or programmer errors. One famous such exception is NullPointerException, often abbreviated as NPE, which is a subclass of RuntimeException, and therefore unchecked.

Another very crucial difference between unchecked exceptions and checked exceptions is that within a try-catch block, if you want to catch unchecked exceptions, you must catch them explicitly.

Final note: if you have exception classes E1 and E2 and E2 extends E1, then catching and/or throwing E1 also catches/throws E2. This stands for both checked and unchecked exceptions. This has an implication on catch blocks: if you do differentiate between catching E2 and E1, you must catch E2 first.

For instance:

// IllegalArgumentException is unchecked, no need to declare it public void illegal() {     throw new IllegalArgumentException("meh"); }  // IOException is a checked exception, it must be declared public void ioerror()     throws IOException {     throw new IOException("meh"); }  // Sample code using illegal(): if you want to catch IllegalArgumentException, // you must do so explicitly. Not catching it is not considered an error public void f() {     try {         illegal();     } catch (IllegalArgumentException e) { // Explicit catch!         doSomething();     } } 

I hope this makes things clearer...

like image 33
fge Avatar answered Oct 02 '22 06:10

fge