Results 1 to 5 of 5

Thread: Try...Catch...Finally

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Try...Catch...Finally

    Last one, how many of you guys are using this rather than the on error goto statement in your aspx pages?

    What's the general preference of this one, as a rule, when are you meant to use it?

    Thanks!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322
    I am using this Try...Catch...End Try as it is more structured.
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    <raises hand>

    Easier to handle errors when they need to be handled without jumping around. Considering most every other language uses TRy catch...you should not use on error anymore. Besdies, the Exception object is so much cooler anway.

    Cool tip:

    Catch ex As Exception

    Dim errorlocation As String = ex.TargetSite.Name

    errorlocation will return the sub/function name where the error occurs. Very handy. using this can demonstrate another reason why try catch is better. build a try catch in one sub..Call another sub with no error handler and raise an error.

    ie:
    Error 91

    then use that code up above in the Catch statement for the orginal sub. Even though the error happens in the called sub, the orginal sub will catch the error and the errorlocation string will still return the name of the sub where the error really happened.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Member
    Join Date
    Dec 2000
    Location
    UK
    Posts
    39
    I use the Try..Catch...Finally structure, works a treat, especially useful in pages doing data acces (ie just about every .aspx known to man!!).

    I particularly like being able to nest them, it's just so much better than the old on error... statement.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    A nice feature of Try...Catch is Catch When, e.g.

    Catch When Err.Description = "Some error string for a non-numbered exception that you know exactly what the description will be and would like specific code executed for that error"

    Catch When Err.Number = 5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width