加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – Exception可能什么都没有?

发布时间:2020-12-17 07:14:01 所属栏目:百科 来源:网络整理
导读:在尝试维护别人的代码时,我发现了这个小宝石: Catch ex As Exception If Not ex Is Nothing Then ... End IfFinally 有没有时间发生这种情况我不知道?我应该将这些添加到我的代码中吗? 解决方法 如果你试试这个: Try Dim x As Exception = Nothing Throw
在尝试维护别人的代码时,我发现了这个小宝石:

Catch ex As Exception
   If Not ex Is Nothing Then
     ...
   End If
Finally

有没有时间发生这种情况我不知道?我应该将这些添加到我的代码中吗?

解决方法

如果你试试这个:

Try
    Dim x As Exception = Nothing
    Throw x
Catch ex As Exception
    Debug.Print(ex.ToString())
End Try

ex将是一个System.NullReferenceException. Throw statement文档没有提到如果传递空引用会发生什么,但OpCodes.Throw文档说:

NullReferenceException is thrown if the object reference is a null reference.

所以,我相信答案是前者永远不会是无.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读