2

I am catching an exception and processing it. Somewhere up the call tree, I am doing the same. Once I process my exception at the child level, I want to also go ahead and invoke the exception handler, wherever it is, somewhere up the call tree.

For that, I thought I would do run the throw again. But instead of breaking somewhere up the call tree, it is breaking in the place where I am doing the throw and crashing, at this line:

throw new Exception("Cannot Write Header Row to Database " + Msg);

code:

public static void NewHeaderRow(string FILE_REV_NUMBER, DateTime FILE_CREATE_DATE, string EDC_DUNS_NUMBER, int RunId)
{

    SqlConnection connection = null;

    try
    {
        connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());
        connection.Open();
        SqlCommand com;
        com = new SqlCommand("dbo.INSERT_PPL_HEADER", connection);


        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(new SqlParameter("@FILE_REV_NUMBER", FILE_REV_NUMBER));
        com.Parameters.Add(new SqlParameter("@FILE_CREATE_DATE", FILE_CREATE_DATE));
        com.Parameters.Add(new SqlParameter("@EDC_DUNS_NUMBER", EDC_DUNS_NUMBER));
        com.Parameters.Add(new SqlParameter("@RunId", RunId));


        if (com.Connection.State == ConnectionState.Closed) com.Connection.Open();

        com.ExecuteNonQuery();

    }
    catch (Exception e)
    {
        string Msg;
        Msg = "Encountered unexpected program issue.  Please contact your program administator.  Error details...";
        Msg = Msg + System.Environment.NewLine;
        Msg = Msg + System.Environment.NewLine;
        Msg = Msg + e.ToString();
        Msg = Msg + System.Environment.NewLine;
        Msg = Msg + System.Environment.NewLine;
        Msg = Msg + e.Message;

        throw new Exception("Cannot Write Header Row to Database " + Msg);
    }
    finally
    {
        if (connection == null) { } else connection.Close();
    }

}
Kam Mistry
  • 105
  • 8
  • 2
    Does this answer your question? [What is the proper way to re-throw an exception in C#?](https://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c) – gunr2171 Dec 10 '19 at 19:23
  • based on that link, I seem to be doing it right, but it isn't working.... does the handler have to be in the method that calls this method, or can it be several points upward on the call stack? – Kam Mistry Dec 10 '19 at 19:29
  • @kammistry, errors propagate "upwards" - when they are thrown, they go all the way up the call stack until they are handled or the program crashes. That handler can be several methods "above" the method in the call stack where the error is originally thrown. – bakester14 Dec 10 '19 at 21:42

3 Answers3

1

Try just using the throw keyword, instead of building a new exception.

https://stackoverflow.com/a/2999314/5145250

bakester14
  • 153
  • 1
  • 4
  • 14
  • If you think this question is a duplicate, please flag/vote to close the post rather than adding new answers. – gunr2171 Dec 10 '19 at 19:30
  • I don't think it's a duplicate. I think the answer is the same as another question, but the question is different. – bakester14 Dec 10 '19 at 19:31
  • didn't work. still crashed with same error as what I was in the catch for... – Kam Mistry Dec 10 '19 at 20:32
  • I thought that was your desired behavior? If not, it indicates to me that you are not handling exceptions the right way upstream. Perhaps you have some middleware that is catching and throwing exceptions before they hit your exception handler? I'd try reducing the complexity to root out the problem. At the top of your `NewHeaderRow` method, throw a new exception and see if it gets handled correctly. – bakester14 Dec 10 '19 at 21:39
1

To add additional information to the exception warp it in another exception object and pass the original exception as argument with new message to keep the original stack trace in inner exception.

throw new Exception("Cannot Write Header Row to Database " + Msg,  e);

At some top level you should handle global exceptions to avoid crashing.

vendettamit
  • 14,315
  • 2
  • 32
  • 54
1

The way I was finally able to pin point the problem was to extremely simplify my code so as to be able to see the problem clearly. I just copied my solution to a new location, and gutted out all the non-essential stuff -- stuff I knew was not going to be important for the purposes of troubleshooting.... Very effective way of troubleshooting difficult problems that are hard to trace.... Here is what I ended up with (the simple code).

I was not catching general exception in the code that calls NewHeaderRow. I was catching System.IO exception. So, because code had nowhere to go, it crashed.... It is very hard for the eyes to catch this error and also difficult to trace.

private void button1_Click(object sender, EventArgs e)
{
    LoadFile();

}

private static int ProcessHeaderRow(string line)
{

    int LoadRunNumber = 0;

    try
    {
        //some complex logic was here; error occurs here, so I throw an exception....
        throw new Exception("An Error Occurs -- Process Header Row Try block");
    }
    catch (CustomExceptionNoMessage e)
    {
        throw new CustomExceptionNoMessage(e.Message);
    }
    catch (Exception e)
    {
        //Process the exception, then rethrow, for calling code to also process the exception....
        //problem is here...XXXXXXXXXXXXXXXXXX
        throw new Exception(e.Message);  //crashes

    }
    return LoadRunNumber;
}

public static bool LoadFile()
{

    int RunId = 0;

    try
    {
        RunId = ProcessHeaderRow("10~~happy~007909427AC");
        MessageBox.Show("Completed Upload to Cloud...");
    }
    catch (CustomExceptionNoMessage ce)
    {
        MessageBox.Show(ce.Message);
    }
    catch (System.IO.IOException e)   //CHANGED THIS LINE, AND I AM UP AND RUNNING (Changed to Exception e)
    {
        MessageBox.Show(e.Message);
    }

    return true;
}


    public class CustomExceptionNoMessage : Exception
    {
        public CustomExceptionNoMessage()
        {
        }

        public CustomExceptionNoMessage(string message)
            : base(message)
        {
        }

        public CustomExceptionNoMessage(string message, Exception inner)
            : base(message, inner)
        {
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LoadFile();

    }
Kam Mistry
  • 105
  • 8