Saturday, May 1, 2010

Going Going Gone.. catch(T exception)

While writing some diagnostic code I got clever and tried to use a generic in a catch statement. After spending some time head scratching I came across this stackoverflow article http://stackoverflow.com/questions/1577760/why-cant-i-catch-a-generic-exception-in-c
apparently while this code compiles without error it results in an uncaught exception. The article refers to a defect in the compiler which indicates it is version specific. A good work around is to use the following code block instead:
catch(Exception exception) { if(exception is T) {//Your specialized code here;} }

No comments:

Post a Comment