0

Hello I have a regular WinForm that calls this:

private void childDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
     try
     {
         SQL.createTable("childDirectory"); //THIS LINE
     }
     catch(SystemException ecp)
     {
         MessageBox.Show(string.Format("An error occurred: {0}", ecp.Message));
     }
}

and have a class named "SQL". The C# class cannot throw a messageBox to the user, only Console.WriteLine:

static public void createTable(string tableToCreate)
        {
            try
            {
                .
                .
                .
                .
            }
            catch (SqlException exp)
            {
                Console.WriteLine("Database not created: " + exp.Message, "Error");
            }
        }

How can I throw this SqlExecption back in the Form.cs call? Sorry if my wording is wrong, hopefully you can understand what I'm trying to do.

ikathegreat
  • 2,311
  • 9
  • 49
  • 80

3 Answers3

2
    static public void createTable(string tableToCreate)
    {
        try
        {
            .
            .
            .
            .
        }
        catch (SqlException exp)
        {
            Console.WriteLine("Database not created: " + exp.Message, "Error");
            throw exp;
        }
    }

And catch it with:

 private void childDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
          SQL.createTable("childDirectory"); //THIS LINE
     }
     catch(SystemException ecp)
     {
        MessageBox.Show(string.Format("An error occurred: {0}", ecp.Message));
     }
     catch (SqlException exp)
     {               

     }
 }

But unless it is necessary you don't need to catch exception in called method if you catch it in calling method.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
  • 3
    Instead of `throw exp;` it should just be `throw;` which preserves the stack trace of the original exception – tbridge Mar 01 '12 at 04:20
1

Since you want to bubble up the exception I would suggest you simply don't catch it in the createTable method - instead add an exception handler for SqlException iny your childDirectoryToolStripMenuItem_Click method.

private void childDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
     try
     {
         SQL.createTable("childDirectory"); //THIS LINE
     }
     catch (SqlException ex)
     {
         MessageBox.Show("Database not created: " + ex.Message);
     }
     catch(SystemException ecp)
     {
         MessageBox.Show(string.Format("An error occurred: {0}", ecp.Message));
     }
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

Why not just let the error propogate up to the windows form, and avoid a double catch? You could do the logging there.

However, if you do indeed want to do it this way, then you would just call throw. This will simply rethrow the exception. The plus to this method over throw exp is that it will keep the original stack trace, and not mislead any debugging. An already vetted explanation for this can be found at: What is the proper way to re-throw an exception in C#?

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180