I have recently started coding in VB.Net and started using the Try-Catch style of exception handling available in .Net, which I admit is very similar to the exception handling of C++, so it is not all that unfamiliar. In my VB6 code, which I have been using up till now to support a nearly-decade old legacy application, I would often use a fairly consistant error handling block.

Function Sample(intValue As Integer) As Integer
    On Error Goto Error_Sample

    ' Do whatever is needed for main operation.
Exit_Sample:
    Exit Function

Error_Sample:
    With Err
        Select Case .Number
        ' Specific cases could be put here to handle specific errors.
        Case 6  ' Division by 0 error
            intResult = -1
            Resume Next
        Case Else
            Select Case MsgBox("Error " & .Number & " in " & .Source & vbCr & .Description, _
                vbAbortRetryIgnore Or vbDefaultButton2 Or vbIconExclamation, "Form.Sample")
            Case vbAbort
                Resume Exit_Sample
            Case vbRetry
                Resume
            Case vbIgnore
                Resume Next
            End Select
        End Select
    End With
End Function

If you have an error that I did not define a handler for, then it falls through to a general handler that displays the error to the user and asks the user what to do - Abort (exit routine), Retry (go back to the statement that generated the error) or Ignore the error (move on to the next statement). Is similar functionalty available using the Try-Catch schema? Abort seems easy (Exit Function); Resume Next looks like it could be done by just falling through to the next statement after the Try block. Is the functionality of Resume available, or do I have to write it in a loop?

Re: How can I Resume in Try-Catch error handling. 80 80

you mean, you want to use the try and catch exception handling to trap error?
try
........line of codes
catch ex as exception
.......
end try

Re: How can I Resume in Try-Catch error handling. 80 80

or you may use do while loop..

Re: How can I Resume in Try-Catch error handling. 80 80

I know the basics of Try-Catch structure in C++, and I have been using it in a new VB.Net application. I am just hoping there is a nuance of the system in VB.Net that would allow this functionality, without having to resort to the older On Error Goto Handler method. My first thought is put the Try-Catch block in a loop and break the loop on success.

Do
    Try
        ' Put something that might fail unexpectedly in here, like a network failure.
        Exit Do
    Catch ex as Exception
        Select Msgbox("Error: " & ex.Message, MsgBoxStyle.Exclamation Or _
            MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.AbortRetryIgnore, _
            "Error Title")
        Case MsgboxResult.Abort
            Exit Function
        Case MsgboxResult.Retry
            Continue Do
        Case MsgboxResult.Ignore
            ' Let it flow out to the next statement.
        End Select
    End Try
Loop

Mind you, thinking about at as I wrote this chunk of code, I realized that because I can Catch specific types of Exceptions, I should be able to anticipate the specific exceptions that are being thrown and only need to handle them. A general error handler like I needed in VB6 is less needed in VB.Net, or at least that is how it looks to me right now. Still, is there a more efficient way of doing what am thinking of?

Re: How can I Resume in Try-Catch error handling. 80 80

How about a Try...Catch...Finally block

Also, you can create some custom exceptions that inherit the system.exceptions class. Then for a particular error, throw that exception.

Public Class SQLCodeNullException
    Inherits System.Exception

    ' A custom exception
End Class

' ...

Try

    ' My code that throws variaous errors
Catch sqlEX As SqlCodeNullFormatException

    ' I caught an excetion with a type of SqlCodeNullFormatException
    Msgbox ("The sql code provided will result in a null value.")
Catch ex as Exception

   ' This is an excetion I did not define.
    Msgbox ("Opps!")
Finally

   ' Some code here that is required before we exit the block.
End Try
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.