By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Resume next in VB.NET ?

P: n/a
In VB 6.0 in the error trapping, we can do "resume next" to continue on the
next code. How can we do that in .NET with "Try", "Catch","End Try" ?
Thanks
Jul 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"fniles" <fn****@pfmail.comwrote in
news:OA**************@TK2MSFTNGP05.phx.gbl:
In VB 6.0 in the error trapping, we can do "resume next" to continue
on the next code. How can we do that in .NET with "Try", "Catch","End
Try" ? Thanks
Try

Catch ex As Exception
Response.Write(ex.message)
End Try

Does that help?
Jul 14 '06 #2

P: n/a
There is an equivalent, but it's not very attractive:
Try
<statement 1>
Catch
'do nothing
End Try
Try
<statement 2>
Catch
'do nothing
End Try
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"fniles" wrote:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the
next code. How can we do that in .NET with "Try", "Catch","End Try" ?
Thanks
Jul 14 '06 #3

P: n/a
It's still there (unfortunately).

You can say: On Error Resume Next.

However, this does not work with try/catch. It's one or the other.

My recommendation is to get rid of On Error Resume Next, and always use
Try/Catch in the appropriate places. Usually if an unexpected run time
error ocurred, you don't want to execute any more statements - so I never
really understood why you would want to Resume Next. If an error happens
that you were not expecting, you need to handle it, and get out
appropriately.

"fniles" <fn****@pfmail.comwrote in message
news:OA**************@TK2MSFTNGP05.phx.gbl...
In VB 6.0 in the error trapping, we can do "resume next" to continue on
the next code. How can we do that in .NET with "Try", "Catch","End Try" ?
Thanks

Jul 14 '06 #4

P: n/a
Thank you all for your replies.
In VB 6 at the top of a sub I used to put "on error goto err_routine", and
in err_routine I will write the error to a file and do resume next,
because the "resume next" is helpful during debuging a program, because I
can put a break point on the Resume Next and when I hit F8 it will go to the
next code so that I know where the error occurs.

In VB.NET where is the best place to put Try/catch, because if I put it only
for 1 line of code, it won't catch error on the other lines of code in that
same subroutine.
So, I ended putting Try/catch at the top of the subroutine, and include all
the codes in that subroutine under "Try". When I get an error, even knowing
ex.message sometimes it is difficult to know which line of code causes the
problem, and I would like to know which line is it that produced the error.
With resume next, I will be able to tell during debuging.

Is there any way to do this ?

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:Ok**************@TK2MSFTNGP04.phx.gbl...
It's still there (unfortunately).

You can say: On Error Resume Next.

However, this does not work with try/catch. It's one or the other.

My recommendation is to get rid of On Error Resume Next, and always use
Try/Catch in the appropriate places. Usually if an unexpected run time
error ocurred, you don't want to execute any more statements - so I never
really understood why you would want to Resume Next. If an error happens
that you were not expecting, you need to handle it, and get out
appropriately.

"fniles" <fn****@pfmail.comwrote in message
news:OA**************@TK2MSFTNGP05.phx.gbl...
>In VB 6.0 in the error trapping, we can do "resume next" to continue on
the next code. How can we do that in .NET with "Try", "Catch","End Try" ?
Thanks


Jul 14 '06 #5

P: n/a
You're struggling with one of the major differences between unstructured
error handling (VB6) and structured error handling (VB.NET - all versions).
In VB6, each Sub or Function could only have a single error block, which
translated into additional, smaller, superflous, subs and functions to nest
error handling. In VB.NET, you can have multiple, nested error handlers.
Here's how to do it -

For each section of code you wish to protect, put the keyword "try" before
it. At the end of the protected code, put the keyword "Catch" with an
exception type to handle. You can catch different types of exceptions for
the same try block. Finally, if there is any clean up code that you need to
execute before leaving the "try" block, use the keyword "Finally" and put
the code after it. The last line of the try block is "End Try"

