Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Exception error code...

Replytotopic

Exception error code

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

Hello

When I treat an Exception in my application I do this with the following code:

def x begin ... rescue Exception => e logger.error(e.message) end
end

“e.message” gives the error message. Is there a way to get the error code?

Thanks
regards

 
Profile

Authority 12
Posting Rating 96
Sign in to rate this post

Hi Paulo,

Ruby’s base Exception class ( http://www.ruby-doc.org/core/classes/Exception.html ) only has a ‘message’ property. In cases where a code is needed the original developer will have made a subclass of Exception which defines the extra fields needed.

Your code can have multiple rescue clauses to handle this, for example

begin
  do_something()
rescue SomethingError => er
  logger.debug er.code
rescue AnotherException => ex
  logger.debug ex.name
rescue Exception => e
  logger.debug e.message
end

Jon

 
Profile

Authority 12
Posting Rating 95
Sign in to rate this post

When an error occurs, the Ruby interpreter packages the error in an exception object. This object propagates back up the call stack until it reaches some code that explicitly declares it knows how to handle this particular type of exception. Exceptions that are never caught propagate through the call stack, ending up with an abnormal program termination; the stack trace is printed to stderr. This is opposed to returning error codes like shell scripts and C do, leading to less-nested statements, less spaghetti logic and simply better error handling.

Go through this http://www.linuxjournal.com/article/4834

Replytotopic

Other Recent Topics

Ask a Rails expert : How to use mephisto

Ask a Rails expert : How to use mephisto

Ask a Rails expert : will_paginate customization problem

Ask a Rails expert : BackgroundRB still wants 'development' environment...?

Ask a Rails expert : activescaffold, sql exception

Ask a Rails expert : Passing non-english chars in query string

Ask a Rails expert : Rails and 2D barcodes

Ask a Rails expert : apache giving proxy error

Ask a Rails expert : Custom Responses w/ 'extra' information...?

Ask a Rails expert : Log rotation in rails

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel