The variable '<variablename>' hides the variable in the closing block

When copying and pasting a piece of code from MSDN, I came up with an error in the header - Variable '' hides the variable in the closing block ,

Everything I copied was a very simple example of a try loop.

As stated in the sentence, β€œThe common cause of this error is the use of Catch e As Exception inside the event handler. If so, name the block variable Catch ex, not e.” A.

So, I did this, changed both eto ex, and it worked, however, I don’t understand why this is not causing the same error.

Can someone please better explain what the error is, and why e causes it, but ex does not?

edit -

sample code ...

    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try

.

    Try
    Catch ex As Exception
        msgbox(ex.Message)
    End Try

, , - , , - , ... , - , , . , , .

+3
3

, , , "e" . , catch, "e" , . , catch "ex" "e" , , .

: , , , .

, :

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
    Catch e As Exception
        msgbox(e.Message)
    End Try
End Sub

e, ByVal e As System.EventArgs, - Catch e As Exception.

+13

, :

int abc = 0;
if (abc == 0)  {
  int abc = 1;  // Error
}

, , , .

+8

. , .

0

Source: https://habr.com/ru/post/1757690/


All Articles