This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why dont we catch Error. How Error is different from Exception. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why dont we catch Error. How Error is different from Exception." Watch "Why dont we catch Error. How Error is different from Exception." New topic
Author

Why dont we catch Error. How Error is different from Exception.

yogendra singh
Greenhorn

Joined: Jan 08, 2004
Posts: 21
My question is that i have seen almost in every code that we use Exception in catch not Error.
in a general code flow,if we use the try -catch block ,we usually catch the exceptions and the last catch statement of our try-catch block is
catch(Exception e) { } e.g
try {
// some code say I/O activity
}catch(IOException ioe){ //.. }
catch(Exception e){ // .. }
why we dont use catch(Error er) { //..}
As we know that java.lang.Throwable is the super class of java.lang.Exeption why we dont use catch(Throwable te) { // .. } in the end of try-catch block.
I know that there is no technically error to use catch(Error e)we can do it but it doesnt come i coding generally , WHY ? is there any specail purpose of it.
What is the difference between Error and Exception.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hi Yogendra,
Errors are abnormal conditions that should never occur, if they do, it is normally to late to catch them, or is pointless.
An error exception should occur if for say your program runs out of memory, could you fix this problem with a catch statement??
of course you couls ask the JVM to do some garbage collection, but what if your JVM is on its coffee break. So catching an Error is normally pointless or too late.
hope this helps?
Davy


How simple does it have to be???
yogendra singh
Greenhorn

Joined: Jan 08, 2004
Posts: 21
Thanks for nice explanation ,
but consider the case in which out application is making connection with database and/ or writing to some data file . so when an abnormal condition occur cant we close the connection with database and/or closing the o/p stream ??? like if we leave the o/p stream open then no data will be written on the file..
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
consider the case in which out application is making connection with database and/ or writing to some data file

These are exception conditions. That's why you can and should close the connection / file by catching these exceptions.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
these exceptions that you are talking about should be just an exception, e.g.

This should handle what you are trying to close, but then again, i am not that familiar with databases and java together yet!
Davy
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
And if you code cannot catch the exception you could use a finally statement, and this could close your streams instead.
Davy
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I will note that there are some rare occasions where you need to catch an exception - if only to try to log it before everything blows up. You may think that an uncaught error will just produce a stack trace anyway, but that's not always the case. Especially in complex environments - someone else may have redirected the output without you realizing it. And sometimes Sun or someone will throw an Error for something that's not that serious, that you can work around. So sometimes you really do catch an Error or other Throwable. But at the very least, it's something you should think twice about before you do it. And don't be surprised if something else crashes shortly afterwards.


"I'm not back." - Bill Harding, Twister
 
wood burning stoves
 
subject: Why dont we catch Error. How Error is different from Exception.
 
Similar Threads
Regarding try / catch block in Exception
Catching Exceptions
exception
Exception
Static final blank variable