Monthly Archives: September 2016

How to catch multiple types of exceptions in one catch block

Catch ’em all

Last week a colleague asked: what’s the best way to catch different types of exceptions that need to be handled in the same way? He saw a couple of options himself, for example:

Or an other proposal:

Exception Filters

While both of these options work, since C# 6 there is an other way of dealing with this. Since C# 6 we have Exception Filters that allow us to ‘filter’ our catch-blocks.

So, for example, if we only want to catch our exceptions when the Logger property on our current object isn’t null, we could write the following:

In this when-clause we can basically write any expression that returns a bool.

Thus, if we want to handle multiple types of exceptions in the same way using Exception Filters, we could write this:

One could argue if this approach is better compared to the first two proposals…

Well, it is definitely better than the second one! Why? Because if we would just catch all exceptions and do checks inside that particular catch block, we are always unwinding the stack. Even if we decide -because of the checks- to do nothing with that exception and re-throw it.

For this particular scenario, both the first example and the Exception Filtering approach are good solutions. Honestly, I don’t know which approach I prefer… I do like Exception Filters, but I don’t know if we should use it for this particular scenario…

Category: C#

Fix Visual Studio 2015 random crash

Visual Studio 2015 randomly crashing -> FIX!

A colleague of mine was having issues with Visual Studio 2015. Every now and then his instance of Visual Studio would just crash. He thought it would crash more often when pressing CTRL+X or Backspace… but I don’t believe him, I think it was just random!

After trying out some things (including repairing Visual Studio), we finally could fix the issue by doing the following:

  • Start Visual Studio in ‘Safe Mode’Start Visual Studio in Safe Mode
  • Inside Visual Studio, go to Tools -> Import and Export Settings -> Reset all settings and complete the wizard
  • Restart Visual Studio normally

That did the trick!

UWP Simple PDF viewer

Some months ago, I blogged about how to display PDF files in your UWP app: Displaying PDF files in a UWP app

When I look at the stats of my blog, I notice that that particular blogpost gets a lot of visitors. Also, I regularly get mails from people having questions about displaying PDF files in a UWP app.

So, I figured out that this is something a lot of developers seem to need or want to use. And that’s why I thought I’d make a custom control for this exact purpose.

You can find the control, together with the source code, on my GitHub: SimplePdfViewer


pdf print screen

If you have any suggestions for this control, please let me know! I’ll try to gradually keep on improving this control.

UWP app on Xbox One: controller input gotcha (KeyDown event)

I was trying out some stuff on my Xbox One (well technically not mine, but that doesn’t matter). More particularly I was testing out UWP for Xbox One.

One of the first things I wanted to try out was how to get input from the Xbox One controller. And so I hooked up the KeyDown event of the Windows’ CoreWindow.

As the VirtualKey enumeration, which I get back in the KeyEventArgs and represents the key being pressed, has values like ‘GamePadA‘ or ‘GamePadLeftShoulder‘, I thought that I already found a way to get input from the game controller.


Well, this blogpost is about a ‘gotcha’, so you would have probably guessed that there is a small caveat.

It turned out that when I ran my UWP app on my Xbox One, that pressing any button -except for the B button- nothing would happen. I didn’t see anything appearing in my Output window. Only when pressing the B button, ‘GamepadB‘ was printed out. Strange…

After looking up some things, I found out that by default UWP apps on Xbox One run in ‘mouse mode’. So my controller is more or less treated like a mouse. Hence the ‘pointer’ on my screen:

Xbox Mouse Pointer

In order to disable this ‘mouse – or pointer mode’ I had to set the RequiresPointerMode property of my App class to ‘WhenRequested‘. Like this (App.xaml.cs):

This is, by the way, recommended for a UWP app that needs to run on a Xbox One. Because, you could also control your Xbox with a remote-like controller where you don’t have a thumbstick.

When I start up my application now, I no longer see this mouse pointer. But, when I now press a button on my controller I see that my Output Window is printing out the expected values.


So, now the KeyDown event is triggered every time we press a button on the controller and through the VirtualKey property of the passed in KeyEventArgs we can easily determine which button the user has pressed on his controller.

Having no mouse pointer in your screen, however, means that we should think through our UI when building UWP apps for Xbox One as we now navigate through our app using the controller’s D-pad or left thumb stick, but not as if we are using some sort of mouse pointer. But more on this in a later blogpost.


When building a UWP app that needs to run on Xbox One, you should set your App’s RequiresPointerMode to ApplicationRequiresPointerMode.WhenRequested. That way the KeyDown event of the CoreWindow is triggered and works as expected. This is the recommended mode in which your app should run on Xbox One.


Setting the RequiresPointerMode to ‘WhenRequested‘ means that we no longer see a pointer on the screen. However, in some scenarios you might want the user to interact with a particular control using the pointer. For example, on a Custom Control on which you would like the user to interact using ‘mouse controls’, you can set the RequiresPointer and IsFocusAngagementEnabled properties, like this:

Now every child in my custom control will be in ‘mouse mode’ as well so the user will see a pointer on the screen when het interacts with my custom control.



I hope this blogpost could be of any help to anyone who’s also been struggling with controller input!
In a later posts I will be discussing things like  IsFocusAngagementEnabled, how to efficiently allow the user to navigate through your app (when not in mouse mode), …