try
' Protected Code
catch fEx as system.FileException
' File errors
catch ex as Exception
' All other errors
Finally
' clean up code - release file handles, etc.
end try

At any time, you can nest try...catch...[finally]...end try blocks, even
inside a catch or finally handler. An easy way to start converting is to
find your On Error Goto statement and replace it with Try. At the error
label, replace this with Catch ex as Exception. At the end of the routine,
insert an End Try. If you have a resume statement, replace it with
"Finally" (before the end try) and move the code from the resume target into
the finally block.

For example -

sub MySub()
On error goto myErr
' Protected Statement

ResumeHere:
' Clean up code
exit sub

myErr:
' Error Handler
resume ResumeHere
end sub

becomes

sub MySub()
Try
' Protected Statement

Catch ex as exception
' Error Handler - note: you can use multiple catch statements, one for
each type of error you expect to have; If none of your catch statements can
handle the error, it is automatically propagated up the call stack to the
next level of error handling.

Finally
' Clean up code
End Try
end sub

As for "On Error Resume Next", it's still useful, especially if you are
configuring an environment for your application and some parts of the
environment already exist and you would get an error back when you attempt
to configure previously existing parts. Keep these sections of code as
short as possible (I never need more than 10 lines under this type of
control).

All uses of On Error Goto ... should be immediately replaced with try
blocks.

Mike Ober.

"fniles" <fn****@pfmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Thank you all for your replies.
In VB 6 at the top of a sub I used to put "on error goto err_routine", and
in err_routine I will write the error to a file and do resume next,
because the "resume next" is helpful during debuging a program, because I
can put a break point on the Resume Next and when I hit F8 it will go to
the
next code so that I know where the error occurs.

In VB.NET where is the best place to put Try/catch, because if I put it
only
for 1 line of code, it won't catch error on the other lines of code in
that
same subroutine.
So, I ended putting Try/catch at the top of the subroutine, and include
all
the codes in that subroutine under "Try". When I get an error, even
knowing
ex.message sometimes it is difficult to know which line of code causes the
problem, and I would like to know which line is it that produced the
error.
With resume next, I will be able to tell during debuging.

Is there any way to do this ?

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:Ok**************@TK2MSFTNGP04.phx.gbl...
It's still there (unfortunately).

You can say: On Error Resume Next.

However, this does not work with try/catch. It's one or the other.

My recommendation is to get rid of On Error Resume Next, and always use
Try/Catch in the appropriate places. Usually if an unexpected run time
error ocurred, you don't want to execute any more statements - so I
never
really understood why you would want to Resume Next. If an error
happens
that you were not expecting, you need to handle it, and get out
appropriately.

"fniles" <fn****@pfmail.comwrote in message
news:OA**************@TK2MSFTNGP05.phx.gbl...
In VB 6.0 in the error trapping, we can do "resume next" to continue on
the next code. How can we do that in .NET with "Try", "Catch","End Try"
?
Thanks



Jul 17 '06 #6

P: n/a
Thank you for your reply. It is very helpful.
>For each section of code you wish to protect, put the keyword "try"
before
it.
I really want to protect all codes in the subroutine, so replacing the "On
Error Goto" statement at the top of the sub with the Try is a good idea
(otherwise I will have to put "try/catch" everywhere in the subroutine)
The problem with this is, when I get an error, I really do not know which
section of the code the error comes from, while in VB6, with Resume Next
statement in my error label section, I can know which code it comes from.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:dV*****************@newsread2.news.pas.earthl ink.net...
You're struggling with one of the major differences between unstructured
error handling (VB6) and structured error handling (VB.NET - all
versions).
In VB6, each Sub or Function could only have a single error block, which
translated into additional, smaller, superflous, subs and functions to
nest
error handling. In VB.NET, you can have multiple, nested error handlers.
Here's how to do it -

