19

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.

Ell
  • 4,238
  • 6
  • 34
  • 60
  • 1
    http://stackoverflow.com/questions/912334/differences-betweeen-exception-and-error has a good description of the differences between exceptions and errors, albeit for a language other than ruby. – Andrew Grimm Jun 06 '10 at 23:16
  • Thanks, that cleared things up a bit! – Ell Jun 07 '10 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. – Keith Bennett May 30 '21 at 02:56

2 Answers2

12

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.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 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. – Joshua Pinter Jan 06 '19 at 23:16
  • Yes, I think it _would_ make sense to call it `error`. – Keith Bennett May 30 '21 at 02: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.

Denny Abraham
  • 812
  • 6
  • 6