473,289 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Resume next in VB.NET ?

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
7 21844
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
4
by: MLH | last post by:
Consider the following 3 lines. If there are no records in tblStartUps and the DMax in line 65 returns Null, a runtime error condition exists in that Null is not a valid value for LastInRefNum. My...
3
by: Dave | last post by:
Hi, I have a C# program that is parsing an XML file and loading a database table. Some of the elements may be missing but I want to continue with loading the data anyway because they may not...
5
by: itsupport1 | last post by:
Hi, I am importing some records from one table to another table, and due to Constraints in the destination table, it Throws an Exception and Skip the Whole Rest of records. So I did implement...
3
by: bob.needler | last post by:
I know On Error Resume Next is generally considered lazy. But can someone tell me why the resume next in Exit_Handler does not seem to work? It generates the typical unhandled runtime error...
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
11
by: Maxwell2006 | last post by:
Hi, I know that this is not a good practice, but I wonder do we have "on error resume next" in C#? Thanks,
11
by: fniles | last post by:
In VB 6 I can do the following: Sub MySub on error goto Err1 : --all my codes are here : --say this is where the error occurs : --this is where resume next will bring me after Err1 exit sub...
18
by: julietbrown | last post by:
Can you boost my understanding of Access? I don't like things I can 'cure' but don't understand why! I've converted the Save macro on my form to VBA. It looks like this ... as I'm sure you...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.