C # Catch Throw IL Analysis

Source: Internet
Author: User
Tags mscorlib

C # Catch Throw IL Analysis

1. Several Forms of catch throw and performance impact:

 

 private void Form1_Click(object sender, EventArgs e)        {            try            {            }            catch            {                throw;            }        }        private void Form1_Load(object sender, EventArgs e)        {            try            {            }            catch (Exception)            {                throw;            }        }        private void Form1_Enter(object sender, EventArgs e)        {            try            {            }            catch (Exception ee)            {                throw;            }        }        private void Form1_DoubleClick(object sender, EventArgs e)        {            try            {            }            catch (Exception ee)            {                throw ee;            }        }
The corresponding IL Code (the following code is the IL code of the release version ):

 

. Method private hidebysig instance void Form1_Click (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 1 (0x1 ). maxstack 8 IL_0000: ret} // end of method Form1: Form1_Click.method private hidebysig instance void Form1_Load (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 1 (0x1 ). maxstack 8 IL_0000: ret} // end of method Form1: Form1_Load.method private hidebysig instance void Form1_Enter (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 1 (0x1 ). maxstack 8 IL_0000: ret} // end of method Form1: Form1_Enter.method private hidebysig instance void Form1_DoubleClick (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 1 (0x1 ). maxstack 1. locals init ([0] class [mscorlib] System. exception ee) IL_0000: ret} // end of method Form1: Form1_DoubleClick
We can see that the try catch in Form1_Click, Form1_Load, and Form1_Enter has been optimized by the compiler:

 

 

IL_0000: ret // indicates the return value.

 

Only try catch in Form1_DoubleClick is processed:

 

. Locals init ([0] class [mscorlib] System. Exception ee) // defines the Exception type parameter ee (ee has been saved to the Call Stack)
That is, try catch in Form1_DoubleClick will affect the performance.

= "It can be seen that the three try catch statements are exactly the same for the release version code, and will not produce any performance consumption:

 

           try            {            }            catch            {                throw;            }            try            {            }            catch (Exception)            {                throw;            }            try            {            }            catch (Exception ee)            {                throw;            }
For the above conclusions, you can write a test demo for verification. I have verified it.

 

So what does the IL Code look like in debug mode?

 

. Method private hidebysig instance void Form1_Click (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 11 (0xb ). maxstack 1 IL_0000: nop. try {IL_0001: nop IL_0002: nop IL_0003: leave. s IL_0009} // end. try catch [mscorlib] System. object {IL_0005: pop IL_0006: nop IL_0007: rethrow} // end handler IL_0009: nop IL_000a: ret} // end of method Form1 :: form1_Click.method private hidebysig instance void Form1_Load (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 11 (0xb ). maxstack 1 IL_0000: nop. try {IL_0001: nop IL_0002: nop IL_0003: leave. s IL_0009} // end. try catch [mscorlib] System. exception {IL_0005: pop IL_0006: nop IL_0007: rethrow} // end handler IL_0009: nop IL_000a: ret} // end of method Form1 :: form1_Load.method private hidebysig instance void Form1_Enter (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 11 (0xb ). maxstack 1. locals init ([0] class [mscorlib] System. exception ee) IL_0000: nop. try {IL_0001: nop IL_0002: nop IL_0003: leave. s IL_0009} // end. try catch [mscorlib] System. exception {IL_0005: stloc.0 IL_0006: nop IL_0007: rethrow} // end handler IL_0009: nop IL_000a: ret} // end of method Form1 :: form1_Enter.method private hidebysig instance void Form1_DoubleClick (object sender, class [mscorlib] System. eventArgs e) Pencil managed {// code size 11 (0xb ). maxstack 1. locals init ([0] class [mscorlib] System. exception ee) IL_0000: nop. try {IL_0001: nop IL_0002: nop IL_0003: leave. s IL_0009} // end. try catch [mscorlib] System. exception {IL_0005: stloc.0 IL_0006: nop IL_0007: ldloc.0 IL_0008: throw} // end handler IL_0009: nop IL_000a: ret} // end of method Form1: Form1_DoubleClick
We can see that the difference between the four writing methods in debug mode is the difference between rethrow and throw. What does rethrow and throw represent in IL?

 

Throw: The exception object currently on the computing stack is thrown.
Rethrow: The current exception is thrown again.

That is, when an exception is thrown, the CLR will reset the exception start point. CLR only records the location where the last exception was thrown. The following code throws an exception, causing CLR to reset the start point of the exception:

 

Try {// some processing} catch (Exception e) {// some processing throw e; // CLR considers this as the starting point of the Exception}
On the contrary, if we throw an exception object, the CLR will not reset the starting point of its stack. The following code throws an exception, but will not cause the CLR to reset the starting point of the exception:

 

Try {// some processing} catch (Exception e) {// some processing throw; // CLR will not reset the Exception start point}
In C #, throw and throw ex are used to throw an exception, but the two are different.

Throw; is recommended in C # To throw an exception. throw ex; clears all information so far and considers that the exception you caught has been handled, however, a new exception is thrown during the processing, and the real error source cannot be found.

Extended reading:

IL command details

Best practices for exception handling in. NET)

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.