.net - 如何使用 Try 和 Catch 在 VB.Net 中重试操作?

标签 .net vb.net exception try-catch

我想读取文件,如果失败,让用户重试或放弃。到目前为止,代码如下所示:

Read_Again:
    Try
        my_stream.Read(buffer, 0, read_len)
    Catch ex As System.IO.IOException
        If MessageBox.Show("try again?") = DialogResult.Retry Then
            GoTo Read_Again
        Else
            Application.Exit() 'just abort, doesn't matter
        End If
    End Try

我不喜欢Goto,它很丑。但我不知道如何创建一个跨越 try 和 catch 的循环。

有更好的写法吗?

最佳答案

Dim retry as Boolean = True
While retry
   Try
      my_stream.Read(buffer, 0, read_len)
      retry = False
   Catch ex As System.IO.IOException
       If MessageBox.Show("try again?") = DialogResult.Retry Then
           retry = True
       Else
           retry = False
           Application.Exit() 'just abort, doesn't matter
       End If
   End Try
End While

关于.net - 如何使用 Try 和 Catch 在 VB.Net 中重试操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6446495/

相关文章:

c# - 从 WPF 中的窗口中删除控件

c# - 如何在给定负载下运行 CPU(CPU 利用率百分比)?

.net - 为什么我不能将匿名类型作为参数传递给函数?

c# - .NET 中的 "Debug.Print"和 "Console.WriteLine"有什么区别?

vb.net - 双击 DataGridView 行?

C# IL - 调用构造函数

sql-server - 如何使用 LINQ 将记录添加到数据库?

java - 捕获cxf在camel上下文中抛出的异常

当 list 添加为资源时,Delphi 5 导致 EAccessViolation

c++ - 也许我误解了 C++ 异常?