Why doesn't rescue catch Exception by default?

S

Seth

From:
http://www.rubycentral.com/book/tut_exceptions.html

--- BEGIN
When you need to raise an exception, you can use one of the built-in
Exception classes, or you can create one of your own. If you create your
own, you might want to make it a subclass of StandardError or one of its
children. If you don't, your exception won't be caught by default.
--- END

My question is *why* doesn't it handle the most generalized class,
Exception, by default? What's the logic behind this?
 
E

Eric Hodel

From:
http://www.rubycentral.com/book/tut_exceptions.html

--- BEGIN
When you need to raise an exception, you can use one of the built-in
Exception classes, or you can create one of your own. If you create
your
own, you might want to make it a subclass of StandardError or one
of its
children. If you don't, your exception won't be caught by default.
--- END

My question is *why* doesn't it handle the most generalized class,
Exception, by default? What's the logic behind this?

From http://www.zenspider.com/Languages/Ruby/QuickRef.html#34:

Exception
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError (default for rescue)
[...]
SystemExit
fatal

You don't want to rescue any of these other exceptions (besides
StandardError and friends) unless you really know what you're doing.

Don't create your own exception classes by subclassing Exception
directly, subclass the closest matching exception class, or subclass
RuntimeError or StandardError.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top