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?
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
rescue SomethingError => er
rescue AnotherException => ex
rescue Exception => e
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