VB.NET:关于“使用”的问题堵塞

发布于 2024-09-05 16:41:14 字数 373 浏览 7 评论 0原文

考虑代码:

On Error Goto ErrorHandler

Using sr As StreamReader = New StreamReader(OpenFile)
    str = sr.ReadToEnd
    sr.Close()
End Using

Exit Sub

ErrorHandler:

如果 Using 块内出现错误,如何清理 sr 对象?

sr 对象不在 ErrHandler 的范围内,因此无法调用 sr.Close()。即使出现错误,Using 是否会自动阻止清理任何资源?

Consider the code:

On Error Goto ErrorHandler

Using sr As StreamReader = New StreamReader(OpenFile)
    str = sr.ReadToEnd
    sr.Close()
End Using

Exit Sub

ErrorHandler:

If there is an error inside the Using block how do you clean up the sr object?

The sr object is not in scope in ErrHandler so sr.Close() cannot be called. Does the Using block cleanup any resources automatically even if there is an error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左秋 2024-09-12 16:41:14

正如 codeka 所说,您不需要在 sr 上调用 Close 。它会自动调用,包括是否出现错误。使用 using 语句可以提供与 try ... finally ... end try 相同的功能。

正如您在其他问题的答案中看到的那样,您不应该使用 On Error 等,只需执行以下操作:

Try
  Using sr as StreamReader ...
     ...
  End Using
Catch ex as SomeException
...
End Try

As codeka says, you don't need to call Close on sr. It'll called automatically, and that includes if there is an error. Using the using statement gives you the same functionality as try ... finally ... end try.

And as you see in the answers to your other question, you shouldn't be using On Error etc just do:

Try
  Using sr as StreamReader ...
     ...
  End Using
Catch ex as SomeException
...
End Try
八巷 2024-09-12 16:41:14

是的,using 块会自动调用 IDisposable.Dispose (对于 StreamReader 来说,这与调用 Close 相同),因此您无需执行任何操作要做的事情(这就是使用块的全部要点!)

Yes, the using block will automatically call IDisposable.Dispose (which, for a StreamReader is the same as calling Close) so there's nothing you need to do (that's the whole point of using blocks!)

幻梦 2024-09-12 16:41:14

这段代码:

Using sr As StreamReader = New StreamReader(OpenFile)
    str = sr.ReadToEnd
    sr.Close()
End Using

实际上相当于:

Dim sr As StreamReader = Nothing
Try
    sr = New StreamReader(OpenFile)
    sr.Close() ' notice: unnecessary '
Finally
    sr.Close()
End Try

请记住,Finally 块中的代码将在方法返回之前始终执行(如果它抛出自己的异常,那么,那么你就会陷入一个受伤的世界)。因此,Using 块中的 sr.Close 行是多余的(请注意,在使用 Try/的等效代码中没有必要) Final 因为 sr.Close 将在 Finally 中被调用,无论发生什么——是否抛出异常)。

This code:

Using sr As StreamReader = New StreamReader(OpenFile)
    str = sr.ReadToEnd
    sr.Close()
End Using

Is really equivalent to this:

Dim sr As StreamReader = Nothing
Try
    sr = New StreamReader(OpenFile)
    sr.Close() ' notice: unnecessary '
Finally
    sr.Close()
End Try

Keep in mind that code within a Finally block will always execute before the method returns (if it throws an exception of its own, well, then you're in for a world of hurt). So the sr.Close line you have within your Using block is superfluous (notice it is unnecessary in the equivalent code using Try/Finally since sr.Close will be called in the Finally no matter what -- exception thrown or not).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文