我的代码中有一个Try Catch。我想知道是否有一个只在没有发现错误时执行的语句? 最后执行有或没有错误,但我不希望...
我有这个
Try
My.Computer.FileSystem.DeleteDirectory(txtFolder.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
Catch ex As Exception
Status(ex.Message)
End Try
Status("Resetted", , 2000)
我希望最后一个状态语句只显示是否没有错误
答案 0 :(得分:3)
我觉得这样简单就足够了:
Try
My.Computer.FileSystem.DeleteDirectory(txtFolder.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
Status("Resetted", , 2000) 'will only have reached here if there were NO exceptions
Catch ex As Exception
Status(ex.Message)
End Try
答案 1 :(得分:1)
真的,你不需要特别声明
Try
' Code to watch for exceptions
Catch Ex as Exception
' Code to handle exception and eventually, if it is not possible
' to continue, exit from this method. There are two possibilities:
' Return <some meaningful value to the caller if this is a function>
' Throw -->> **without any argument** and let the caller handle the exception
End Try
' Code here will be executed only if the first try block doesn't throw exceptions
常见的错误是使用 Throw Ex 而不是投掷。差异很微妙但非常重要。如果使用 Throw Ex ,则会覆盖堆栈跟踪,因此很难找到失败代码的原始行。