For each section of code you wish to protect, put the keyword "try" before
it. At the end of the protected code, put the keyword "Catch" with an
exception type to handle. You can catch different types of exceptions for
the same try block. Finally, if there is any clean up code that you need
to
execute before leaving the "try" block, use the keyword "Finally" and put
the code after it. The last line of the try block is "End Try"

try
' Protected Code
catch fEx as system.FileException
' File errors
catch ex as Exception
' All other errors
Finally
' clean up code - release file handles, etc.
end try

At any time, you can nest try...catch...[finally]...end try blocks, even
inside a catch or finally handler. An easy way to start converting is to
find your On Error Goto statement and replace it with Try. At the error
label, replace this with Catch ex as Exception. At the end of the
routine,
insert an End Try. If you have a resume statement, replace it with
"Finally" (before the end try) and move the code from the resume target
into
the finally block.

For example -

sub MySub()
On error goto myErr
' Protected Statement

ResumeHere:
' Clean up code
exit sub

myErr:
' Error Handler
resume ResumeHere
end sub

becomes

sub MySub()
Try
' Protected Statement

Catch ex as exception
' Error Handler - note: you can use multiple catch statements, one for
each type of error you expect to have; If none of your catch statements
can
handle the error, it is automatically propagated up the call stack to the
next level of error handling.

Finally
' Clean up code
End Try
end sub

As for "On Error Resume Next", it's still useful, especially if you are
configuring an environment for your application and some parts of the
environment already exist and you would get an error back when you attempt
to configure previously existing parts. Keep these sections of code as
short as possible (I never need more than 10 lines under this type of
control).

All uses of On Error Goto ... should be immediately replaced with try
blocks.

Mike Ober.

"fniles" <fn****@pfmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
>Thank you all for your replies.
In VB 6 at the top of a sub I used to put "on error goto err_routine",
and
in err_routine I will write the error to a file and do resume next,
because the "resume next" is helpful during debuging a program, because I
can put a break point on the Resume Next and when I hit F8 it will go to
the
>next code so that I know where the error occurs.

In VB.NET where is the best place to put Try/catch, because if I put it
only
>for 1 line of code, it won't catch error on the other lines of code in
that
>same subroutine.
So, I ended putting Try/catch at the top of the subroutine, and include
all
>the codes in that subroutine under "Try". When I get an error, even
knowing
>ex.message sometimes it is difficult to know which line of code causes
the
problem, and I would like to know which line is it that produced the
error.
>With resume next, I will be able to tell during debuging.

Is there any way to do this ?

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:Ok**************@TK2MSFTNGP04.phx.gbl...
It's still there (unfortunately).

You can say: On Error Resume Next.

However, this does not work with try/catch. It's one or the other.

My recommendation is to get rid of On Error Resume Next, and always use
Try/Catch in the appropriate places. Usually if an unexpected run time
error ocurred, you don't want to execute any more statements - so I
never
really understood why you would want to Resume Next. If an error
happens
that you were not expecting, you need to handle it, and get out
appropriately.

"fniles" <fn****@pfmail.comwrote in message
news:OA**************@TK2MSFTNGP05.phx.gbl...
In VB 6.0 in the error trapping, we can do "resume next" to continue
on
the next code. How can we do that in .NET with "Try", "Catch","End
Try"
?
>Thanks




Jul 17 '06 #7

P: n/a
I use multiple try catch statements to catch errors at the logical statement
level vs. the language statement level.

Mike.

