【发布时间】:2014-05-21 04:05:17
【问题描述】:
我正在尝试使用此代码下载文件:
Try
My.Computer.Network.DownloadFile _
(fileUrl, Path.Combine(mySettings.filePath, fileName), _
"", "", False, 500, True)
Catch ex As Exception
MsgBox(translation.GetString("msgNavError") & vbCrLf & ex.Message)
End Try
此代码有效并显示类似Le nom distant n'a pas pu être résolu: '<hostname>' 的错误。像 If ex.Message = "Le nom distant n'a pas pu être résolu: '<hostname>'" Then ... 这样比较是没有意义的,因为消息是本地化的。
那么我如何在 VB.net 中编写这个伪代码呢?
Catch ex As Exception
If ex.NoNetworkWorkConnection Then
MsgBox("Your computer is not connected to the network")
Else If ex.ServerDidntRespond Then
MsgBox("The server did not respond")
Else
MsgBox("Unexpected error : " & ex.Message)
End If
End Try
我正在使用 Visual Basic 2008 Express。
【问题讨论】:
-
一定要避免“catch-em-all”代码,必须生成一个体面的诊断结果将成为您的下一个问题。改为捕获 WebException。它的 Status 属性为您提供了与语言无关的错误提示。
标签: vb.net exception visual-studio-2008 exception-handling try-catch