Question regarding C# exception control flow

I feel like I already know the answer to this, but I just wanted to make sure I wasn’t missing anything.

I’ve seen developers, usually new, who wrap code in try / catch blocks, leaving the catch block empty, just to “swallow” exceptions, and allow the program to continue normal execution after a failure. I realize that this is not a good idea, and is not something I’d ever do personally.

However, I have a coworker who follows this pattern, but instead of having an empty catch block, he simply does:

catch (Exception ex) { throw; } 

Ignoring the unused reference to “ex” (which bothers the hell out of me), based on everything I know, code wrapped in a try/catch where the catch only throws is functionally identical to not having the try/catch at all. Assuming the type being caught is “Exception,” is there any use for code like this or is my coworker just thoroughly confused? There is no finally block – only try/catch.

submitted by /u/musical_bear
[link] [comments]

Leave a Reply