关键词搜索

源码搜索 ×
×

C# 异常处理(Catch Throw)IL分析

发布2015-10-28浏览6259次

详情内容

1、catch throw的几种形式及性能影响:

  1. private void Form1_Click(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. }
  6. catch
  7. {
  8. throw;
  9. }
  10. }
  11. private void Form1_Load(object sender, EventArgs e)
  12. {
  13. try
  14. {
  15. }
  16. catch (Exception)
  17. {
  18. throw;
  19. }
  20. }
  21. private void Form1_Enter(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. }
  26. catch (Exception ee)
  27. {
  28. throw;
  29. }
  30. }
  31. private void Form1_DoubleClick(object sender, EventArgs e)
  32. {
  33. try
  34. {
  35. }
  36. catch (Exception ee)
  37. {
  38. throw ee;
  39. }
  40. }
        对应的IL代码(以下代码是release版本的IL代码):

.method private hidebysig instance void  Form1_Click(object sender,
                                                     class [mscorlib]System.EventArgs e) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       1 (0x1)
  .maxstack  1
  .locals init ([0] class [mscorlib]System.Exception ee)
  IL_0000:  ret
} // end of method Form1::Form1_DoubleClick
        可以看到Form1_Click、Form1_Load、Form1_Enter中的try catch已经被编译器优化掉了:

IL_0000:  ret         //即为  return  标记 返回值

        只有Form1_DoubleClick中的try catch中对try catch进行了处理:

 .locals init ([0] class [mscorlib]System.Exception ee) //定义 Exception 类型参数 ee (此时已经把ee存入了Call Stack中)
        即在Form1_DoubleClick中的try catch才会对性能产生影响。

==》可以看出一下三种try catch的写法对于release版本的代码来说是完全一样,也不会产生任何的性能消耗:

  1. try
  2. {
  3. }
  4. catch
  5. {
  6. throw;
  7. }
  8. try
  9. {
  10. }
  11. catch (Exception)
  12. {
  13. throw;
  14. }
  15. try
  16. {
  17. }
  18. catch (Exception ee)
  19. {
  20. throw;
  21. }
对于上面的结论大家可以写测试demo验证一下 (已测试,结果与分析一致偷笑)。       

那么对于debug模式下的IL代码是什么样子的呢?

.method private hidebysig instance void  Form1_Click(object sender,
                                                     class [mscorlib]System.EventArgs e) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       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) cil managed
{
  // 代码大小       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
        可以看出四种写法在debug模式下区别只是:rethrow与throw的区别。IL中rethrow与throw分别代表啥呢?

        Throw:引发当前位于计算堆栈上的异常对象。
        Rethrow:再次引发当前异常。

        即当我们抛出一个异常时, CLR会重新设置一个异常起始点。 CLR只记录最近一次异常抛出的位置。下面代码抛出一个异常,从而导致CLR重新设置该异常的起始点:

  1. try
  2. {
  3. //一些处理
  4. }
  5. catch (Exception e)
  6. {
  7. //一些处理
  8. throw e; //CLR认为这里是异常的起始点
  9. }
         相反,如果我们抛出一个异常对象, CLR将不会重新设置其堆栈的起始点,下面代码抛出一个异常,但不会导致CLR重新设置异常的起始点:

  1. try
  2. {
  3. //一些处理
  4. }
  5. catch (Exception e)
  6. {
  7. //一些处理
  8. throw; //CLR不会重新设置异常的起始点
  9. }
         C#中使用throw和throw ex抛出异常,但二者是有区别的。

        在C#中推荐使用throw;来抛出异常;throw ex;会将到现在为止的所有信息清空,认为你catch到的异常已经被处理了,只不过处理过程中又抛出新的异常,从而找不到真正的错误源。

throw e重新抛出异常,并非转发原来的异常,而会更改包括StackTrace在内的许多异常内部信息;对于调用连很深情况,性能损耗超出想象。

拓展阅读:

IL指令详细

.NET中异常处理的最佳实践(译



相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载