# Sorta odd error handling

**URL:** <https://www.ruby-forum.com/t/sorta-odd-error-handling/64138>\
**Category:** Ruby\
**Created:** [June 22, 2006, 11:53am UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138 "2006-06-22T11:53:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [June 22, 2006, 11:53am UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138/1 "2006-06-22T11:53:30Z")

</div>

When an exception gets raised ‘all the way out’, you get a nice little  
call-stack with line-numbers and function names, which really helps  
seeing what went wrong, and how. However, I want my application (which  
has an infinite mainloop) to keep running, but have that call stack  
logged. So I have:

begin  
run\_worker\_process queue\_entry[:user][:username],  
queue\_entry[:directory]  
rescue  
log\_error $!  
end

This doesn’t work particularily well for me, because it only mails out  
the actual error, not how it happened. Any ideas on how to improve this?

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [June 22, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138/2 "2006-06-22T14:51:16Z")

</div>

2006/6/22, Ohad L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> log_error $!
> 
> ```
> 
> end
> 
> This doesn’t work particularily well for me, because it only mails out  
> the actual error, not how it happened. Any ideas on how to improve this?

Assuming “how it happended” means the stack trace. You can access it  
via method backtrace.

Another note: don’t use global variables unless you have to. So rather  
do

begin  
…  
rescue Exception =\> e

# print stackt trace as demo, should be done in log\_error

puts e.backtrace  
log\_error e  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [June 22, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138/3 "2006-06-22T16:47:09Z")

</div>

Hi,

In message “Re: Sorta odd error handling”  
on Thu, 22 Jun 2006 18:53:54 +0900, Ohad L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:  
|logged. So I have:  
|  
| begin  
| run\_worker\_process queue\_entry[:user][:username],  
| queue\_entry[:directory]  
| rescue  
| log\_error $!  
| end  
|  
|This doesn’t work particularily well for me, because it only mails out  
|the actual error, not how it happened. Any ideas on how to improve this?

Can you show us the actual error? The plain rescue clause captures  
exceptions that is subclass of StandardError. The error in question  
may not be a subclass.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [June 22, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138/4 "2006-06-22T22:26:18Z")

</div>

Much thanks! I ended up using a variation on the last guest’s  
suggestion.

Question: Class Exception has to\_s and to\_str. Why not include a builtin  
function which would return the full textual description that an  
interpreter bail-out would print?

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [June 22, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/sorta-odd-error-handling/64138/5 "2006-06-22T16:56:28Z")

</div>

On Thu, 22 Jun 2006, Ohad L. wrote:

> log\_error $!  
> end
> 
> This doesn’t work particularily well for me, because it only mails out  
> the actual error, not how it happened. Any ideas on how to improve this?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

errmsg =  
lambda{|e| “%s (%s)\n%s” % [e.message, e.class, (e.backtrace ||  
[]).join(“\n”)]}

begin  
run\_worker\_process queue\_entry[:user][:username],  
queue\_entry[:directory]  
rescue Exception =\> e  
log\_error(errmsg[e])  
end

regards.

-a
