有时在调用一个方法时,会进行异常处理。但是当方法内部出现错误时,无法快速定位到是哪一行代码有问题。
下面介绍一下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
原文地址:https://www.cnblogs.com/tengwei6328/p/11296754.html
时间: 2024-11-04 11:23:47