【发布时间】:2010-12-17 20:00:14
【问题描述】:
在this post中,python有traceback库来获取一些详细的错误信息(行号,文件名...)。
C# 是否有类似的函数可以知道生成异常的文件/行号?
添加
我根据答案想出了以下代码。
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);
}
}
}
它给了我这样的信息:
Illegal weekday: thrown by try
at TryCatchFinally.Main () [0x00000] in <filename unknown>:0
【问题讨论】:
-
标题的写法,听上去好像是在寻找相当于python中C#的库。
-
@Falmarri :感谢您指出。
标签: c# python exception-handling error-handling