C# multiple catch blocks
-
I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.
-
I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.
Hi, if you want to catch *all* Exception types and threat them identically, then no there is no use in having separate catch blocks. BTW: logging is fine, not dealing with the Exception probably is a poor choice, since (unless you rethrow the Exception) the caller will be unaware something went wrong. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, if you want to catch *all* Exception types and threat them identically, then no there is no use in having separate catch blocks. BTW: logging is fine, not dealing with the Exception probably is a poor choice, since (unless you rethrow the Exception) the caller will be unaware something went wrong. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.
proganon wrote:
Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way?
A resounding No. The only place where this would be valid is if you wanted to catch and handle CustomException1 and CustomException2 in the same way and leave the rest for calling code to handle, where said Exception types are unrelated to each other.
Cheers, Vikram.
Current activities: Films: Philadelphia TV series: Friends, season 4 Books: Six Thinking Hats, by Edward de Bono.
Carpe Diem.
-
I have read many articles on expception handling in C# and I have a question regarding the use of multiple catch blocks. Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way? NOTE: At the moment I am only adding exception logging to the application not exception handling.
proganon wrote:
Is there any purpose for using multiple (specific) catch blocks if all exceptions will be handled the same way?
Yes. This provides the ability to handle specific exceptions in a sensible fashion - for instance, you might have a bit of code that updates a database, and the SQL Connection terminates unexpectedly during the operation - you might want to attempt the update again (having reopened the connection). If you get a different type of exception, you probably wouldn't want to attempt the update again. There's just one example of doing this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.