Changed casting test in try-catch to use the as-operator

Using the as-operator to make the code more simple and also so that
Visual Studio will not break in the debugger when using
first-change-exceptions