Every now and then, I encounter smart developers that seem to misunderstand how exceptions and exception handling works. Often regardless of language or technology stack. While I don’t see these issues daily, I thought it might be interesting to reiterate on some exception handling basics. Rethrow correctly In .NET, it’s important to rethrow exceptions correctly. This is a classic mistake for beginning developers, no shame in that. But I’ve seen it in code of more experienced developers too. Take this
On two projects I have worked on, I’ve seen a lot of this: public void DoSomething() { try { // actually do something here } catch (Exception ex) { throw new Exception(“Unable to do something”, ex); } } I’ve seen this in various differing styles: not including the original exception in the newly thrown one (ouch!)* having a custom Exception type, indicating where in the code/assemblies the exception happened.Reasons seem to be a desire to easily see where the exception