C # equivalent to Python trace library

In this post , python has a trace library to get detailed error information (line number, file name ...).

Does C # have similar functions to find out which file / line number throws an exception?

ADDED

I came up with the following code based on the answers.

using System; // https://stackoverflow.com/questions/4474259/c-exception-handling-with-a-string-given-to-a-constructor class WeekdayException : Exception { public WeekdayException(String wday) : base("Illegal weekday: " + wday) {} } class TryCatchFinally { public static void Main() { try { throw new WeekdayException("thrown by try"); } catch(WeekdayException weekdayException) { Console.WriteLine(weekdayException.Message); Console.WriteLine(weekdayException.StackTrace); } } } 

And he gives me this message:

 Illegal weekday: thrown by try at TryCatchFinally.Main () [0x00000] in <filename unknown>:0 
+2
source share
3 answers

When you catch an exception, you can look at its stack trace, which should give similar results:

 catch(Exception e) { Console.WriteLine(e.StackTrace); } 

If debugging data can be downloaded (if the .pdb file is in the same directory as the executable / you are creating in debugging mode), this will contain the line numbers of the file names. If not, it will only include method names.

+4
source

When an exception is thrown, check the stack trace - it will give you the same results.

+1
source

All classes that inherit from Exception have the StackTrace property. This is not quite the same as the Python traceback library due to the way C # /. NET works (for example, it will not have the equivalent of tb_lineno ). All this is a string representation of the stack trace.

+1
source

Source: https://habr.com/ru/post/1332546/


All Articles