2

Consider this code:

Task.Factory.StartNew (() => {
    try {
        Console.WriteLine ("Throwing");
        throw new Exception ();
    } catch {
        Console.WriteLine ("Gotcha!");
    }
});

On iOS Simulator, it prints Throwing and Gotcha! as expected.
However, when compiled for the device, it only prints Throwing and never prints Gotcha!.

Why does this happen?
I'm using MonoTouch 6.0.8.
I can also reproduce it on 6.0.6.

I put up a sample project here, you can try to run it on the device.
(never mind the name—I was investigating a different crash when I found this).

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • Try looking at this previous stackoverflow post-http://stackoverflow.com/questions/363495/difference-between-catch-block-and-throw-new-exception-in-method – MethodMan Dec 21 '12 at 23:24
  • 1
    @DJ: Not sure how it is relevant. I'm throwing an exception, and `catch` doesn't catch it. – Dan Abramov Dec 21 '12 at 23:26
  • @DJ, this is obviously a trivial example to illustrate the issue. – Kirk Woll Dec 21 '12 at 23:31
  • 1
    Here's a similar bug report from Xamarin that claims it's been fixed in relase 5.3.4: https://bugzilla.xamarin.com/show_bug.cgi?id=5423 . Have you tried updating your Monotouch version? – Diego Dec 21 '12 at 23:55
  • @Diego: Mine is the newest. :-) But this looks very similar indeed. – Dan Abramov Dec 21 '12 at 23:56
  • Dear downvoter: you're just like that `catch` block, so silent and mysterious. – Dan Abramov Dec 25 '12 at 01:02

2 Answers2

2

I just found that it only happens with LLVM enabled.
Perhaps it's a bug so I'll report it to Xamarin.

Interestingly, adding

BeginInvokeOnMainThread (() => {
    new UIAlertView ("Gotta catch 'em all!", "", null, "OK", null).Show ();
});

after Console.WriteLine call helps—I see both alert and log entry.

There's my sample project.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
1

Maybe a silly question but have the conditions been met to require the catch command?

If so perhaps the application breaks before returning the "Gotcha" and iOS commands take over.

I believe iOS listens to see if and when your command/application breaks because default functions kick in to handle this. Similar to the dealloc commands.

AJames
  • 64
  • 3