C# Exceptions Not Giving Line Numbers

后端 未结 5 1995
攒了一身酷
攒了一身酷 2020-12-03 09:55

I am using C# having come from a Java background - I have an exception but it does not tell me the line number - Just the method name.

Is that usual?? Is it down to

5条回答
  •  囚心锁ツ
    2020-12-03 10:11

    The StackTrace property of the Exception class contains line numbers, at least if the debug information (pdb file) is available:

    using System;
    class Program {
        public static void Main() {
            try {
                throw new Exception("test");
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
            }
        }
    }
    

    will give the following output with the pdb file:

    at Program.Main() in X:\code\test\test\Program.cs:line 6
    

    and this without:

    at Program.Main()
    

提交回复
热议问题