Why doesn't rescue catch Exception by default?

Discussion in 'Ruby' started by Seth, Jun 22, 2007.

  1. Seth

    Seth Guest

    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?

    --
    Posted via http://www.ruby-forum.com/.
     
    Seth, Jun 22, 2007
    #1
    1. Advertising

  2. Seth

    Eric Hodel Guest

    On Jun 21, 2007, at 21:04, Seth wrote:

    > 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.

    --
    Poor workers blame their tools. Good workers build better tools. The
    best
    workers get their tools to do the work for them. -- Syndicate Wars
     
    Eric Hodel, Jun 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,168
    Xenos
    Aug 20, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,036
    Smokey Grindel
    Dec 2, 2006
  3. Steve
    Replies:
    3
    Views:
    698
    Steve
    Aug 4, 2009
  4. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    574
    Daniel Pitts
    Aug 5, 2009
  5. Wes Gamble
    Replies:
    1
    Views:
    102
    Robert Klemme
    Nov 20, 2006
Loading...

Share This Page