Full Version: C# equivalent to 'Exit Sub'
UtterAccess Discussion Forums > And More... > JAVA and C#
kbrewster
I have just finished a C# project and I am now going through and adding error trapping. But I have come across this one error...the code is calling a stored procedure that does not exist and the error keeps popping up after hitting OK and you have to go to task manager to exit out of the program. I am using the try, catch and finally but I want it to exit out of the code...
CODE
private void FillClientCodes()
        {
            //Gets a list of client codes from EMR Database
            lvClientCodes.Visible = true;
            lvClientCodes.Items.Clear();
            SqlConnection con = new SqlConnection(constring);
            SqlDataReader rdr = null;
            try
            {
                con.Open();

                SqlCommand cmd = new SqlCommand("EHR_GetClientCodes", con);
                cmd.CommandType = CommandType.StoredProcedure;

                rdr = (SqlDataReader)cmd.ExecuteReader();

                ListViewItem itmX;

                while (rdr.Read())
                {
                    itmX = new ListViewItem();
                    itmX.Text = rdr.GetValue(0).ToString();

                    for (int i = 1; i < rdr.FieldCount; i++)
                    {
                        itmX.SubItems.Add(rdr.GetValue(i).ToString());
                    }
                    lvClientCodes.Items.Add(itmX);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        finally
        {
            if (con != null)
            {
                con.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
        }
rabroersma
What exactly is the error that you are getting?

Also for a function returning void the equivalent to exit sub would be:

return;
ace
Use your favorite search engine to serach for C# Return


Return is how you exit and return a value from a C# function.
kbrewster
The exact error that I am getting is...

And it is occuring on the line...
CODE
rdr = (SqlDataReader)cmd.ExecuteReader();


And using return; is not making the error go away...it just keeps cycling on the screen rather than just showing once.
rabroersma
Rather than the error message, what is the error class that is getting thrown?

The catch{} will trap the error once it is encountered. The only way I can see that you'd see this error message cycling would be if you are cycling the call to FillClientCodes().

It could be that trapping the error in FillClientCodes() is the wrong design. If it make sense to your logic, it may make your life easier if you throw this error back up to the calling function so that it can deal with it.


Also, the design of your application requires you to call stored procedures that may not exist, it may help to test for their existence first before calling the procedure.
kbrewster
Ok, so it keeps cycling because on the form's Activated event I am calling FillClientCodes() and therefore, each time I click the ok button, the form is activated. But I need the code to run on the activated event because from this form you can open another form, but once this second form is close I need the first form to 'refresh'.

How can I "throw this error back up to the calling function so that it can deal with it"?
rabroersma
/>
To pass the exception to the calling function you use:

throw;

in the catch block.

Or if develop your own application specific error class hierarchy, you can throw your own application specific error class:

http://msdn.microsoft.com/en-us/library/1a...28VS.80%29.aspx

< There are different designs you can choose to accomplish this. One design that would make your application forms loosely coupled would be the use of the Observer design pattern..

I could create a linked-list that would store the results of the stored procedure (the observed subject). Any controls that are based on this observed list are notified anytime the list is changed so that they can make an appropriated action. Now all form from never need any code to keep the lists refreshed.

Edited by: rabroersma on Mon Nov 23 10:30:19 EST 2009.
kbrewster
I just changed the FillClientCodes() from a private void to a public void and am calling it from the second form. Now everything seems to be working as expected..thanks!
datAdrenaline
If you do find out the exeception class, as Richard talked about, it is good to understand that you can have more than one catch block in order to suppress a specific exception that is being thrown ... something like this:

CODE
try
    {
        <your code>
    }
    catch (someexceptionclass ex)
    {
        //if the exception is the exception class you specify, then the ex is suppressed
        //and control is passed back to the caller.
        return;
    }
    catch (Exception ex)
    {
        //this will "Throw" the exception to the caller for handling
        throw new Exception("FillClientCodes():" + ex.message);
    }
kbrewster
Thank you Brent! I am sure this will come in handy! sad.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.