Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Related to exception handling

 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
Regards
Danish
[ July 05, 2003: Message edited by: Danish Shaukat ]
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't you display your error message in the catch block?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

or

[ July 05, 2003: Message edited by: Jim Yingst ]
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic