0

In c#, how much time is expended in an empty try catch block?

I've heard that it is between 10-55ms, but i'm leaning to zero/one as the compiler will replace the empty try catch with a no-op placeholder.

any compiler experts that can verify this information?

10
  • I believe the reason that you're getting so many downvotes is that this probably isn't the appropriate place to ask this particular question... Commented Mar 13, 2014 at 19:43
  • 3
    You can review the generated IL/bytecode yourself. How can I view MSIL / CIL generated by C# compiler? Why is it called assembly? Commented Mar 13, 2014 at 19:46
  • 2
    Make sure to choose a good title, it really helps avoid negative initial reactions/votes. The hypothesis and fundamental question that has been presented is "I'm leaning to zero/one as the compiler will replace the empty try catch with a no-op placeholder.", so focus on that. Commented Mar 13, 2014 at 19:47
  • 1
    When you say "empty try catch block", do you mean if you literally have: try { } catch { }? Commented Mar 13, 2014 at 19:50
  • what's the real world application of this question? Commented Mar 13, 2014 at 19:51

2 Answers 2

5

If when you say "empty try catch block", you mean literally:

try
{
}
catch
{
}

Then yes, when building with compiler optimizations turned on (that is, "release" mode), it will not emit any IL instructions. For example:

private void Test()
{
    try
    {
    }
    catch
    {
    }
}

Compiles to:

IL_0000:  ret

Which is the same as if it were an empty method.

However, when optimizations are turned off (that is, "debug" mode), it emits:

IL_0000:  nop         
IL_0001:  nop         
IL_0002:  nop         
IL_0003:  leave.s     IL_000A
IL_0005:  pop         
IL_0006:  nop         
IL_0007:  nop         
IL_0008:  leave.s     IL_000A
IL_000A:  nop         
IL_000B:  ret 

Whereas an empty method would be:

IL_0000:  nop         
IL_0001:  ret 

Whether or not these IL instructions are stripped when the JIT compiler is executed, I am not positive. But this would still only apply when compiling without compiler optimizations as with them turned on the try/catch is stripped out during the initial compile to IL before it hits the JIT compiler.

EDIT: I just realized I may not have actually answered the question:

In release mode, zero time is expended on an empty try/catch.
In debug mode, a non-zero time is expended on an empty try/catch as it still emits the necessary IL code for debugging/breakpoint purposes and requires some JIT compilation. However, it would be a negligibly small amount of time. Definitely nowhere near the neighbourhood of "10-55 milliseconds".

Sign up to request clarification or add additional context in comments.

Comments

2

If you check compiled code by proper software (like ILspy or Reflector), you can see that empty block will be deleted completley by compiler.

ILSpy image

3 Comments

In the future, you'll want to back up your answers with sources and such
@Jfabs check this prntscr.com/30ielc (i hope i get this question right, because name of topic is pretty confsing)
looks like you are correct to me. Thank you for following up. I would recommend you edit your post to include that screenshot as evidence of your statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.