-1

My code is like this

 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
    }

}

If any error occurs inside ShowResult function I want ignore that Error and go on. I have tried some thing like adding return in the catch block. But its not helping me, after executing the catch block of ShorResult() it directly goes to the Catch block of GetShippingInstruction , which I do not want. Is there any way to solve this issue?

Sample Code: https://dotnetfiddle.net/Jbse21

And the error I get: {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

None
  • 5,582
  • 21
  • 85
  • 170
  • 4
    Your example is wrong because the catch with a return WILL handle the exception and it will thus NOT bubble up. – TomTom Nov 26 '14 at 11:07
  • @TomTom Well I have pasted the same code , and for some reason, this is what Happening to me. – None Nov 26 '14 at 11:10
  • Why don't you handle exception in `ShowResult` method too. Do not let it throw the exception. `catch (Exception ex) { // Handle ShowResult exception }` – Rohit Nov 26 '14 at 11:14
  • Can you provide code that can reproduce the problem you describe. Here is a simplified version of your code and it does not behave as you describe https://dotnetfiddle.net/fDNdMw. – Ben Robinson Nov 26 '14 at 11:21
  • The `return;` is useless and might interfere. I would remove that line just to be sure. – H H Nov 26 '14 at 11:23
  • I think TomTom is right so do make sure this isn't a diagnostic or debugging problem. – H H Nov 26 '14 at 11:24
  • @BenRobinson I have added my complete code here https://dotnetfiddle.net/Jbse21 – None Nov 26 '14 at 11:29
  • ...which doesn't throw an exception. – J. Steen Nov 26 '14 at 11:29
  • 1
    It's entirely possible that, as stated in an answer below, the exception is not actually in the ShowResult method, but in the statement where you serialize your object. – J. Steen Nov 26 '14 at 11:30
  • @J.Steen No I have now have only the code that I put in fiddle. and the exception I get is {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} – None Nov 26 '14 at 11:33
  • 1
    In that case: http://stackoverflow.com/questions/2041482/unable-to-evaluate-expression-because-the-code-is-optimized-or-a-native-frame-is And please don't look at *just* the accepted answer. Read all of the helpful answers to see if any of them will help you. – J. Steen Nov 26 '14 at 11:34
  • @J.Steen Yap. I guess this is what My problem is. But still no Idea, Why this event bubbling happens;) – None Nov 26 '14 at 11:38

2 Answers2

1

when you call Response.Write - you open your response .. you might need to close it when error occurs while writing.. please try the following:

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

}
ymz
  • 6,602
  • 1
  • 20
  • 39
0

Is it possible that the exception is thrown in the first line of GetShippingInstruction?

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

If so, it's the GetShippingInstruction catch block that will execute.

Resource
  • 524
  • 4
  • 16