Skip to content Skip to sidebar Skip to footer

C# Equivalent To Python's Traceback Library

In this post, python has traceback library to get some detailed error information (line number, file name ...). Does C# have similar functions to know what file/line number that ge

Solution 1:

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

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

If debug data can be loaded (if the .pdb file is in the same directory as the executable/you build in debug mode) this will include file names line numbers. If not it will only include method names.

Solution 2:

Solution 3:

All classes that inherit from Exception have a StackTrace property. It's not quite the same as the Python traceback library because of the nature of how C#/.NET is run (like it won't have an equivalent to tb_lineno). All it is is the string representation of the stack trace.

Post a Comment for "C# Equivalent To Python's Traceback Library"