Compartilhar via


You learn something new everyday...

Ok, I know that Paul knew this, but did anyone else notice that the VB.NET Try...Catch syntax includes a 'when' clause?

  •  Try
       [ tryStatements ]
    [ Catch [ exception [ As type ] ] [ When expression ] 
       [ catchStatements ] ]
    [ Exit Try ]
    ...
    [ Finally
       [ finallyStatements ] ]
    End Try
    
  • When

  • Optional. A Catch statement with a When clause will only catch exceptions when expression evaluates to True. A When clause is only applied after checking the type of the exception, and expression may refer to the identifier representing the exception.

I never did! I can't think of a good use for it right at this moment, but I'm still shocked that I never noticed it before... now what to do with it?

Comments

  • Anonymous
    July 25, 2003
    This is one of those cases where a feature of the CLR is exposed to VB.NET and not to C#.

    It's not something I've ever used, but I've seen a few examples that make sense.
  • Anonymous
    July 25, 2003
    As you may have seen, professionally, I am a VB.NET developer now (still write C# after hours) so thanks for the tip. I will find someway to use it one of these days.
  • Anonymous
    July 25, 2003
    The comment has been removed
  • Anonymous
    July 25, 2003
    Knew it but took a while to get my mind around what it was good for.
  • Anonymous
    July 25, 2003
    Never new that. I find myself just looking up syntax as I need it (former C++, Java and C# guy that picked up VB.NET).

    To me it would be great if you were constructing lots of objects in a try block and you were not sure which failed. This assumes that you could do things like:
    try
    ...
    Catch ex as exception where var is Nothing
    Catch ex as exception where var2 is Nothing
    End Try

    I'll try it sometime.
  • Anonymous
    July 25, 2003
    The comment has been removed
  • Anonymous
    July 25, 2003
    I forgot to mention, if you take a look at the example I gave:

    Catch ex As OutOfRangeException When i < 0

    You can see that the When condition clearly enables you to limit the scope of your catching down to exceptions that you are expecting could occur in the normal course of your program flow.

    Now, that's the theory; would I ever use it - NAH! Probably not, I think I'll just stick to:



    Try
    ' insert 800 lines of code
    Catch
    ' do nothing
    End Try




    :P


  • Anonymous
    July 25, 2003
    Is it of any use? I mean: there is an exception, OR you handle it at that level OR you don't, but that doesn't depend on a state of a variable, but the exception itself. Inside the catch clause, if you decide to bubble the exception, youcan always rethrow it. Simply a weird construct that's IMHO not that useful.
  • Anonymous
    July 28, 2003
    I knew it existed because it's mentioned when discussing security. The Where clause code is executed when the runtime is trying to determine how far up the stack to unwind, but the stack is still in place, so there's lots of (stack-based) security scenarios to consider...

    IMHO there's nothing wrong with "weird" rarely used constructs -- as long as they're not going to be removed from the language!
  • Anonymous
    September 19, 2003
    thanks guys...
    http://weblogs.asp.net/sbchatterjee/posts/28362.aspx