1

Which SPL exception should be thrown when sanity checks fail? For example, if the check() function fails when saving data with JTable or a parameter check fails? I'd prefer to use something more specific then RuntimeException.

If the answer is to extend my own exceptions, where would be the best place to put them? In a helper class file?

1 Answer 1

1

First of all, i recommend reading this two posts:

Whats the correct way for exception handling?

http://martinfowler.com/articles/replaceThrowWithNotification.html

Basically, it might be so that in your case it is not wise to use exceptions at all. It depends on the answer to the question if this behavior is expected (not an exception) or not (then it is an exception).

If you think you need to use exceptions after reading the linked pages, the next question is where the benefit is in using more specific (custom) exceptions? The rule of thumb is to use built-in exceptions whenever possible. Maybe throw in some context and i can elaborate more.

2
  • Interesting reading. Since validation errors are expected then they should be enqueued and not thrown. However, in the case of check() in JTable, it is expected that the data would have been validated prior to that point. If I validate the data before writing it and it fails, that is unexpected behavior. Should an exception not be thrown? The error is recoverable but the data won't be written.
    – nimble
    Commented Apr 28, 2016 at 18:43
  • Hm good question. What can happen so that the check() method fails? What "more" does a sanity check do in comparison to validation errors?
    – JSamir
    Commented Apr 29, 2016 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.