首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >嵌套的Try- catch :为外部try catch循环抛出异常

嵌套的Try- catch :为外部try catch循环抛出异常
EN

Stack Overflow用户
提问于 2019-10-18 17:48:58
回答 1查看 460关注 0票数 0

我定义了2个自定义异常,如下所示

代码语言:javascript
运行
AI代码解释
复制
Public Class SkipException
   Inherits System.ApplicationException
End Class

Public Class NoRecordFoundException
   Inherits System.ApplicationException
End Class

在我的代码中,场景是1.数据导致一般异常2.我没有数据3.我已经处理过的异常

代码语言:javascript
运行
AI代码解释
复制
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

那么这会奏效吗?异常会转到外部处理而不是内部捕获吗?

现在,我正在重新抛出异常以使其正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-18 18:58:02

只需处理特定的异常并重新抛出。下面的Catch ex as Exception将忽略在它之前捕获的异常。

代码语言:javascript
运行
AI代码解释
复制
Try
    Try
        Throw New NoRecordFoundException()
    Catch ex As NoRecordFoundException
        Throw
    Catch ex As Exception
        ' nothing happens here
    End Try
Catch ex As NoRecordFoundException
    ' handled here
Catch ex As Exception
    ' nothing happens here
End Try
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58455846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档