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

前端之家收集整理的这篇文章主要介绍了如何使用Try and Catch在VB.Net中重试操作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从一个文件中读取,如果我失败了,让用户重试或以其他方式放弃.到目前为止代码看起来像这样:
  1. Read_Again:
  2. Try
  3. my_stream.Read(buffer,read_len)
  4. Catch ex As System.IO.IOException
  5. If MessageBox.Show("try again?") = DialogResult.Retry Then
  6. GoTo Read_Again
  7. Else
  8. Application.Exit() 'just abort,doesn't matter
  9. End If
  10. End Try

我不喜欢Goto,这很难看.但我不知道如何制作一个跨越try和catch的循环.

有没有更好的方法来写这个?

  1. Dim retry as Boolean = True
  2. While retry
  3. Try
  4. my_stream.Read(buffer,read_len)
  5. retry = False
  6. Catch ex As System.IO.IOException
  7. If MessageBox.Show("try again?") = DialogResult.Retry Then
  8. retry = True
  9. Else
  10. retry = False
  11. Application.Exit() 'just abort,doesn't matter
  12. End If
  13. End Try
  14. End While

猜你在找的VB相关文章