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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

kumar abhay
Ranch Hand

Joined: Oct 02, 2001
Posts: 53
hi there,
hope that u r doing fine.i m having a problem regarding difference between "ERROR" & "EXCEPTION".could u plz demonstrate it in detail.i will be really thankfull to u.
thanks in advance.
kumar abhay
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
They are physically speaking identical. The difference is in what they signify.
A piece of Java code can throw a java.lang.Throwable or any subclass of it. This is a valid code:

You probably know that this would not be valid code, because Integer is not a subclass of Throwable:

There are only two direct subclasses of Throwable that Sun has included in the java platform, java.lang.Exception, and java.lang.Error.
To quote the java API documentation:
"The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch."
An example would be java.io.IOException for when there are errors reading files and other resources. If a word processor cannot save a file, for example, that does not mean the virtual machine should immediatly shut it down. It should just show a little error message explaining that your file couldn't be saved. It is an Exception and not an Error because the application can recover after an IOException.
Back to the API docs:
"An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur."
Some examples of Error subclasses include
OutOfMemoryError - what can you do if the virtual machine runs out of memory?
ClassFormatError - when the virtual machine tries to load a classfile the does not conform to the classfile format, it would be quite impossible to continue.
That last bit of the API docs means that this is valid code:

Whereas an Exception would require a throws clause too:

Errors don't need to declared in the throws clause because you can't predict where they'll happen and you're not expected to catch them anyway. Basically you won't have much reason to write code that throws or catches Errors. As the API docs said, you're not expected to catch errors.
I should note that subclasses of java.lang.RuntimeException don't need to be declared in the throws clause either. For example:

I hope this was in some way useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptions