Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I leave the catch block empty when handling some exceptions

 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-Does the user need to be notified about the following two exceptions :

1- NotBoundException
2- InterruptedException

- If we are sure that none of the previous exceptions is thrown, could we leave the catch bloc empty when handling these exceptions, I am getting confused, because the compiler oblige me to catch the exception and in the other hand, I am sure that the exception will never be thrown.

Best Regards
 
Robert Bar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The main purpose of exceptions is to leave the method body abnormally if it's not possible to provide a valid result. Just to remember: "return desired result or throw exception". Have you considered throwing business specific exception (lets say ServerFailureException) rather than eating the cause?
Eaten exception always smells bad...

regards,
Robert
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are certain they'll never cause problems, at the very least comment the empty blocks to say so.
But you'd rather be darn certain as it's bad practice to say the least.
And how can you be sure? With these exceptions, you can never be sure. You're talking about a distributed, multithreaded application.
Those exceptions can both happen for a variety of reasons, none of which may ever occur on any machine you use for testing but might well happen on the assessor's machine the first time he starts it.
You'd not want the man to see a stacktrace and find the application crash from under him would you?

At the very least you should log an exception, just to be sure you know when it happens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic