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


有时在调用一个方法时,会进行异常处理。但是当方法内部出现错误时,无法快速定位到是哪一行代码有问题。
下面介绍一下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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM