• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Advice on handling exceptions

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I debugging an existing piece of code which looks like the following.

After start() is called, invocations on method1() and method2() can procede.
If an exception is encountered in start(), conn might end up with the null
value. And method1() and method2() will throw NPEs.

One way to handle the NPE is to: log them & silently default values back.
e.g. I could return the empty Set in method1() and null in method2(). Or perhaps I could throw an IllegalStateException. Which one is better ? Are there other possible alternatives. Thanks
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go for the IllegalStateException. A valid connection is a precondition of your methods.
You definitely do not want to just return an empty Set when there is no connection. This can be taken as if there is just no data available at the moment, which could happen with a valid connection.
A client attempting to use these methods without a valid connection might be buggy, so it's better to let it blow up with an exception.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg, I'll throw the IllegalStateException like you suggested. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic