我想读取文件,如果失败,让用户重试或放弃。到目前为止,代码如下所示:
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/