Avoiding the warning "variable is declared but never used" in the try / catch block

Given the following code in C #:

public void CatchExceptionThenThrow()
{
    try
    {
        StartThings();
    }
    catch (Exception)
    {
        throw;
    }
}

I converted this to VB as such using the vnet.net dotnetfiddle converter:

Public Sub CatchExceptionThenThrow()
    Try
        StartThings()
    Catch As Exception
        Throw
    End Try
End Sub

This results in a compilation error:

Catch As Exception

Expected end expected

Then I change this to:

Public Sub CatchExceptionThenThrow()
    Try
        StartThings()
    Catch ex As Exception
        Throw
    End Try
End Sub

But this creates the warning "variable declared but never used." How do I go throwing rather than throw exing in VB without receiving a warning while maintaining the integrity of the stack trace, as in the first C # example?


. , try/catch , try/catch. , , , ( ) .

- throw vs throw ex, , VB - VB .

, , , . ( ): https://dotnetfiddle.net/741wAi

+4
2

Catch :

Try
     StartThings()
Catch
    Throw
End Try

Catch, , try-catch .

StartThings() try-catch, .

, Catch ex As Exception, , ex, .

+6

Exception, , . , , , Exception s, , , VB . , ex.

+1

All Articles