[Ironruby-core] hosting: howto get the nice ruby backtrace on exception thrown from ruby?

Tomas Matousek Tomas.Matousek at microsoft.com
Thu Feb 19 12:10:23 EST 2009

I completely agree with that. I think we should add a method GetStackTrace() that returns an instance of some StackTrace class, which is an array of StackFrames, where StackFrame is some struct/class that contains method name, file name and line number.


From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Meinrad Recheis
Sent: Thursday, February 19, 2009 1:06 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] hosting: howto get the nice ruby backtrace on exception thrown from ruby?

Thanks Thomas, thats very good. As for a suggestion about ExceptionService features: It would be quite useful to retrieve the exception backtrace as string[] in order to be able format it to one's needs. For instance, I like using the VisualStudio error parser to be able to jump from the backtrace in the output right into the right line in the source file (see my blog post: http://www.eqqon.com/index.php/Navigating_in_Exception_Stack_Traces_in_Visual_C-Sharp). To do that, currently, I would need to parse the formatted backtrace and re-format it. No big deal. Just would be nice to have.

-- henon

On Thu, Feb 19, 2009 at 3:30 AM, Tomas Matousek <Tomas.Matousek at microsoft.com<mailto:Tomas.Matousek at microsoft.com>> wrote:

You can use ExceptionService:

        var engine = Ruby.CreateEngine();

        try {


def foo



def goo

  raise 'hello'




        } catch (Exception e) {

            var exceptionService = engine.GetService<ExceptionOperations>();

            string message, typeName;

            exceptionService.GetExceptionMessage(e, out message, out typeName);





The current implementation is not ideal. If you're missing some features on the service let us know. We might consider adding them.


From: ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org> [mailto:ironruby-core-bounces at rubyforge.org<mailto:ironruby-core-bounces at rubyforge.org>] On Behalf Of Meinrad Recheis
Sent: Wednesday, February 18, 2009 1:25 AM
To: ironruby-core
Subject: Re: [Ironruby-core] hosting: howto get the nice ruby backtrace on exception thrown from ruby?

I've given it some thought. I would like to avoid wrapping the code in "begin - rescue" and instead catch the exception in c#. Then just send the ruby methods "message" and "backtrace" to the Ruby-exception-object from C#. How would that be done?
Any other hints?

-- Henon

On Tue, Feb 17, 2009 at 11:36 PM, Meinrad Recheis <meinrad.recheis at gmail.com<mailto:meinrad.recheis at gmail.com>> wrote:


I am now currently using this ugly hack to get a reasonable (let's say user friendly) exception backtrace from an error in executed code:

        public object Execute(string code, ScriptScope scope)


            code = @"begin




rescue Exception


self.__backtrace__ = $!.backtrace



            object result = null;



                LogManager.GetLogger("Ruby").Debug("\r\n" + code);

                result = m_engine.Execute(code, scope);


            catch (Exception e)


                var backtrace = scope.GetVariable<RubyArray>("__backtrace__");

                string msg = scope.GetVariable<string>("__message__") +"\r\n" + string.Join("\r\n", backtrace.Select(s => s.ToString()).ToArray());



            return result;


Even though it seems to work well I am absolutely not satisfied with it. Is there a better way to do this?
-- henon

Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090219/cf178e57/attachment-0001.html>

More information about the Ironruby-core mailing list