20

I have noticed that in the Ruby exception hierarchy, there are "errors" such as ArgumentError and there are "exceptions" such as SignalException. Is there a certain practise of naming exceptions? thanks in advance, ell.

3
  • 1
    stackoverflow.com/questions/912334/… has a good description of the differences between exceptions and errors, albeit for a language other than ruby. Commented Jun 6, 2010 at 23:16
  • Thanks, that cleared things up a bit!
    – Ell
    Commented Jun 7, 2010 at 17:12
  • 1
    @AndrewGrimm I think that reference is misleading in the case of Ruby. Ruby's error/exception hierarchy is designed such that classes representing situations requiring handling are extremely likely to be subclasses of StandardError or RuntimeError and should have Error in their names. Commented May 30, 2021 at 2:56

2 Answers 2

13

Looking at the list of Ruby exceptions, SignalException is the only one that is named *Exception; everything else is an XXXError (except for SystemExit and fatal). If anything, the practice is to name your exception FooError. I'm having trouble finding any specific reason why SignalException isn't named SignalError.

2
  • 2
    I would second that. Another question, since everything we should be rescuing is inherited from StandardError, would it make sense to use rescue StandardError => error instead of rescue StandardError => exception? I've always used exception but this answer gave me pause now. Commented Jan 6, 2019 at 23:16
  • Yes, I think it would make sense to call it error. Commented May 30, 2021 at 2:51
5

The convention is Module::#{Type}Error for anything caused by your application (e.g. http://weblog.jamisbuck.org/2007/3/7/raising-the-right-exception). Exception handling in Ruby isn't stratified the same way as in Java since the exception model is different at the language level.

From what I've seen, the conventions are adhered to a little more loosely for C/FFI/JNA extensions.

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