【问题标题】:If there is no Internet connection, program crashes如果没有 Internet 连接,程序会崩溃
【发布时间】:2014-11-29 19:36:31
【问题描述】:

我的程序运行良好,但是,如果没有互联网连接,它就会崩溃。(错误:blabla 停止工作)

我的程序 exe

Module module2


    Dim webClient As New System.Net.WebClient
    Dim result As String = webClient.DownloadString("http://www.example.org")
    Dim oza As Object


    Sub main()

        oza = CreateObject("InternetExplorer.Application")
        oza.Navigate2("http://www.example.org", "", "", "")

End Sub

End Module

如何防止程序崩溃?

【问题讨论】:

  • 抛出了异常,不是吗? - 见Handling and Throwing Exceptions
  • 无法理解您的意思。
  • 好吧,例如,如果DownloadString 抛出 WebException (因为它无法下载文件,因为互联网不可用)然后程序,它不会优雅地处理异常,将“异常”终止。请参阅第一条评论中的链接。

标签: .net vb.net webclient


【解决方案1】:

WebClient.DownloadString 在无法下载资源时抛出 WebException - 例如无法访问 Internet。由于未妥善处理此异常,程序“崩溃”。

如何处理这种情况,请参阅Handling and Throwing Exceptions,并注意:

从出现问题的代码区域引发异常。异常向上传递到堆栈,直到应用程序处理它或程序终止。

attaching the debugger or "running with F5" 可以轻松诊断此类问题,从而可以监控抛出的异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多