by Pxtl · 12y ago · 0 comments
Using static is great - finally we've liberated the verbs in the kingdom of nouns.
However, some of them have really ugly syntax. I know they wanted to get rid of the boilerplate problem of declaring and setting into a read-only member, but the primary constructor syntax is monstrous - declaring local variables inside of your constructor? Really? I feel like just having a "readonly set" access modifier on properties (both auto and explicit) would have been sufficient.
The new dictionary accessor seems silly.
Exception filters seem just as pontless in C# as they did in VB (nesting an "if/else throw" into a catch clause wasn't too hard).
The crazy null ?. operator I'm unsure about... that one I'll have to use. I've run into the problem enough times to see why they did it, though.
That is
catch(Exception ex) {throw;}
Rethrows without meddling with ex.
No it doesn't. If you then let that exception fall out as unhandled, the stack trace will be centered on the second throw, not on the original.
In one case you
try { Throws(); } catch(Exception ex) { DoHandling(); throw ex; } // BAD!
This one does lose the initial site of the first exception, and throws it as a new exception at this location, centering the stack trace on this line.
This is what you want to do:
try { Throws(); } catch { DoHandling(); throw; }
Notice you do not name, or capture the exception. This causes your handling to execute, but crucially it re-throws the original exception untouched, with the original stack trace, centering on the original exception thrown location.
So you can easily lose that information if you re-throw naively. Good code tools should yell at you for doing this most of the time. Note in some cases you want to add information, so you can throw a new exception with new information, but set the InnerException property to the original causing exception.
See http://msdn.microsoft.com/en-us/library/system.exception.inn...
If the exception was rethrown in a method that is
different than the method where it was originally
thrown, the stack trace contains both the location
in the method where the exception was originally
thrown, and the location in the method where the
exception was rethrown
Putting aside same method/different method, the original stack trace is preserved, but StackTrace is augmented with the location the exception was rethrown.There's also a code analysis warning for misuses of throw that uses "preserve" in its title [2].
It says in your profile that you work on Roslyn and I imagine you're familiar with how the exception syntax works with the CLR, so maybe you know something I don't know.
[1] http://msdn.microsoft.com/en-us/library/system.exception.sta...