Filed Under: .net
Exception handling is important enough to get right. And there’s plenty of sample code out there to help you get it right, nonetheless, i come across two major variations:
catch (Exception ex) {
throw;
}
and
catch (Exception ex) {
throw ex;
}
There’s more about catching general Exception but that’s beyond the scope of this post. So, moving right along…
There is one other variation:
catch (Exception ex) {
throw new Exception(ex);
}
But the first two are more interesting. The second (throw ex;) is the least desirable, unless of course you have a good reason to not preserve the stacktrace.
Code attached rethrow.txt
Subscribe to comments feed (this is global, not just for this entry)
The time has come, the Walrus said… Not moving very far, but consolidating the technology and personal ranting all into one uber blog. New location (location, location, location) is http://bryanallott.net/blog/. Updated feed URL (http://feeds.feedburner.com/bryanallottnet)
I say “industry” but there’s no real regulation put in by the government (at least here) which keeps the industry in check. For one, it’s not illegal to provide IT services or build software without a licence, while in more established industries, it is illegal to, for example, provide medical, financial, engineering or manufacturing services […]