File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exceptions" Watch "exceptions" New topic


Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
what is the difference between checked and unchecked exceptions?

As i know exceptions are conditions which occur at runtime,
then why such distinction between exceptions in first place?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Because most RuntimeExceptions (NullPointerException, IllegalThreadStateException, NoSuchElementException, most occurrences of IllegalArgumentException) can be prevented by adding extra checks, whereas you can't check easily if your database is up and running (SQLException), for instance, or if your network is available, or your hard drive is broken (both IOException).

Granted, sometimes a preventable exception is still a checked exception (CloneNotSupportedException being the most annoying one), and I'm still wondering why FileNotFoundException exists*, but in most cases the rule is: if it can be prevented by adding checks it should be a RuntimeException, otherwise it should be a checked exception.

* there are two occurrences when a FileNotFoundException can be thrown:
- a file does not exist. But of course File.exists() can be used as a check.
- a file exists but cannot be opened as requested (e.g. trying to write to a read-only file). This could (should?) be handled with a regular IOException.

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Rob Prime wrote:- a file exists but cannot be opened as requested (e.g. trying to write to a read-only file). This could (should?) be handled with a regular IOException.

FileNotFoundException is a subclass of IOException, so if you catch IOException, you're also catching FileNotFoundException - i.e. you don't have to deal with it separately if you don't want to.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

True, but that's beside the point. The point is, that I don't like the way the exception is used. Half of its purpose can be prevented, and for the other half the exception name is simply misleading. If I try to write to a read-only file I don't want a FileNotFoundException - I want a CannotWriteToFileException or something like that. Perhaps FileInaccessibleException.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Rajiv : The Java designers decided long ago that the compiler should be
used to enforce additional rules for some, but not all exceptions. Since
exceptions must be throwable, all exception types extend Throwable:
Throwable.Error(*) Throwable.Exception. And at the next level of the
exceptionheirarchy comes Throwable.Exception.Runtime(*).

The compiler does not enforce its "extra" rules for any exception of
the two types marked (*). All others, notably any extension of type
Exception, are examined more closely by the compiler. They are the
"checked" exceptions - as in "checked by the compiler".

Remember that it is just the two types, and their extensions, that are
the "unchecked" excpetions. If you throw a "new Throwable()" object,
for example, you will see the compiler do its extra checking.

Jim ... ...

I agree. Here's the link:
subject: exceptions
It's not a secret anymore!