# Sending mail when exception occured

**URL:** <https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460>\
**Category:** Ruby\
**Created:** [March 3, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460 "2006-03-03T23:41:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![myysth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@myysth](https://www.ruby-forum.com/u/myysth)\
**Post date:** [March 3, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460/1 "2006-03-03T23:41:37Z")

</div>

Hello!

I want to send e-mail when an exception will occur. What is important  
mail should contain backtrace.  
At first I redefined initialize method in  
Exception class, it worked but of course I didn`t have access to backtrace. Now I have redefined set_backtrace method. It works as I want to, it`s access to backtrace and message of exception. I send  
mail directly from set\_backtrace, because I noticed that method is  
called every  
time an exception occurs.  
Is it correct way? Maybe, you could propose better solution of my  
problem?

Thanks in advance.  
Michal

---

<div class="post-metadata">

**Author:** ![myysth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@myysth](https://www.ruby-forum.com/u/myysth)\
**Post date:** [March 4, 2006, 12:50am UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460/2 "2006-03-04T00:50:21Z")

</div>

Michal Lomnicki wrote:

> Hello!

Hello!

> Is it correct way?

Does it work? Does it allow you to go back to solving more interesting  
or valuable problems?

Not always the best criteria, but worth considering.

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://web2.0validator.com](http://web2.0validator.com) - We’re the Dot in Web 2.0

---

<div class="post-metadata">

**Author:** ![myysth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@myysth](https://www.ruby-forum.com/u/myysth)\
**Post date:** [March 4, 2006, 1:16am UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460/3 "2006-03-04T01:16:34Z")

</div>

> > Is it correct way?
> 
> Does it work? Does it allow you to go back to solving more interesting  
> or valuable problems?

Yes, it works, but I`m not sure if it works in all cases. I`m just courious about other solution of this problem:)

---

<div class="post-metadata">

**Author:** ![myysth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@myysth](https://www.ruby-forum.com/u/myysth)\
**Post date:** [March 5, 2006, 5:42am UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460/4 "2006-03-05T05:42:42Z")

</div>

On Friday 03 March 2006 18:16, Michal Lomnicki wrote:

> > > Is it correct way?
> > 
> > Does it work? Does it allow you to go back to solving more interesting  
> > or valuable problems?
> 
> Yes, it works, but I`m not sure if it works in all cases. I`m just courious about other solution of this problem:)

Unless I completely misunderstood you, why not just do:

begin

# Do whatever it is you’re going to do

rescue Exception =\> e

# Create your email

message\_string = “An error has occurred. Backtrace: #{e.backtrace}”

# Email it off however.

end

I get the strange feeling though that you want more than this…?

---

<div class="post-metadata">

**Author:** ![myysth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@myysth](https://www.ruby-forum.com/u/myysth)\
**Post date:** [March 5, 2006, 9:52am UTC](https://www.ruby-forum.com/t/sending-mail-when-exception-occured/76460/5 "2006-03-05T09:52:18Z")

</div>

> Unless I completely misunderstood you, why not just do:
> 
> begin
> 
> # Do whatever it is you’re going to do
> 
> rescue Exception =\> e
> 
> # Create your email
> 
> message\_string = “An error has occurred. Backtrace: #{e.backtrace}”
> 
> # Email it off however.
> 
> end

Of course it`s the simplest way, but I`ll have to invoke that in every  
rescue block. So, let`s say that there is _uncatched_ exception, even  
then mail should be sent. I redefined set\_backtrace in Exception class.

> I get the strange feeling though that you want more than this…?

I hope that now my problem is clear.
