aspose file tools*
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
Author

Exceptions

Arun Prasath
Ranch Hand

Joined: Jul 18, 2006
Posts: 60
why exception in java has been classified into checked and unchecked
exception rather than keeping it under simply exception?
Dan Howard
Ranch Hand

Joined: Feb 22, 2004
Posts: 47
why exception in java has been classified into checked and unchecked
exception rather than keeping it under simply exception?


There are actually 3 types. Checked Exceptions, Runtime Exceptions and Errors. Errors are similar to Exceptions but they are reserved for VM errors like out of memory conditions.

When you have a method, it's name usually indicates what it does, one of the reason for having checked exceptions is to indicate what the method can't do.

Example:



So for someone using this method what is it saying?

readFile: Hi, I'm readFile. If you pass a file name string to me I can read it and return the contents as a string. But if the file's not there, I'm sorry but I can't handle that. I'm just giving up and throwing it back at you. Really sorry, but I just can't deal with this situation.

The reason is that this method might be common and could be used with a web application, web service, desktop app, cell phone, etc. Each might would want to hand this situation differently.


This is a simple example. There are many cases where you might not want to throw an exception. This is one of the great debates. If you google you'll find many articles and threads on this topic.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

the difference between checked an unchecked exception is, for an unchecked exception it is not required to handle or declare it.


Imagine what would happen if all exceptions were checked, NullPointerException for example.
Or if all were unchecked, IOException for example.

Yours,
Bu.


all events occur in real time
Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi....

unchecked exceptions are the ones where the fault lies with programmer, where as checked exceptions are the ones where programmer may not be at fault....
unchecked or Run time exception can occur due to faulty design of your code such as dividing integer by zero.... Immagine what would have happened if it was put under checked exception, you would then have to provide try catch block to every integer you use thus making code difficult to read and write....
As for checked exception, they are the ones on which programmer don't have any control.... File name passed to program may not be present or corrupted....
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Checked exceptions were a fairly new experiment in Java. They are consistent with compile time type checking in the rest of the language but few (any?) other languages use them. There is much debate on whether they were a good idea overall or not. See the Exceptions chapter in Thinking In Java for a good explanation of the pros & cons.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions