Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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:

VB
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??
Posted
Comments
Sandeep Mewara 29-Jul-10 11:33am    
If you don't want Try-Catch, why put it in the first place?
William Winner 29-Jul-10 12:37pm    
you got your answer, but this is a perfect example of why proper use of the Try/Catch is so important. Many, many people just add a Try/Catch to a huge block of code, when it is meant, AFAIK, to target specific statements that could throw an error.

People are using it like VB6's On Error Goto which is still available if that's what you're looking for. IMO, there should only be one line of code in each Try/Catch block aside from a few exceptions.

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

VB
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
 
Share this answer
 
Comments
amit_upadhyay 29-Jul-10 11:32am    
hahahaha ok got it. how stupid of me.
Nish Nishant 29-Jul-10 14:38pm    
Reason for my vote of 5
Worth a 5.
move you try catch into returnSynonyms. At the point of the exception the execution will move to you catch block.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900