Why .net exception is not caught?

前端 未结 1 1655
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 19:56

Consider the following \"Safe\" program:

internal class Safe
{
    public static void SafeMethodWillNeverThrow()
    {
        try
        {
            var          


        
相关标签:
1条回答
  • 2020-12-17 20:19

    It is because you have Code Contracts Runtime Contract Checking enabled in your project properties you use Release configuration. And if you are, your SafeMethodWillNeverThrow() method gets converted to the following with the help of Code Contracts rewriter:

    public static void SafeMethodWillNeverThrow()
    {
        object something = ThrowsNewException();
        try
        {
            Func<int, string> func1 = p => something.ToString();
        }
        catch (Exception)
        {
        }
    }
    

    Ouch!

    Conclusion: Do not trust in what you see - read IL :).

    The issue is reproducible with following Code Contracts versions:

    1. 1.4.50327.0

    2. 1.4.50126.1

      I am using Code Contracts and would like to have the error fixed ASAP. I have posted it to Code Contracts forum. The only way to have it fixed soon is to attract enough attention to it. So please vote up, especially on the Code Contracts forum

    Update May 2016:

    Version 1.9.10714.2 gives a different exception Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program.

    0 讨论(0)
提交回复
热议问题