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

Related to exception handling

Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi !
I've been making a stupid mistake while coding. After try/catch block i write code as if nothing has happened. For example i establish a network connection and retrieve some data from the server. Now after my try/catch block i'm using that data and go on as if nothing has happened. I don't think this is right. So i have come up with the following approach.
Suppose i have a boolean value and it is set to true in the try block. In the catch block it is set to false. So after my try/catch block I should test this boolan value. If it is true then there should be further processing and if it is false an error message should be displayed.
Do you think this is the right thing to do ?
[ July 05, 2003: Message edited by: Danish Shaukat ]
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
why can't you display your error message in the catch block?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I think that most likely, if the code following the catch block is something that shouldn't be executed after an exception, then either (a) your catch block is in the wrong place, or (b) you shouldn't catch the exception at all - declare it in the method's throws clause instead. (This will probably force you to decide whether or how to deal with the exception in some other method instead.)
E.g. if you have

consider replacing it with


[ July 05, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Thanks Jim
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Excellent examples, Jim! And Danish, if your code is any longer than Jim's, consider breaking it up to look just like his. (Had to try that new Graemlin!)

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:
subject: Related to exception handling
It's not a secret anymore!