OK, I know I said my next post would be about my first open-source project, but… well, I’m not quite ready to write about that yet. (Although mysteriously it’s already had 14 downloads? Anyway, you can click on the link if you’d like to know about it.)
I periodically send e-mails to the other members of my development team at work, just to provide bits of useful advice garnered from my… well, my reading about nerdy things. Anyway, one of my coworkers suggested that I post this info in my blog, which (potentially — though certainly not yet) many people could read, rather than in e-mails that only a few people read.
That made sense to me, so, without further ado: I give you the first post in my new “Advice” category.
(By the way, posts in this category will often be in VB.NET, since two of my three teammates primarily code in VB.NET.)
Re-throwing caught exceptions
Just a little FYI:
If you ever write any code and you want to “re-throw” an exception (e.g., if you want to log it and then pass it along), avoid doing this:
Try ' some code here Catch ex As Exception ' some code to log error Throw ex End Try
The reason is that the “Throw” keyword has two forms. One is “Throw [some exception]”, which causes the callstack to unwind and indicate the current point in code (what you see in a Stack Trade in an exception’s details). The other is simply “Throw” (no argument), which forwards on whatever exception is currently being passed up to the next exception handling code. “Throw ex” in the code above obviously falls into the first category, which means it unwinds the stack and you therefore potentially lose a lot of information about what happened up until that moment in code. The preferable alternative is to simply write “Throw”:
Try ' some code here Catch ex As Exception ' some code to log error Throw End Try
Yes, I know, I am a huge nerd.