停止在C#中冒泡的异常?

时间:2014-11-26 11:05:11

标签: c# asp.net

我的代码就像这样

 private void GetShippingInstruction()
{
    try
    {
        string json = JsonConvert.SerializeObject(ds, Formatting.Indented);
        ShowResult(json);
    }
    catch (Exception ex)
    {
       //custom logic        
    }

}

 private void ShowResult(string json )
{
    try
    {
        Response.Write(json);
        Response.End();
    }
    catch 
    { 
       return;
        // do nothing for now
    }

}

如果在ShowResult函数中发生任何错误,我想忽略该错误并继续。我尝试过在catch块中添加return之类的东西。但它没有帮助我,在执行ShorResult()的catch块后,它直接进入GetShippingInstruction的Catch块,这是我不想要的。有什么方法可以解决这个问题吗?

示例代码:https://dotnetfiddle.net/Jbse21

我得到的错误:{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

2 个答案:

答案 0 :(得分:1)

当你调用Response.Write时 - 你打开你的回复..你可能需要在写错时关闭它...请尝试以下方法:

private void ShowResult(string json)
{
    try
    {
         if (string.IsNullOrEmpty(json)) return; // no need to get an exception for that

         Response.Write(json);

    }
    catch 
    { 

    }
    finally
    {
        Response.End();
    }

}

答案 1 :(得分:0)

是否有可能在GetShippingInstruction的第一行抛出异常?

  

string json = JsonConvert.SerializeObject(ds,Formatting.Indented);

如果是这样,它将是将执行的GetShippingInstruction catch块。

相关问题