try-catch会因错误而停止执行 [英] try-catch stops execution on error

查看:388
本文介绍了try-catch会因错误而停止执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在任何代码中实现try catch都会在捕获错误时停止执行.我们如何继续执行?

例如:

 尝试
             Dim  i  As  整数 =  0 
            对于 i =  0  >到 RichTextBox2. .Count- 1 
                ReturnSynonyms(RichTextBox2.Lines( 0 ))
            下一步
        捕获,例如 As 异常
        结束 尝试 




在上面的代码中,如果richtextbox2有一个像aaa这样的词,没有任何同义词,try-catch会捕获一个错误并停止,即使遇到异常,我如何继续搜索整个for循环? h2_lin>解决方案

Try...Catch放在您的ReturnSynonyms函数周围,方法是:

  Dim  i  As  整数 =  0 
对于 i =  0  >到 RichTextBox2. .Count- 1 
    尝试
        ReturnSynonyms(RichTextBox2.Lines( 0 ))
    捕获,例如 As 异常
    结束 尝试
下一个 


您可以尝试使用returnSynonyms.在异常发生时,执行将移至您的catch块.


i noticed that implementing a try catch in any code stops the execution when it catches an error. how do we continue the execution?

eg:

Try
            Dim i As Integer = 0
            For i = 0 To RichTextBox2.Lines.Count - 1
                ReturnSynonyms(RichTextBox2.Lines(0))
            Next
        Catch ex As Exception
        End Try




in above code if richtextbox2 has a word like aaa, for which there are no synonyms, the try-catch catches an error and stops, how do i continue searching the whole for loop even there is an exception caught??

解决方案

Put the Try...Catch just around your ReturnSynonyms function, this way:

Dim i As Integer = 0
For i = 0 To RichTextBox2.Lines.Count - 1
    Try
        ReturnSynonyms(RichTextBox2.Lines(0))
    Catch ex As Exception
    End Try
Next


move you try catch into returnSynonyms. At the point of the exception the execution will move to you catch block.


这篇关于try-catch会因错误而停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