Home All Groups Group Topic Archive Search About
Author
12 Dec 2006 4:56 PM
GK
what is the best way to handle runtime error for VB.net. back on VB6, I
normally used
On error goto handle_err
.....
handle_err:
....

Under VB.net, is this still a good method? thanks.

Author
12 Dec 2006 5:03 PM
Marina Levit [MVP]
Try/Catch blocks.

Show quoteHide quote
"GK" <G*@discussions.microsoft.com> wrote in message
news:8978175E-92A4-4759-B416-74F42BB26CC7@microsoft.com...
> what is the best way to handle runtime error for VB.net. back on VB6, I
> normally used
> On error goto handle_err
> ....
> handle_err:
> ...
>
> Under VB.net, is this still a good method? thanks.
>
Author
12 Dec 2006 5:03 PM
lord.zoltar
GK wrote:
> what is the best way to handle runtime error for VB.net. back on VB6, I
> normally used
> On error goto handle_err
> ....
> handle_err:
> ...
>
> Under VB.net, is this still a good method? thanks.

I guess it depends on the error...
If the runtime errors you are getting are unhandled exceptions, put the
code that causes them into a Try...Catch block.

Example:
try
    sub_that_causes_exception()
catch e as exception 'you actually catch anything that inherits from
System.Exception, and then even have different catches for different
types of exceptions, if your "tried" code could cause more than 1 kind
of exception.
    do_error_handling() '
end 'code may have small syntax errors ;)

If you want to THROW an exception then:

if some_variable = bad_value then
    throw New BadValueException 'Assumes that BadValueException is a
defined class
end if

I'd suggest looking up on MSDN.com more on exception handlinging in
VB.NET. There's a lot more to it than what I just described. Ijust gave
the absolute basics.
Author
12 Dec 2006 10:33 PM
Herfried K. Wagner [MVP]
"GK" <G*@discussions.microsoft.com> schrieb:
> what is the best way to handle runtime error for VB.net. back on VB6, I
> normally used
> On error goto handle_err
> ....
> handle_err:
> ...
>
> Under VB.net, is this still a good method?

I am sure that somebody will disagree, but I'd say yes, it's still a good
method when it is used correctly.  Especially for code migrated from VB6
it's the way to go without completely rewriting code.  However, check out
'Try...Catch...End Try' and 'Throw'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>