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?

user2864740
  • 60,010
  • 15
  • 145
  • 220
pithhelmet
  • 2,222
  • 6
  • 35
  • 60
  • 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... – Jfabs Mar 13 '14 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?](http://stackoverflow.com/questions/3326571/how-can-i-view-msil-cil-generated-by-c-sharp-compiler-why-is-it-called-assemb) – Jonathan Lonowski Mar 13 '14 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. – user2864740 Mar 13 '14 at 19:47
  • 1
    When you say "empty try catch block", do you mean if you _literally_ have: `try { } catch { }`? – Chris Sinclair Mar 13 '14 at 19:50
  • what's the real world application of this question? – Jonesopolis Mar 13 '14 at 19:51
  • Jonesy- there is no real world application... it is more of a "what does the c# compiler do in this instance" question. Chris- yup a literally empty try catch. jfabs- why do you feel this is not the right place for this type of question? It has to deal 100% with coding, it deals with the very fundamentals of programming and optimization. SO really needs to implement a ban on anonymous downvotes.... they are non constructive and alienates people, thus enforcing the elitism that is currently hurting apple. – pithhelmet Mar 13 '14 at 19:59
  • @pithhelmet: Then yes, if compiler optimizations are turned on, the compiler will ignore the try/catch. With optimizations turned off (i.e., "debug mode"), then it will emit the IL instructions for debugging purposes. – Chris Sinclair Mar 13 '14 at 20:01
  • @pithhelmet Didn't downvote, but if you think that you could elaborate on what you mean when you say "SO really needs to implement a ban on anonymous downvotes", may I suggest posting it on http://meta.stackoverflow.com/ ? – jdphenix Mar 13 '14 at 20:13
  • jdphenix - i did post over there... and did i ever get flamed... it was like 300 down votes for asking the question.... but thanks for the reply – pithhelmet Mar 13 '14 at 20:17
  • @pithhelmet It almost seemed like a bit more theory oriented but I'll give it to you that it is code based. I would still have to recommend that you included something code-specific like what you had tried to suit your question. It is interesting though. – Jfabs Mar 13 '14 at 21:05

2 Answers2

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".

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
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

kjbartel
  • 10,381
  • 7
  • 45
  • 66
nuclear sweet
  • 1,079
  • 10
  • 27
  • 5
    In the future, you'll want to back up your answers with sources and such – Jfabs Mar 13 '14 at 19:43
  • @Jfabs check this http://prntscr.com/30ielc (i hope i get this question right, because name of topic is pretty confsing) – nuclear sweet Mar 13 '14 at 19:55
  • 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. – Jfabs Mar 13 '14 at 21:00