"fniles" <fn****@pfmail.comwrote in message
news:eh**************@TK2MSFTNGP03.phx.gbl...
Thank you for your reply. It is very helpful.
For each section of code you wish to protect, put the keyword "try"
before
it.
I really want to protect all codes in the subroutine, so replacing the "On
Error Goto" statement at the top of the sub with the Try is a good idea
(otherwise I will have to put "try/catch" everywhere in the subroutine)
The problem with this is, when I get an error, I really do not know which
section of the code the error comes from, while in VB6, with Resume Next
statement in my error label section, I can know which code it comes from.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:dV*****************@newsread2.news.pas.earthl ink.net...
You're struggling with one of the major differences between unstructured
error handling (VB6) and structured error handling (VB.NET - all
versions).
In VB6, each Sub or Function could only have a single error block, which
translated into additional, smaller, superflous, subs and functions to
nest
error handling. In VB.NET, you can have multiple, nested error
handlers.
Here's how to do it -

For each section of code you wish to protect, put the keyword "try"
before
it. At the end of the protected code, put the keyword "Catch" with an
exception type to handle. You can catch different types of exceptions
for
the same try block. Finally, if there is any clean up code that you
need
to
execute before leaving the "try" block, use the keyword "Finally" and
put
the code after it. The last line of the try block is "End Try"

try
' Protected Code
catch fEx as system.FileException
' File errors
catch ex as Exception
' All other errors
Finally
' clean up code - release file handles, etc.
end try

At any time, you can nest try...catch...[finally]...end try blocks, even
inside a catch or finally handler. An easy way to start converting is
to
find your On Error Goto statement and replace it with Try. At the error
label, replace this with Catch ex as Exception. At the end of the
routine,
insert an End Try. If you have a resume statement, replace it with
"Finally" (before the end try) and move the code from the resume target
into
the finally block.

For example -

sub MySub()
On error goto myErr
' Protected Statement

ResumeHere:
' Clean up code
exit sub

myErr:
' Error Handler
resume ResumeHere
end sub

becomes

sub MySub()
Try
' Protected Statement

Catch ex as exception
' Error Handler - note: you can use multiple catch statements, one
for
each type of error you expect to have; If none of your catch statements
can
handle the error, it is automatically propagated up the call stack to
the
next level of error handling.

Finally
' Clean up code
End Try
end sub

As for "On Error Resume Next", it's still useful, especially if you are
configuring an environment for your application and some parts of the
environment already exist and you would get an error back when you
attempt
to configure previously existing parts. Keep these sections of code as
short as possible (I never need more than 10 lines under this type of
control).

All uses of On Error Goto ... should be immediately replaced with try
blocks.

Mike Ober.

"fniles" <fn****@pfmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Thank you all for your replies.
In VB 6 at the top of a sub I used to put "on error goto err_routine",
and
in err_routine I will write the error to a file and do resume next,
because the "resume next" is helpful during debuging a program, because
I
can put a break point on the Resume Next and when I hit F8 it will go
to
the
next code so that I know where the error occurs.

In VB.NET where is the best place to put Try/catch, because if I put it
only
for 1 line of code, it won't catch error on the other lines of code in
that
same subroutine.
So, I ended putting Try/catch at the top of the subroutine, and include
all
the codes in that subroutine under "Try". When I get an error, even
knowing
ex.message sometimes it is difficult to know which line of code causes
the
problem, and I would like to know which line is it that produced the
error.
With resume next, I will be able to tell during debuging.

Is there any way to do this ?

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:Ok**************@TK2MSFTNGP04.phx.gbl...
It's still there (unfortunately).

You can say: On Error Resume Next.

However, this does not work with try/catch. It's one or the other.

My recommendation is to get rid of On Error Resume Next, and always
use
Try/Catch in the appropriate places. Usually if an unexpected run
time
error ocurred, you don't want to execute any more statements - so I
never
really understood why you would want to Resume Next. If an error
happens
that you were not expecting, you need to handle it, and get out
appropriately.

"fniles" <fn****@pfmail.comwrote in message
news:OA**************@TK2MSFTNGP05.phx.gbl...
In VB 6.0 in the error trapping, we can do "resume next" to continue
on
the next code. How can we do that in .NET with "Try", "Catch","End
Try"
?
Thanks








Jul 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.