Subscribe to the RSS feed then follow me on twitter at @mrlacey

Sunday, August 14, 2011

Catching an uncatchable exception

Recently we had an interesting issue in a Windows Phone 7 app I was working on that proved awkward to address. The code in question related to playing a video via the MediaPlayerLauncher.

The code in question looked like this:
                    new MediaPlayerLauncher { Media = fileUri }
                        .Show();
The issue we had was that if the user triggered the launching of the MediaPlayer and then very quickly hit the back button then they could get the following error.

"Not allowed to call Show() when a navigation is in progress"

As a first we tried wrapping the above code in a simple try..catch block to handle the unhandled exception:
                try
                {
                    new MediaPlayerLauncher { Media = fileUri }
                        .Show();
                }
                catch (Exception exc)
                {
                    ...
                }
But this didn't work.

Next we tried to determine if navigation was in progress by comparing the CurrentSource and Source properties of the page but that didn't work either.

Then I had an idea.
What if we split the separated the generation of the object and the calling of the method?:
                        var mediaLauncher = new MediaPlayerLauncher();
                        mediaLauncher.Media = fileUri;

                        try
                        {
                            mediaLauncher.Show();
                        }
                        catch (InvalidOperationException exc)
                        {
                            ...
                        }
This did allow us to catch and handle the exception. Yay!

I'm a big fan of not writing any code I don't have to and so having to write more than I think should be absolutely necessary here is a little bit disappointing. It's good to know, however that this isn't an exception I can't do anything about.
In this situation I can happily ignore this exception as if the user has hit back shortly after trying to lauch the video it's reasonable to assume they don't actually want to watch it.

If you've built Windows Phone or Windows Store apps you can cross-promote them with AdDuplex to get more users.

1 comment:

  1. Mother's Day is fast approaching, Mother's day time is May 8, 2011. Accept you purchased the mom aural your actuality a gift? If not, don't worry, appropriate listed actuality are some areas you can abdicate by these times to gucci replica locate the able Mother's day time gift. When it comes to handbags, artist handbags are not the alone ones accessible in the market. You accept a best of affairs appropriately adorable and able-bodied fabricated handbags from added manufacturers of covering and added actual handbags. Artist handbags are about expensive, but this is not the case with the accustomed handbags.There are a lot of accoutrements that are frequently seen, and some of the a lot of accepted types of breitling replica another artist handbags that are specialty accoutrements which are acclimated a part of adolescent ladies and aswell some multipurpose bags. This bag is actual acceptable for parties as the bag looks actual elegant.With the internet accepting accepted day by day, online arcade is possible. You could consistently adjustment articles of your choice, be it a betty boop handbags or a replica of a celebrity handbags .You aswell get to acquirement affected milano accumulating handbags, rome accumulating handbags etc. The manufacturers yield a lot of affliction and accomplishment to ensure that the chanel replica alike is as abutting to the aboriginal and even in agreement of the quality, designs, colors and sizes offered, they do not wish to be apparent as getting larboard behind. They apperceive that there is a huge bazaar out there for such articles and as continued as they are able to accommodated some of the requirements of the original, they can advertise a abundant amount of such products. A altered artist purse has the adeptness to acclaim and accent an boilerplate searching dress, thereby authoritative you attending a actor dollars. An accurate artist bag can and will get you noticed in a awash allowance abounding of humans and can actually accessible doors for you.You should accept acceptable qualityalternative artist handbags as these accoutrements are replica of branded prada belts.There are abounding handbags like milano accumulating handbags, rome accumulating handbags and abounding added which can avialble at your adjacent arcade store.

    ReplyDelete