Results 1 to 8 of 8

Thread: [RESOLVED] Catching errors syntax

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Resolved [RESOLVED] Catching errors syntax

    Hi There Guys,

    I'm just curious as to which one of these 3 error syntaxes in the try/catch is right.

    code:

    vb.net Code:
    1. try
    2.  
    3.         Catch ex As Exception
    4.  
    5.             MessageBox.Show(ex.Message.ToString())
    6.             MessageBox.Show(ex.Message)
    7.             MessageBox.Show(ex.ToString)
    8.  
    9.         End Try

    Is there any advanatges to either of them or all they all ok?

    thanks for any info guys

    Graham

  2. #2
    Lively Member
    Join Date
    Sep 2009
    Posts
    115

    Re: Catching errors syntax

    Not sure really what you are asking...

    The syntax is:

    Try
    <Your program code here>
    Catch ex As Exception
    <Your error handler code here>
    End Try

    http://msdn.microsoft.com/en-us/libr...5z(VS.71).aspx
    http://msdn.microsoft.com/en-us/libr...tz(VS.71).aspx

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Catching errors syntax

    It depends on what information you need when the exception is caught. Did you run all 3 ? Did you see one that gives you more information than another?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Catching errors syntax

    I would suggest that none of the three of them is appropriate as is. When you're logging an error for your own benefit as the developer then I would suggest that you should use ex.ToString because that gives you the most information. You may want to log other information too. When you're notifying a user of an error though, you'll want to provide your own error message in plain English, or plain some-other-language . You might want to display the Message property of the exception too, but that will rarely be enough on its own for a user unless they are of a technical persuasion.

    There's also no point calling ToString on the Message property as it is already a String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Re: Catching errors syntax

    Hi Guys,

    I had found that MessageBox.Show(ex.ToString) gives you a little bit more information as well as the line number of the error which is always good lol, the only one i never tried was MessageBox.Show(ex.Message.ToString())

    Thanks Guys

    Graham

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Catching errors syntax

    Do you really want your users to see all that information when an error occurs? ex.ToString also shows you the call stack and things like that, which are definitely of no use to the user even if he is a programmer himself.

    I usually create a (shared) class that handles all errors. Instead of the MessageBox.Show method, I create a new (shared) method in that class (I usually call it WriteError or something) that takes the exception object as a parameter.

    Then I create an 'error form' where I display my own error message in a label, and there is a button "Details" that shows the ex.Message.

    Finally, I write the ex.ToString to a log file, including the time and date.

    This way, the user always has the most basic information, but can ask for a little more (using the details button). I can then read the log file and I can see what exactly went wrong without the user having to bother with deciphering what it means.


    All that is done in that single method, so I only need to write WriteError(e) where you wrote MessageBox.Show, and it does the same, and much more, than your code.

    This is just the way I usually do it and there's certainly other ways.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Re: [RESOLVED] Catching errors syntax

    Hi Nick,

    That sounds like an ideal way to do things! and much more efficient.

    So instead of: MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) have something like: writeError(ex.ToString) (which goes into a sub/function) that does all the work, displays a custom form, with a "Details" button which shows the error in more depth?

    also see the log file you would create (i know this is small lol) would you use the 1 log file say "log.txt" then append to it, or create a new .txt file on error?

    thanks Nick

    Graham

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Catching errors syntax

    Something like that yeah. But don't pass ex.ToString, just pass the whole exception object (just ex), which carries all the information you'll need.

    I usually create one log file but it doesn't really matter I suppose.

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