-
Apr 14th, 2013, 01:52 AM
#1
[RESOLVED] VB6 Resume in .Net (error handling)
In VB6 error handling there is the "Resume" command which lets us try running again the line which raised an error, this was handy to me in errors pertaining to connection where the LAN may just have been disconnected that I allow the user re-connect the LAN and just try again, in the Try-Catch error handling scheme in .Net, how do we do that?
-
Apr 14th, 2013, 03:58 AM
#2
Re: VB6 Resume in .Net (error handling)
It's known as a loop, e.g.
Code:
Dim attemptsRemianing = 3
Do
Try
connection.Open()
Catch ex As Exception
'Log exception here.
attemptsRemaining -= 1
Thread.Sleep(1000)
End Try
Loop Until connection.ConnectionState = ConnectionState.Connected OrElse attemptsRemaining = 0
-
Apr 14th, 2013, 08:09 AM
#3
Re: VB6 Resume in .Net (error handling)
Not like that. See sample VB6 code below, as you can see the user could choose to Retry in certain errors and I would just easily call Resume, can this be done in the Try-Catch exception handling in .Net?
Code:
Private Sub PopulateMonths(ByVal YearSelected As String)
'<EhHeader>
PushStack "frmMainMenu.PopulateMonths"
On Error GoTo HandleError
'</EhHeader>
Dim adoR As ADODB.Recordset
100 cboMonth.Clear
102 SetFHC adoR, "SELECT Month(CheckupDate) AS Months FROM tabPatientCheckups WHERE Year(CheckupDate) = '" & YearSelected & "' GROUP BY Month(CheckupDate) ORDER BY Month(CheckupDate)"
104 With adoR
106 .Open
108 Do While Not .EOF
110 cboMonth.AddItem MonthName(.Fields(0))
112 cboMonth.ItemData(cboMonth.NewIndex) = .Fields(0)
114 .MoveNext
Loop
116 .Close
End With
118 Set adoR = Nothing
'<EhFooter>
ExitProcedure:
PopStack
Exit Sub
HandleError:
If WithADOERROR Then
PromptAndLogError "frmMainMenu.PopulateMonths", adoR.Source, 0
CleanUpADO adoR
PopStack
Else
Select Case PromptAndLogError("frmMainMenu.PopulateMonths")
Case vbAbort
CleanUpADO adoR
GoTo ExitProcedure
Case vbRetry
Resume
Case vbIgnore
Resume Next
Case Else
SignalCriticalError Err.Description
End
End Select
End If
'</EhFooter>
End Sub
-
Apr 14th, 2013, 08:34 AM
#4
Re: VB6 Resume in .Net (error handling)
I know that VB.NET does support On Error Resume Next but I don't think that there's a Resume keyword that can be used like that. That's all rather dodgy anyway. In VB.NET you should use structured exception handling and structure your code in such a way that, if you need to re-execute a line after an exception, you redirect execution back to that line. Just treat it like any other code, i.e. use a loop.
-
Apr 14th, 2013, 09:28 AM
#5
Re: VB6 Resume in .Net (error handling)
Can you show me how to structure my sample vb6 code above such that I can re-execute a line after an exception?
-
Apr 14th, 2013, 09:31 AM
#6
Re: VB6 Resume in .Net (error handling)
Originally Posted by dee-u
Can you show me how to structure my sample vb6 code above such that I can re-execute a line after an exception?
With a loop.
-
Apr 14th, 2013, 10:09 AM
#7
Re: VB6 Resume in .Net (error handling)
The loop that was showed earlier IS the way it works. It's the way it worked in VB6, as well, except that:
1) It was easier to write (one word versus a loop).
2) The loop occured on a single line and couldn't occur easily around more than one line.
3) You had the option to break it in interesting ways.
After all, when you added that Resume statement, it was generally added as you showed it: Down in some section of code not normally reachable. If the Resume occured, then you were effectively looping around that one statement, except that there was no explicit loop because execution actually wandered off to a different section of code for a line or two before looping back. So, at the benefit of writing somewhat less code, you ended up with a bowl of spaghetti with a fair number of gotos and code tags. Ultimately, the same result was achieved, though.
My usual boring signature: Nothing
-
Apr 14th, 2013, 12:40 PM
#8
Re: VB6 Resume in .Net (error handling)
As Jim said, all that VB6-style error handling is:
1. Still available in VB.NET
2. Rather dodgy
Note that you cannot combine the two approaches in one method - a method can use Try/Catch or it can use On Error/Resume/Resume Next/etc., but it cannot use both in the same method.
-
Apr 14th, 2013, 12:51 PM
#9
Re: VB6 Resume in .Net (error handling)
Notices tht nasty End statement also
-
Apr 14th, 2013, 05:45 PM
#10
Re: VB6 Resume in .Net (error handling)
Originally Posted by ident
Notices tht nasty End statement also
The VbMsgBoxStyle used is vbAbortRetryIgnore so that case with End is never called anyway, just left it that way if just in case the MsgBox returned a different reply for some peculiar reason.
Well, it was very easy to add such kind of error handlers in all procedures using MZTools and the like and it was rather effective in catching all unpredictable errors and at the same time I am able to let the user try fixing things before resuming. But I get the idea that there is no other way to emulate such without using a loop but is it not rather cumbersome to add a loop for every line that you think the user can retry executing after letting them fix the exception? Up til now this is just my complaint with .Net.
-
Apr 14th, 2013, 06:32 PM
#11
Re: VB6 Resume in .Net (error handling)
Your code should look something like this:-
vbnet Code:
' Private Sub PopulateMonths(ByVal YearSelected As String) Dim retry As Boolean = True PushStack("frmMainMenu.PopulateMonths") Do While retry retry = False Dim adoR As ADODB.Recordset Try cboMonth.Clear() SetFHC(adoR, "SELECT Month(CheckupDate) AS Months FROM tabPatientCheckups WHERE Year(CheckupDate) = '" & YearSelected & "' GROUP BY Month(CheckupDate) ORDER BY Month(CheckupDate)") With adoR .Open() Do While Not .EOF cboMonth.AddItem(MonthName(.Fields(0))) cboMonth.ItemData(cboMonth.NewIndex) = .Fields(0) .MoveNext() Loop .Close() End With adoR = Nothing Catch ex As Exception If WithADOERROR Then PromptAndLogError("frmMainMenu.PopulateMonths", adoR.Source, 0) CleanUpADO(adoR) PopStack() Else Select Case PromptAndLogError("frmMainMenu.PopulateMonths") Case vbAbort CleanUpADO(adoR) PopStack() Case vbRetry retry = True Case vbIgnore 'TODO Case Else SignalCriticalError(Err.Description) End End Select End If End Try Loop PopStack() End Sub
Note that ignore is not implemented. Some refactoring will be needed for that.
-
Apr 15th, 2013, 08:43 AM
#12
Re: VB6 Resume in .Net (error handling)
You would only loop around those lines that the user can do something about, which is the distinct minority of lines or methods in any app. If you have only one such location, then there isn't any real difference between old and new. Where there is a difference is in the fact that the old style allowed you to effectively write a single line and have it act as if there was a loop wrapping every single line in the method (and possibly in nesting methods). That IS easier, but it is also pretty sloppy. After all, there are only a few areas where the user has a viable chance of fixing things effectively, while also being areas where error handling should be used. By far, the largest of these would be accessing files or other types of system resources. In these cases, the problem generally comes down to a single loop around a single line. That line could be a method that attempts to access the file, or it could be just the line that accesses the file. In either case, it can generally be a single line, so there isn't much difference between old and new.
In your case, things are a bit different. For one thing, you are using legacy database tools (ADO rather than ADO.NET), which complicates the situation. If you were using ADO.NET, you would be using a datareader, and the only place you would probably really need the error trap would be around the ExecuteReader statement. In practice, you'd almost certainly include the next few lines as Niya showed (though that is still ADO, it would look pretty similar with a DataReader), but there would only be one line that would be throwing the error you would be wanting to trap, so it isn't really a different situation, it just looks a bit different.
I should also add that all the meat in that PopulateMonths could be turned into a single method that opened a DataReader, performed some actions, then closed the reader. That method would have no error handling at all. The PopulateMonths method would then:
1) Push (whatever that means in this context).
2) Start a loop that began a Try....Catch block, called the method, handled any exceptions thrown by the method.
3) Pop (whatever that means in this context).
That would REALLY bring it down to a single loop around a single line within an exception handler.
Last edited by Shaggy Hiker; Apr 15th, 2013 at 08:48 AM.
My usual boring signature: Nothing
-
Apr 15th, 2013, 08:55 AM
#13
Re: VB6 Resume in .Net (error handling)
Originally Posted by David Anton
As Jim said, all that VB6-style error handling is:
1. Still available in VB.NET
2. Rather dodgy
Note that you cannot combine the two approaches in one method - a method can use Try/Catch or it can use On Error/Resume/Resume Next/etc., but it cannot use both in the same method.
Jim?
-
Apr 15th, 2013, 09:12 AM
#14
Re: VB6 Resume in .Net (error handling)
Originally Posted by dbasnett
Jim?
For some reason, I assumed jmcilhinney's first name was Jim...
-
Apr 15th, 2013, 09:18 AM
#15
Re: VB6 Resume in .Net (error handling)
Originally Posted by David Anton
For some reason, I assumed jmcilhinney's first name was Jim...
It's actually John but, for some reason, lots of people have referred to me as Jim on this forum so you may well have picked it up along the way, consciously or subconsciously.
-
Apr 15th, 2013, 09:33 AM
#16
Re: VB6 Resume in .Net (error handling)
Sorry John - I'll scratch 'Jim' from my memory.
-
Apr 15th, 2013, 11:03 AM
#17
Re: VB6 Resume in .Net (error handling)
I think the source of the Jim is pretty simple: There are a whole lot of vertical lines in the displayed name, so adding an i between the, very vertical, j and m is about as easy as seeing my avatar as a pineapple.
My usual boring signature: Nothing
-
Apr 15th, 2013, 11:23 AM
#18
Re: VB6 Resume in .Net (error handling)
Your avatar isn't a pineapple??
-
Apr 15th, 2013, 03:35 PM
#19
Re: VB6 Resume in .Net (error handling)
Originally Posted by Shaggy Hiker
I think the source of the Jim is pretty simple: There are a whole lot of vertical lines in the displayed name, so adding an i between the, very vertical, j and m is about as easy as seeing my avatar as a pineapple.
I was thinking the same thing.
Originally Posted by David Anton
Your avatar isn't a pineapple??
I used to think it was some kind of stunted palm tree.
-
Apr 16th, 2013, 04:46 AM
#20
Re: VB6 Resume in .Net (error handling)
Ok, got it, so loop it is. =)
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
|