【发布时间】:2019-10-18 17:48:58
【问题描述】:
我已经定义了 2 个自定义异常,例如
Public Class SkipException
Inherits System.ApplicationException
End Class
Public Class NoRecordFoundException
Inherits System.ApplicationException
End Class
在我的代码中,场景是 1.数据导致一般异常 2. 我没有数据 3. 我已经处理过的异常
Try
'Some code here
Try
''Do some code
''Cant find the record
If i = 0 then
Throw NoRecordFoundException
End if
Catch ex as Exception
End Try
Try
''Cant do nothing so just skip
If CantDoNothing then
Throw SkipException
End if
Catch ex as Exception
End Try
Catch SkipException
''Some code here
Catch NoRecordFoundException
'' some code here
Catch ex as Exception
''Handle regular exception
End Try
那么这会奏效吗?异常会转到外部处理而不是内部捕获吗?
现在,我正在重新抛出异常以使其正常工作。
【问题讨论】:
-
不是现在,你捕获所有异常并且对它们不做任何事情。我想知道你是否真的需要例外。也许您只需要一个状态变量或方法的返回值。
-
Throw New NoRecordFoundException()。内部Catch会捕获它(如果它正在用它做某事)。你可以有类似Throw new Exception("Inner", New SkipException())和Catch ex As Exception、if ex.InnerException is SkipException then Throw ex.InnerException的东西。这将被外部Catch SkipException捕获。但是,内部Try块可能只有一个Finally块而不是Catch块,因此外部Catch块将捕获所有异常。 -
虽然下面的答案似乎是正确的,但我也同意例外可能不是解决此问题的正确工具。更好地设计
If块或一些变量来跟踪当前状态可能更合适。 -
感谢所有的解释。 “重新投掷”是一个简单的答案!