Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Given the following code in C#:

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

I have converted that to VB as such using the dotnetfiddle VB.net converter:

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

This throws a compile error on:

Catch As Exception

End of Statement expected

I then change that to:

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

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


All good comments, and thanks for the redundancy information I realize the try/catch is completely not needed as this would have occurred with or without the try/catch. The question was more for curiosities sake in a scenario that, I suppose, has no real basis in (a good code) reality.

I had seen something similar in a blog post about exception handling recently and why to throw vs throw ex, and was just curious as to how to accomplish the same code in VB - as I'm not strong with VB and am trying to better understand it, and exception handling.

I had hoped I'd be able to find the blog post I referenced above, but was unable. The gist of it (which spawned the question) can be found: https://dotnetfiddle.net/741wAi

like image 531
Kritner Avatar asked Mar 06 '15 20:03

Kritner


People also ask

How do you catch an exception in a try catch block?

How to use the try/catch block to catch exceptions. Place the sections of code that might throw exceptions in a try block and place code that handles exceptions in a catch block. The catch block is a series of statements beginning with the keyword catch, followed by an exception type and an action to be taken.

Is the variable “VAR” declared but never used?

Thank you. In this article The variable 'var' is declared but never used The compiler issues a level-three warning when you declare a variable, but do not use it. The following sample generates a CS0168 warning:

Why is the try block placed in a try block?

Since the code might throw any of three exceptions, it's placed in a try block. Three catch blocks catch the exceptions and handle them by displaying the results to the console.

Why am I getting a warning for catch ex as exception?

The reason you are getting warning for Catch ex As Exception, is that you caught the exception in variable exbut you are not using it anywhere. Share Follow answered Mar 6 '15 at 20:52


2 Answers

Just have an empty Catch like:

Try
     StartThings()
Catch
    Throw
End Try

But if you are not doing anything in the Catch block other than re-throwing it, then there is no point to have try-catch in first place.

You can have StartThings() without try-catch and in case of exception, the exception will propagate to the caller.

The reason you are getting warning for Catch ex As Exception, is that you caught the exception in variable ex but you are not using it anywhere.

like image 125
Habib Avatar answered Sep 21 '22 21:09

Habib


If you're just catching Exception then your code is redundant, as has been pointed out. If however you're simplifying your example code and you are trying to catch only a specific type of exception and do other processing for other thrown Exceptions, then I hate to break it to you but it appears VB can't really do this so you'll have to put up with the warning. Obviously don't throw ex.

like image 45
moarboilerplate Avatar answered Sep 21 '22 21:09

moarboilerplate