Bug 56102 - Try/Catch not working as expected on initial load of activity
Summary: Try/Catch not working as expected on initial load of activity
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 7.3 (15.2)
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2017-05-08 15:38 UTC by JohnRowse
Modified: 2017-06-27 21:47 UTC (History)
2 users (show)

See Also:
Tags: bb ac
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project to recreate (27.27 KB, application/x-zip-compressed)
2017-05-08 15:38 UTC, JohnRowse
Details

Description JohnRowse 2017-05-08 15:38:52 UTC
Created attachment 22005 [details]
Test Project to recreate

If you successfully load/create an activity, any subsequent unhandled exception is confined only to that activity – you get a stopped working dialog, but the previous activity(s) remain functional. However if you put an unhandled exception in the create method of an activity, and handle the exception form the calling activity (from the StartActivity method, not only does it crash the called activity, but it crashes the calling activity. In the example, trying to load activity three, should remain on activity 2, as activity 2 is handling the load of activity 3 with a try/catch, however it returns to activity 1. The ‘crash’ button is just there to confirm the normal operation of an unhandled operation from a fully loaded activity.
Comment 1 Mark McLemore 2017-06-27 21:47:08 UTC
Confirmed with the following steps using Visual Studio 2017 15.2 and Xamarin.Android 7.3.1.2:

1. Build and run the attached project.
2. In the Activity 1 screen, tap LOAD ACTIVITY 2.
3. In the Activity 2 screen, tap LOAD ACTIVITY 3.

The last step causes "Unfortunately, ActivityCrashTest has stopped" (Nexus 5, Android 6.0.1).

Here's the relevant snippet from Activity2 OnCreate:

=============================

private void LoadActivity3()
{

  //Handle loading an activity, that is at high risk of crashing - activity 3 will have forced exception raised during load.

  try
  {
      Intent intent = new Intent(this, typeof(actActivity3));
      StartActivity(intent);
  }
  catch (Exception)
  {
  }

  //NEVER REACHED!! despite exception handled.  Crashes this activity, as well as called activity (activity 3).
}

======================

And here's the Activity3 OnCreate:

======================

protected override void OnCreate(Bundle savedInstanceState)
{
  ...
  //Crash the activiy, before it loads
  Crash();
  ...

======================

Activity1 continues to run, so you can repeat steps 2, 3 above.

Note You need to log in before you can comment on or make changes to this bug.