[Ironruby-core] Handling runtime errors in embedded applications

James Leskovar lists at ruby-forum.com
Tue Dec 29 19:26:16 EST 2009

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/.

More information about the Ironruby-core mailing list