Windows Develop Bookmark and Share   
 index > Windows Forms General > How to exit loop and function
 

How to exit loop and function

Hi,

I want to come out from loop and function when ever i get error....

i am using like this

public sub function name()


loop

/// here i am getting error i want to come out from function

end loop

end sub

Now i am using 'return' but it is comin out from from loop only ...
i want to come out from function


Thanks in Advance
Kalees
Kaleeswara Murthy  Saturday, August 30, 2008 2:08 PM
I don't see a Return statement in your Catch handler. However, you don't need one if you rearrange the code:

Try
For ...
End For
MsgBox("Successs")
Catch
MsgBox("...")
End Try
nobugz  Sunday, August 31, 2008 12:13 PM

Post your actual code ( or a reasonable facsimile). The error itself should exit to the calling method. "return" should definitely exit the method. Do you have a try-catch within the loop?

JohnWein  Saturday, August 30, 2008 2:42 PM
The Return statement will always exit a function unless you found a new way to loop. Exceptions are the best way to deal with errors, you can "exit" from several functions.
nobugz  Saturday, August 30, 2008 3:17 PM
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

For icount= 0 To iRowCount - 1
Try

// here database code and logic code

Catch ex As Exception
MsgBox(ex.Message())
//after this i want to come out from this method.
End Try
Next
MsgBox("Records successfully saved")

End If


End Sub


return exit only the loop..


thanks for the reply.....























































Kaleeswara Murthy  Sunday, August 31, 2008 12:43 AM
I don't see a Return statement in your Catch handler. However, you don't need one if you rearrange the code:

Try
For ...
End For
MsgBox("Successs")
Catch
MsgBox("...")
End Try
nobugz  Sunday, August 31, 2008 12:13 PM
Or if all you're doing is showing messages, catch the error in the calling method.

JohnWein  Sunday, August 31, 2008 1:29 PM

You can use google to search for other answers

Custom Search

More Threads

• MS-Access/MS-Excel
• Re-sizing datagrid when window is maximized? .NET1.1
• Serializing a Collection into a Settings file (default Settings.settings)
• problem with adding controls to a tablelayoutpanel
• How can I reach the Tag of a checkedListBox-Item??
• inner workings of OnResize, OnLayout and OnLayoutCompleted
• Splitting form horizontally in 4 panels
• What is the differnece?
• MouseWheel event not fired in Control derived class in splittercontainer panel
• Howto? sub-property in property grid : but only "visual" not in code...