Eya*_*yal 7 .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
Run Code Online (Sandbox Code Playgroud)
我不喜欢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
Run Code Online (Sandbox Code Playgroud)