473,511 Members | 12,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Try catch and Resume

Neo
Hello All,

Although, I have read all the advantages of using Try Catch Block
instead of "On error goto", I am still confused what is alternative for
classic "Resume" statement. "Resume" was one of the crucial line in
debugging which let VB programmers go to exact line where error was
thrown.

I still use on error goto instead of try catch since, I want "Resume"
for debugging. Is there any alternative like Resume in VB.net for Try
catch block?

Best Regards,
Pravin

Apr 5 '06 #1
15 11736
I really don't understand what you are trying to say. You can debug to the
line of error in .NET with Try Catch.. Whenever there is a error, in a Try
block, its thrown into the catch statement in the same try or in any upper
level catch. If you do something like below...

Try

' statements
Catch ( Exception ex)

' ex.Tostring() <- will give you the line number where the error was..
This is assuming you complied in
debug mode
End try

Hope I have explained what you are looking for..

VJ

"Neo" <pr*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hello All,

Although, I have read all the advantages of using Try Catch Block
instead of "On error goto", I am still confused what is alternative for
classic "Resume" statement. "Resume" was one of the crucial line in
debugging which let VB programmers go to exact line where error was
thrown.

I still use on error goto instead of try catch since, I want "Resume"
for debugging. Is there any alternative like Resume in VB.net for Try
catch block?

Best Regards,
Pravin

Apr 5 '06 #2
Resume allowed the code to continue while in an error state. This allowed
the programmer to attempt to correct the problem and continue on. I would
propose nested try catch finally statements to get you where you would like
to go.

given:

On Error Resume Next

dim iRet as Long

iRet = objTemp.Blowup

if Err.number <> 0 or iRet <> 0 then
err.clear
correct the state and move on
end if

becomes:

dim iRet as integer

