Suji
Suji

Reputation: 1326

Unexpected control flow while using "On Error GoTo" , compare it with "Try.... Catch"

Both "Try.... Catch" and "On Error GoTo" are exception handling mechanisms in VB.NET. then what are the difference between them?

What are the reasons for the following?

  1. a block will allow multiple try..catch or multiple On Error GoTo but not both will allowed in the same block.

  2. for the following code:

    Private Sub check_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles check.Click
    
        On Error GoTo l1
        On Error GoTo l2
        rt.LoadFile("e:\new\me.txt")
        l1:TextBox1.Text = "Not found"    
        l2:TextBox1.Text = "Not found"
    End Sub
    

    why l2 is executed before l1 executes, if the path is not found?

Upvotes: 0

Views: 140

Answers (1)

Guffa
Guffa

Reputation: 700312

The On Error GoTo construct is only kept for legacy code. The only reason that it exists at all in later versions of VB is to make it easier to use old (but tested and working) code with minimal changes.

There is no reason ever to mix the old type of error handling with exception handling. Newly written code should not use the old error handling. Mixing them has been disallowed in the compiler on purpose.

When you use On Error GoTo each setting will replace the previous, so only the last setting will be active.

Upvotes: 5

Related Questions