1

It was always a good practice not to catch and not to throw common System.Exception and if you catch System.Exception, rethrow it using the throw keyword. So in .NET Framework we always can open a method description and find out what it can throw: msdn.microsoft.com/en-us/library/fkbhht5w(v=vs.110).aspx That is clear for me and if anybody wants to discuss it or to argue– you are welcome. But in UWP/WinRT, please, take a look at Capture photos and video with MediaCapture, There is a code:

try
    {
        await _mediaCapture.InitializeAsync(mediaInitSettings);
        _isInitialized = true;
    }

...

    catch (Exception ex)
    {
        Debug.WriteLine("Exception when initializing MediaCapture with {0}: {1}", cameraDevice.Id, ex.ToString());
    }

It was strange for me to see such thing in official documentation, but, then I found out that a lot of UWP/WinRT methods can throw an exception of type System.Exception. And as I understand it’s so because UWP errors are reflected to System.Exception in C# . As I see, a lot of issues are hidden in that catch(Exception). For example, OutOfMemory will be recognized as camera initializing problem. And since we use “await” keyword a lot of exceptions from “Task” mechanism will be hidden as well. Furthermore, passing MCSD (universal apps) tests I saw a question about exception catching and “proper” answer was to catch every exception. And there was no one word about Application.UnhandledException and UnobservedException.

Finally, my questions are:

  1. Why UWP reflects errors to common System.Exception but not to ComException, for example, to follow c#/.NET best practices?
  2. Why UWP documentation does not cover Exceptions and HResult values for every method as it is in .NET Framework?
  3. MSDN example writers just don’t understand that it hides issues and brings new, or it’s just a new .NET potilic?
Evgeny Gorbovoy
  • 765
  • 3
  • 20
  • About why not to catch all: http://stackoverflow.com/questions/352899/exception-handling – Evgeny Gorbovoy Apr 09 '16 at 07:19
  • Related post: http://stackoverflow.com/questions/12586416/how-to-handle-winrt-exceptions-that-result-in-exception – Evgeny Gorbovoy Apr 09 '16 at 07:19
  • 2
    I doubt anybody but a Microsoft employee could answer the first two questions. That said, from my experience on developing applications for Windows Phone: a phone is a wild environment that isn't even remotely as stable as a desktop computer. I've seen in my crash dumps that pretty much all system functions throw exceptions that you don't except (because the phone itself is in unstable state). In such environment, forget all the wisdom coming from desktop programming, save yourself the pain and catch everything – Kevin Gosse Apr 09 '16 at 09:48
  • I can't agree with you, because I developed couple apps for windows phone Silverlight and all that acts pretty predictable. But if you are talking about winRT environment your case does not surprise me and my post is about that – Evgeny Gorbovoy Apr 09 '16 at 11:41
  • Same here, production app with numerous downloads will provide you a set of such interesting exceptions you have no idea how to reproduce or fix. – Alex Sorokoletov Apr 12 '16 at 17:20
  • I wrote to several authors about this. That article was changed: https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/basic-photo-video-and-audio-capture-with-mediacapture Now there is nothing about catch that is good. – Evgeny Gorbovoy Mar 07 '17 at 20:04

0 Answers0