[Ironruby-core] Handling runtime errors in embedded applications

Jimmy Schementi Jimmy.Schementi at microsoft.com
Tue Dec 29 21:46:08 EST 2009

UnhandledException *does* have the feature to not terminate the process; you need to do "e.Handled = true;" in your exception handler to tell the CLR that the exception has been properly handled. If you don't set it, the CLR doesn't see the exception as handled and continues propagating the exception, which will eventually terminate the process.

I was pretty sure it worked for Threads as well, since all threads are still bound to the current AppDomain, but let me know if your fixed exception handler still doesn't solve this. However, your work-around will work in the meantime.

From: ironruby-core-bounces at rubyforge.org [ironruby-core-bounces at rubyforge.org] on behalf of James Leskovar [lists at ruby-forum.com]
Sent: Tuesday, December 29, 2009 4:26 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Handling runtime errors in embedded applications

Unfortunantly, the problem with setting the unhandled exception event is
that there's no way to 'recover' from the exception; the application
terminates immediately after the event. Additionally, this method
doesn't seem to work for exceptions within threads:

  class Program
    static void Main(string[] args)
      AppDomain.CurrentDomain.UnhandledException += new
      Console.WriteLine("Creating engine");
      var engine = Ruby.CreateEngine();
      engine.Execute("Thread.start { puts 'raising exception...'; raise
Exception, 'my exception'; puts 'not reached'; }");

    static void MyExceptionHandler(object sender,
UnhandledExceptionEventArgs e)
      Console.WriteLine("Caught " +

On my box (IronRuby 0.9.2, .NET 3.5sp1), the output is
Creating engine
raising exception...

and then blocks on the call to ReadKey, without ever calling
MyExceptionHandler. Again, the current workaround is to use a
begin-rescue inside the thread proc, which isn't too terrible. One
possible solution would probably be to reopen Thread.start so that it
wraps the given block with begin-rescue, and then pass the exception
object onto my host object for invoking onerror events.

Jimmy Schementi wrote:
> To solve this, you can hook the
> CLR's ApplicationUnhandedException event to catch all exceptions from
> code executing in an AppDomain:
Posted via http://www.ruby-forum.com/.
Ironruby-core mailing list
Ironruby-core at rubyforge.org

More information about the Ironruby-core mailing list