首页 > Web开发 > 详细

[VB.NET Tips]Try...Catch...End Try的另一种用法

时间:2019-08-04 01:19:44      阅读:131      评论:0      收藏:0      [点我收藏+]

有时在调用一个方法时,会进行异常处理。但是当方法内部出现错误时,无法快速定位到是哪一行代码有问题。
下面介绍一下Try的另一个用法:
Try...Catch ex As Exception When expression
当expression为True时处理异常,否则把异常抛到上一层调用。

    Dim isRelease As Boolean = True         '确定是否是Release版本

    Sub Main()

        Dim reuslt As Integer

#If DEBUG Then

        isRelease = False

#End If

        reuslt = Divide(10, 0)
        Console.WriteLine("结果是:" & reuslt)

        Console.Read()

    End Sub

    Private Function Divide(ByVal x As Integer, ByVal y As Integer) As Integer

        Dim reuslt As Integer

        Try

            Return x / y

        Catch ex As Exception When isRelease        '当isRelease为True时处理异常,否则把异常抛出

            Console.WriteLine("错误:" & ex.Message)

        End Try

    End Function

[VB.NET Tips]Try...Catch...End Try的另一种用法

原文:https://www.cnblogs.com/tengwei6328/p/11296754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!