|
-
Sep 28th, 2009, 07:11 PM
#1
Thread Starter
Hyperactive Member
[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:
try
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
MessageBox.Show(ex.Message)
MessageBox.Show(ex.ToString)
End Try
Is there any advanatges to either of them or all they all ok?
thanks for any info guys
Graham
-
Sep 28th, 2009, 07:22 PM
#2
Lively Member
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
-
Sep 28th, 2009, 07:29 PM
#3
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
-
Sep 29th, 2009, 12:54 AM
#4
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.
-
Sep 29th, 2009, 02:51 AM
#5
Thread Starter
Hyperactive Member
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
-
Sep 29th, 2009, 04:24 AM
#6
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.
-
Sep 29th, 2009, 05:13 AM
#7
Thread Starter
Hyperactive Member
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
-
Sep 29th, 2009, 05:50 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|