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