vb.net 在 try/catch 块中避免警告“变量已声明但从未使用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28907389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoiding warning "variable is declared but never used" in try/catch block
提问by Kritner
Given the following code in C#:
给定以下 C# 代码:
public void CatchExceptionThenThrow()
{
try
{
StartThings();
}
catch (Exception)
{
throw;
}
}
I have converted that to VB as such using the dotnetfiddle VB.net converter:
我已经使用 dotnetfiddle VB.net 转换器将其转换为 VB:
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?
但这会产生一个警告“声明了变量但从未使用过”。我如何在没有收到警告的情况下在 VB 中执行throwing 而不是throw exing,同时像第一个 C# 示例中那样保留整个堆栈跟踪?
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.
所有好的评论,感谢您提供的冗余信息,我意识到完全不需要 try/catch,因为无论是否使用 try/catch,都会发生这种情况。在我认为在(好的代码)现实中没有真正基础的情况下,这个问题更多是出于好奇。
I had seen something similar in a blog post about exception handling recently and why to throwvs 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.
我最近在一篇关于异常处理的博客文章中看到了类似的内容以及为什么要throwvs throw ex,并且很好奇如何在 VB 中完成相同的代码 - 因为我对 VB 并不熟悉,并且正在努力更好地理解它,并且异常处理。
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
我曾希望我能够找到我上面引用的博客文章,但没有找到。可以找到它的要点(产生问题):https: //dotnetfiddle.net/741wAi
回答by Habib
Just have an empty Catchlike:
只是有一个空的Catch:
Try
StartThings()
Catch
Throw
End Try
But if you are not doing anything in the Catchblock other than re-throwing it, then there is no point to have try-catchin first place.
但是,如果Catch除了重新抛出它之外,您没有在该块中做任何事情,那么首先就没有任何意义try-catch。
You can have StartThings()without try-catch and in case of exception, the exception will propagate to the caller.
您可以StartThings()不使用 try-catch 并且在出现异常的情况下,异常将传播给调用者。
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.
您收到 , 警告的原因Catch ex As Exception是您在变量中捕获了异常,ex但没有在任何地方使用它。
回答by moarboilerplate
If you're just catching Exceptionthen 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.
如果你只是在捕捉,Exception那么你的代码是多余的,正如已经指出的那样。但是,如果您正在简化示例代码,并且您试图仅捕获特定类型的异常并对其他抛出的Exceptions进行其他处理,那么我不想打扰您,但看起来 VB 无法真正做到这一点,所以您将不得不忍受警告。显然不要扔前任。