try
try
iRet = objTemp.Blowup
catch SpecificException1
'Known expected exception #1
catch SpecificException2
'Known expected exception #2
catch ex as exception
'Unexpected exception and potentially unrecoverable
throw new applicationexception("An uncorrectable error condition
occured.")
Finally
'Use this block to recover what you can
end try
'continue on with your logic
catch ex as exception
'Log exception
'Notify UI
'Clean up
end try

"Neo" <pr*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hello All,

Although, I have read all the advantages of using Try Catch Block
instead of "On error goto", I am still confused what is alternative for
classic "Resume" statement. "Resume" was one of the crucial line in
debugging which let VB programmers go to exact line where error was
thrown.

I still use on error goto instead of try catch since, I want "Resume"
for debugging. Is there any alternative like Resume in VB.net for Try
catch block?

Best Regards,
Pravin

Apr 5 '06 #3
"Neo" <pr*********@gmail.com> schrieb:
Although, I have read all the advantages of using Try Catch Block
instead of "On error goto", I am still confused what is alternative for
classic "Resume" statement. "Resume" was one of the crucial line in
debugging which let VB programmers go to exact line where error was
thrown.


Unfortunately there is no direct equivalent for 'Resume' available. You'll
have to put every single line into a separate 'Try...Catch' block to
archieve similar behavior.

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

Apr 5 '06 #4
Neo
It's not just question of line number! It's the complete stack and
values of all parameters. By setting your next line to resume after
error you could jump to line which gave error without leaving
execuation. In VB 6, we used fix problem also.

If you are VB programmer and never used resume you have never seen the
beauty of VB debugging.

see following classic VB code
sub test()
On error got hell

'yada yada yada
'yada yada yada

exit_hander:
exit sub
hell:
msgobx error
resume exit_handler
resume
end sub

Now after you get an error, instead of clicking ok, hit Ctrl+Break (in
VB 6.0 though). Then you will go in debug mode. Now, set your next
statement as "resume". It will take your execuation on line which gave
error with whole state preserved. Basically, you reach a line just
before error was thrown.

After knowing the line number you must set breakpoint

Apr 5 '06 #5
Neo
Thanks for reply. But I am not looking for On error resume next (as
reply explain), I am talking about just "Resume" statement. Just stand
alone "Resume" statemet, to be precise.

I see, Try Catch doesn't have that cool thing what "resume" has.
Anyways, I still use On error in VB.net not because, I don't know how
to you use try catch, but because, it doesn't let me do what I used in
classic VB. i.e. Jumping to line where an error occured by setting next
statement in debug mode to "Resume", while preserving param values and
stack.

Apr 5 '06 #6
Neo wrote:
see following classic VB code
sub test()
On error got hell

'yada yada yada
'yada yada yada

exit_hander:
exit sub
hell:
msgobx error
resume exit_handler
resume
end sub


If you want to be able to enter debug mode at the point at which the error
occurred, you can open the Exceptions window (Debug / Exceptions) and check
the "Throw" box for the Common Language Runtime Exceptions row. This will
break as soon as the exception occurs, meaning you don't need to rely on the
hidden "Resume" statement to get back to where you were.

As for the structure of your code, I think you need to re-adjust the way you
think about error handling. If that's a typical scenario in which you want
to use Resume along with a label, either of these would produce the same
results:

\\\
Sub Test
Try
'Yada yada yada
'Yada yada yada

Catch ex As Exception
MsgBox(ex.Message)

Finally
'Exit handler code goes here
End Try
End Sub
///

....or...

\\\
Sub Test
Try
'Yada yada yada
'Yada yada yada

Catch ex As Exception
MsgBox(ex.Message)

End Try

'Exit handler code goes here
End Sub
///

Not sure that's really what you're looking for but perhaps you'll find some
of it useful.

--

(O)enone

Apr 5 '06 #7
Not yet in VS 2005 but...

You could also add try/catch as you go along (if you don't have a handler
the exception is raised and you can correct before retrying the line). Once
tested you can add the try/catch block for more exceptional code (or you
could use conditional compilation to have a global handler in release mode
but not in debug mode). Finally the handler catch only the exception you
specified (i.e. a try catch block could catch known possible error but let
you debug other errors).
--
Patrice

"Neo" <pr*********@gmail.com> a écrit dans le message de news:
11*********************@i40g2000cwc.googlegroups.c om...
Thanks for reply. But I am not looking for On error resume next (as
reply explain), I am talking about just "Resume" statement. Just stand
alone "Resume" statemet, to be precise.

I see, Try Catch doesn't have that cool thing what "resume" has.
Anyways, I still use On error in VB.net not because, I don't know how
to you use try catch, but because, it doesn't let me do what I used in
classic VB. i.e. Jumping to line where an error occured by setting next
statement in debug mode to "Resume", while preserving param values and
stack.

Apr 5 '06 #8
Ahh.. now I get it.. been a while I did Classic VB, I almost forgot this....
anyways like others said.. only way is try/catch around each statement...
Now also if you use the current Edit/Continue in 2005 it provides a similar
functionality.. but like a 1-1 match with Resume..

VJ
"Neo" <pr*********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
It's not just question of line number! It's the complete stack and
values of all parameters. By setting your next line to resume after
error you could jump to line which gave error without leaving
execuation. In VB 6, we used fix problem also.

If you are VB programmer and never used resume you have never seen the
beauty of VB debugging.

see following classic VB code
sub test()
On error got hell

'yada yada yada
'yada yada yada

exit_hander:
exit sub
hell:
msgobx error
resume exit_handler
resume
end sub

Now after you get an error, instead of clicking ok, hit Ctrl+Break (in
VB 6.0 though). Then you will go in debug mode. Now, set your next
statement as "resume". It will take your execuation on line which gave
error with whole state preserved. Basically, you reach a line just
before error was thrown.

After knowing the line number you must set breakpoint

Apr 5 '06 #9
Neo,

Is there somebody who says that you should not what you think is the best?

That is the pleasure from VBNet it let you do it as it fits you the best.

(Not that I use it before you think that, the Try Catch Finally fits me
exact)

Just my thought,

Cor
Apr 5 '06 #10
Neo
I fully agree with you. I am very happy with VB.net.

Especially when I have seen, code getting ugly if you do Office
automation in C# with by passing missing object for optional
arguments. I don't understand why would anyone write office automation
code in C# when you have VB. Even object browser doesn't work in C# for
Office interops.

With VB.net 2005, I could port my VBA code, Win APIs to VB.net code. I
am not interested in writing platform independent code, I just want to
exploit all features in .Net along with windows native APIs, glueing
with COM. All my clients are on Windows, all my servers are on Windows.
and with VB.net 2005. I can do things just the way I like them. Three
cheers to VB.net 2005!!

Apr 5 '06 #11
Neo
Thanks.. you have precisely answered my question. Thanks a lot. I
really appreciate this. I hope All these bulky book about VB 2005 could
mentioned these alternatives in bold letters.

This is not just alternative for "resume", it is better since, I don't
have to type hidden line of resume.

Thanks!

Apr 5 '06 #12
> Hello All,

Although, I have read all the advantages of using Try Catch Block
instead of "On error goto", I am still confused what is alternative
for classic "Resume" statement. "Resume" was one of the crucial line
in debugging which let VB programmers go to exact line where error was
thrown.

I still use on error goto instead of try catch since, I want "Resume"
for debugging. Is there any alternative like Resume in VB.net for Try
catch block?

Best Regards,
Pravin


The following works in VB.Net

Sub Main()

On Error GoTo errHandle
Console.WriteLine("line 1")
Throw New Exception
Console.WriteLine("Line 3")
Console.ReadLine()

errHandle:
Console.WriteLine("In Handler")
Resume Next
End Sub

That being said, I agree with those who argue that if an exception is thrown,
your application is in an invalid state and should not be allowed to just
continue without explicit handling of some sort, even if it is simple logging.
I do not use the above in .Net applications nor do I recommend it.

Jim Wooley
http://devauthority.com/blogs/jwooley
Apr 5 '06 #13
you know; i'm really excited that you just said this; i was trying to
do it earlier and it was choking; but i'll go back and give it another
try
i'm just stoked you said that comment; thanks

trying to rewrite this app in vb6 that is like months and months
overdue; i mean-- rewriting it in .NET basically a buinch of ETL type
stuff

is it going to be easy to consume this VB2005 DLL once i get into SSIS?

Apr 5 '06 #14
Neo,
In addition to the other comments.

The "best" you could do is to put a Try/Catch around each of the commands
you want retry and have the Catch block retry the Try Block. I find using
Goto in the Catch block the "easiest" way to "Retry", others have put the
entire Try/Catch in a loop...

BTW: I've heard all the arguments about how Goto is evil & should be
avoided, in this case the "Goto Retry" is more like a "Retry" statement. Yes
"goto retry" could be used for evil, however it can also be used for good...

Something like:
Try Retry:
'...something

Catch ex as FileNotFoundException

If MessageBox.Show("File does not exit!", _
Application.ProductName, _
MessageBoxButtons.RetryCancel, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) _
= DialogResult.Retry Then
GoTo Retry
End If
End Try


Caution: With either the Goto Retry or a loop, be certain to allow your
users an Out, so you don't get into an endless loop.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Neo" <pr*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
| Hello All,
|
| Although, I have read all the advantages of using Try Catch Block
| instead of "On error goto", I am still confused what is alternative for
| classic "Resume" statement. "Resume" was one of the crucial line in
| debugging which let VB programmers go to exact line where error was
| thrown.
|
| I still use on error goto instead of try catch since, I want "Resume"
| for debugging. Is there any alternative like Resume in VB.net for Try
| catch block?
|
| Best Regards,
| Pravin
|
Apr 10 '06 #15
Neo

Excellent idea. Thanks.

Apr 13 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6767
by: Darta | last post by:
OK- I volunteer to be shot if this question is stupid... BUT, is there a way to, when you catch an exception in the catch{} handler, resume to the same line of code that generated an error while...
5
14055
by: Rob R. Ainscough | last post by:
Coming from VB6 to VB.NET, it appears if I opt to use the Try Catch approach I don't have any way to RESUME from within my catch statement? What I often do is resolve the problem in my catch...
5
8596
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...
7
21902
by: fniles | last post by:
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
4
3751
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...
7
6478
by: Rob R. Ainscough | last post by:
In VB6 I can use Resume Next to execute the line of coding following the line that cause an exception. There doesn't appear to be anything similiar when using Try...Catch -- so how can one resume...
11
47947
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,
19
6560
by: kimiraikkonen | last post by:
Hi, I want to find out if there's difference between "On Error Resume Next" error handler and leaving "catch" block empty in a try-catch-end try block to ignore exceptions which i don't approve of...
3
2920
by: sriharikabattula | last post by:
hi, i am uploading .doc,.docX,rtf files during registration level and that path can be saved in database(i am using database MYSQL).when user want to see his resume he/she can login and retrive...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.