Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Exception or Error?

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.

like image 873
Ell Avatar asked Jun 06 '10 20:06

Ell


People also ask

What is an exception in Ruby?

An exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at runtime, that disrupts the normal flow of the program's instructions. In Ruby, descendants of an Exception class are used to interface between raise methods and rescue statements in the begin or end blocks.

Does raising an error stop execution Ruby?

When you raise an exception in Ruby, the world stops and your program starts to shut down. If nothing stops the process, your program will eventually exit with an error message. Here's an example.

How do you rescue exceptions in Ruby?

A raised exception can be rescued to prevent it from crashing your application once it reaches the top of the call stack. In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from.


2 Answers

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.

like image 57
Mark Rushakoff Avatar answered Oct 07 '22 18:10

Mark Rushakoff


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.

like image 32
Denny Abraham Avatar answered Oct 07 '22 19:10

Denny Abraham