Results 1 to 20 of 20

Thread: [RESOLVED] VB6 Resume in .Net (error handling)

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Resolved [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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  5. #5

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    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?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by dee-u View Post
    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  8. #8
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: VB6 Resume in .Net (error handling)

    Notices tht nasty End statement also

  10. #10

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by ident View Post
    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: VB6 Resume in .Net (error handling)

    Your code should look something like this:-
    vbnet Code:
    1. '
    2. Private Sub PopulateMonths(ByVal YearSelected As String)
    3.         Dim retry As Boolean = True
    4.  
    5.         PushStack("frmMainMenu.PopulateMonths")
    6.  
    7.         Do While retry
    8.             retry = False
    9.             Dim adoR As ADODB.Recordset
    10.             Try
    11.  
    12.                 cboMonth.Clear()
    13.                 SetFHC(adoR, "SELECT Month(CheckupDate) AS Months FROM tabPatientCheckups WHERE Year(CheckupDate) = '" & YearSelected & "' GROUP BY Month(CheckupDate) ORDER BY Month(CheckupDate)")
    14.                 With adoR
    15.                     .Open()
    16.                     Do While Not .EOF
    17.                         cboMonth.AddItem(MonthName(.Fields(0)))
    18.                         cboMonth.ItemData(cboMonth.NewIndex) = .Fields(0)
    19.                         .MoveNext()
    20.                     Loop
    21.                     .Close()
    22.                 End With
    23.                 adoR = Nothing
    24.  
    25.             Catch ex As Exception
    26.  
    27.                 If WithADOERROR Then
    28.                     PromptAndLogError("frmMainMenu.PopulateMonths", adoR.Source, 0)
    29.                     CleanUpADO(adoR)
    30.                     PopStack()
    31.                 Else
    32.                     Select Case PromptAndLogError("frmMainMenu.PopulateMonths")
    33.                         Case vbAbort
    34.                             CleanUpADO(adoR)
    35.                             PopStack()
    36.                         Case vbRetry
    37.                             retry = True
    38.                         Case vbIgnore
    39.                             'TODO
    40.                         Case Else
    41.                             SignalCriticalError(Err.Description)
    42.                             End
    43.                     End Select
    44.                 End If
    45.  
    46.             End Try
    47.         Loop
    48.  
    49.         PopStack()
    50.     End Sub

    Note that ignore is not implemented. Some refactoring will be needed for that.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by David Anton View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by dbasnett View Post
    Jim?
    For some reason, I assumed jmcilhinney's first name was Jim...
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by David Anton View Post
    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.

  16. #16
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: VB6 Resume in .Net (error handling)

    Sorry John - I'll scratch 'Jim' from my memory.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  18. #18
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: VB6 Resume in .Net (error handling)

    Your avatar isn't a pineapple??
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  19. #19
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: VB6 Resume in .Net (error handling)

    Quote Originally Posted by Shaggy Hiker View Post
    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.

    Quote Originally Posted by David Anton View Post
    Your avatar isn't a pineapple??
    I used to think it was some kind of stunted palm tree.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  20. #20

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: VB6 Resume in .Net (error handling)

    Ok, got it, so loop it is. =)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width