# Set\_trace\_func and exceptions

**URL:** <https://www.ruby-forum.com/t/set-trace-func-and-exceptions/108890>\
**Category:** Ruby\
**Created:** [July 9, 2007, 3:09am UTC](https://www.ruby-forum.com/t/set-trace-func-and-exceptions/108890 "2007-07-09T03:09:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 9, 2007, 3:09am UTC](https://www.ruby-forum.com/t/set-trace-func-and-exceptions/108890/1 "2007-07-09T03:09:06Z")

</div>

There seem to be two problems with set\_trace\_func (or things I don’t  
understand):

1. The following program does not print “done”. It does not raise an  
exception. It _does_ set the program’s return code to false.

It’s not a correct program, but I expected an ArgumentError, rather than  
a silent exit.

1. The following program does not print the exception or “done”. It does  
not raise an exception to the top level. It _does_ set the program’s  
return code to false.

Is it impossible to safely raise an exception inside of a trace func?

This is in ruby 1.8.4 and 1.8.6.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 10, 2007, 8:48am UTC](https://www.ruby-forum.com/t/set-trace-func-and-exceptions/108890/2 "2007-07-10T08:48:41Z")

</div>

2007/7/9, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It’s not a correct program, but I expected an ArgumentError, rather than  
> begin  
> x = 1  
> rescue =\> ex  
> p ex  
> end
> 
> puts “done”
> 
> Is it impossible to safely raise an exception inside of a trace func?
> 
> This is in ruby 1.8.4 and 1.8.6.

It does not make sense to allow exceptions to be thrown from  
set\_trace\_func. I do agree however that at least some form of warning  
should be generated but I think the exception should be ignored  
otherwise. But then again, since set\_trace\_func is more of a  
debugging feature it’s not too important to fix it IMHO.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 10, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/set-trace-func-and-exceptions/108890/3 "2007-07-10T19:03:17Z")

</div>

Robert K. wrote:  
…

> It does not make sense to allow exceptions to be thrown from  
> set\_trace\_func. I do agree however that at least some form of warning  
> should be generated but I think the exception should be ignored  
> otherwise. But then again, since set\_trace\_func is more of a  
> debugging feature it’s not too important to fix it IMHO.

But set\_trace\_func has been used for more than debugging  
(Binding.of\_caller, right?) Arguably, that’s a kludge.

What if you are using set\_trace\_func as a debugging tool to detect some  
problem (memory management, for example), and you want to stop or signal  
the program when the problem is detected?

This issue came up for me because I was tinkering with some code for the  
“CPU/Memory limiting” thread. I was trying to use set\_trace\_func in a  
stupid way to count “steps” per thread, as a surrogate for cpu cycles,  
and raise an exception when a limit was exceeded.

Perhaps this use of exceptions (as a way of signaling back to the  
calling code) is not appropriate with set\_trace\_func. There are other  
ways to do this that would work in the context of the “CPU/Memory  
limiting” thread.

After thinking about it, I guess the only change I would ask for is some  
kind of warning (if running with -w) that an exception happened in the  
trace func, rather than a silent exit from the program.
