Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Try Catch When

    This is a feature that's been in VB (.NET) since day one and is very cool.  If you've never used it before, what it does is add a condition to your catch statement so the catch is only executed the condition is met.  I've never really had much of a use for it, but it's good to know it's there.  A couple days ago though, I had a really good use for it though, even though a bit unconventional.  My scenario is basically that after a long process a webservice is called to update some things.  In the event of a failure, it saves what needs to go up to an xml document to try again later.  The "try again later" times are when the application loads or when they click on a certain button.  When the application loads, if there was a failed call from before, I want the app to try the call again, but don't do anything if it fails and just move on with loading.  When they click the button, I want it to also try the webservice call again, but this time display an exception or message or whatever with what went wrong.  Try Catch When is perfect for this.  Here's the code:

    Public Shared Function SaveChangesToServer(ByVal ShouldThrowException As Boolean) As Boolean
        
    Dim Success As Boolean =
    False

        
    If GetHasChanges()
    Then
             
    Try
                  
    'Execute WebService Call

                  
    Success =
    True
             
    Catch Ex As Exception When
    ShouldThrowException
                  
    'Clean-up
                  
    Throw
    Ex
              Catch
                  
    'Clean-up
              End
    Try
        
    End
    If

         Return
    Success
    End Function

    Well, I think it's handy anyway...go VB!  ;)

    UPDATE: Thanks, Steve for pointing out my screw up.  Appreciate it!  :)  Who knows, maybe I have more.  Good to know I have readers checking this stuff out though!  ;)

    Comments

    Steve Hiner said:

    If ShouldThrowException is False won't that code throw an exception anyway since the original exception won't get caught? I'd expect it to look more like:

    Catch ex as Exception When ShouldThrowException
    Throw
    Catch ex as Exception
    'Swallow exception
    End Try

    Also, it's my understanding that "Throw Ex" will make the resulting exception look like it came directly from your routine whereas "Throw" will maintain all the original stacktrace and everything.

    Am I right on either issue?
    # August 10, 2005 3:04 PM

    Erik Porter said:

    Hi Steve, yes, sorry, you're right. I had altered this code a few times and tried to make it not "look" like anything specific I was doing in my app. I'll fix that.

    As for the Throw Ex, I honestly don't know, but you're probably right. Either way though, wouldn't I still get basically the same stack trace?
    # August 10, 2005 3:14 PM

    Shane said:

    The standard advice I've seen is to use Throw as opposed to Throw Ex specifically for the reason Steven gives.

    http://weblogs.asp.net/bhouse/archive/2004/11/30/272297.aspx
    # August 10, 2005 3:20 PM

    Erik Porter said:

    Shane, yes, when I do not catch an actual exception at all (Catch) I just call Throw instead of Throw Ex, but I figured in this case, since I am catching Exceptions (Catch Ex As Exception) I would need to throw it. If I've already caught the exception and I just call throw does it still work properly?
    # August 10, 2005 3:25 PM

    jayson knight said:

    The only time you should catch a specific exception is if you are going to handle it, otherwise specify a generic catch, and throw the exception to (hopefully!) be handled upstream somewhere.
    # August 10, 2005 8:57 PM