1

I have a segment of code that looks like this:

try
{
  // Do stuff
  operationInfo.success = true;
}
catch
{
  operationInfo.success = false;
  throw;
}
finally
{
  try
  {
     UploadToServer(operationInfo);
  }
  catch
  {
     // Suppress, it's ok if we can't upload to the server
  }
}

my question is, what happens to the stack trace in this case if an exception is thrown in the main try block, and then again the finally block actually throws (and handles) another exception in the UploadToServer method? Do you get the stack trace of the last exception that was raised or will it be bubbled up correctly?

I'm looking more for an explanation of how does the IL manage what happens under the hood than a simple "yes or no" answer.

Thx.

Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • 1
    ..give it a try? Also, if not being able to "upload to the server" is expected.. it isn't exceptional.. and therefore not really a good candidate for an `Exception`.. – Simon Whitehead Apr 03 '13 at 11:40
  • I will give it a try. I posted it as a question because I find it interesting, I think it's a good question to have in SO and because giving it a try won't give me any insight about how IL handles that situation and I don't have the time right now to go and dissasemble it to check. – Jorge Córdoba Apr 03 '13 at 11:45
  • UploadToServer can throw an exception and has to execute always, disregarding of whether an exception happens in the operation or not, hence I think is suitable for the finally clause. While we can discuss the suitability of putting that on the finally clause (maybe I'll just take it out) the pattern itself is valid if the finally was doing some cleanup that could throw another exception. – Jorge Córdoba Apr 03 '13 at 11:48
  • If the intention was to have a good question in SO, then it's already there. http://stackoverflow.com/questions/11039591/nested-try-finally-in-c-sharp, – Sandeep Apr 03 '13 at 12:42
  • Sandeep, not the same case. I want to know how the stacktrace will behave in that scenario. The question you post to has nothing to do with that... note that the exception in the finally block in this case is always handled... – Jorge Córdoba Apr 03 '13 at 13:00
  • http://stackoverflow.com/questions/2911215/what-happens-in-c-sharp-if-a-finally-block-throws-an-exception – Lanorkin Apr 03 '13 at 13:20

0 Answers0