But what is this "Catch"?

The title of this post is exactly the question that I've received during the testing phase of a Windows Form application for a customer some days ago.

On this project there's a function that works on some data (essentialy, it does calculations) and to trap exceptions I've written something like this:

Try
  Catch dzex As DivideByZeroException
    '... do something .... 
  Catch aex As ArithmeticException
    '... do something ....
  Catch ex As Exception
    '... do something ....
  Catch
    Throw
End Try

What's this? What's the Catch at the end of the block?

This is my personal best practice to handle exceptions and maybe it's not so known to all: all languages that follow the Common Language Specification (CLS) generate exceptions that derives from System.Exception. Here I assume that all of you knows that Catch blocks are evaluated in order that they occour in your code, so you've always to place the most specific exception at the top of your block otherwise the code will not compile.

The Catch block that does not specify an exception type is the most general form of Catch and it's a best practice to handle exceptions generated by possible non CLS-compliant languages. I know that this is not so common, but here your code will be more secure.

Print | posted on Sunday, July 10, 2005 2:29 PM

Comments on this post

# re: But what is this "Catch"?

Requesting Gravatar...
Right! It is the Rule 19.12 from Balena & Dimauro's book
Left by Adrian Florea on Jul 11, 2005 7:37 AM

# re: But what is this "Catch"?

Requesting Gravatar...
Thanks a lot Adrian, nice to see that also our Regional Directors use this way to do... (and nice to see that a magician of CLR like you agree with me)
Left by Stefano Demiliani on Jul 11, 2005 7:45 AM

# casinos

Requesting Gravatar...
I will certainly bookmark people's site for the reason that a individual's articles turned enlightening. We consume a person's articles and depend on coming backside.
Left by casinos on Oct 03, 2011 11:45 AM

Your comment:

 (will show your gravatar)
 
Please add 6 and 4 and type the answer here: