重新抛出异常难度-异常使程序崩溃而不是重新抛出

时间:2019-12-10 19:22:31

标签: c# exception try-catch

我正在捕获异常并对其进行处理。 在调用树的某个地方,我正在做同样的事情。 在子级处理完异常后,我还要继续调用异常处理程序,无论它在哪里,都在调用树的某个位置。

为此,我认为我会再次进行掷球。 但是,它不是在调用树的某处中断,而是在我正在执行抛出并崩溃的地方中断,在此行:

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

代码:

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();
    }

}

3 个答案:

答案 0 :(得分:1)

尝试仅使用throw关键字,而不要建立新的异常。

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

答案 1 :(得分:1)

要向异常中添加其他信息,请在另一个异常对象中扭曲它,然后将原始异常作为参数与新消息一起传递,以将原始堆栈跟踪保留在内部异常中。

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

在某些顶级级别,您应该处理全局异常以避免崩溃。

答案 2 :(得分:0)

我最终能够指出问题的方法是极大地简化我的代码,以便能够清楚地看到问题。我只是将我的解决方案复制到了一个新位置,然后剔除了所有不必要的东西-我知道的东西对于故障排除来说并不重要。...一种非常有效的方法来解决难以解决的难题跟踪...。这就是我最后得到的(简单代码)。

我没有在调用NewHeaderRow的代码中捕获一般异常。 我正在捕获System.IO异常。 因此,由于代码无处可去,因此崩溃了……。 眼睛很难抓住这个错误,也很难追踪。

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();

    }