问题描述
在,Python已经回溯库得到一些详细的错误信息(行号,文件名...)。
C#有类似的功能要知道什么样的文件/行号产生的例外呢?
ADDED
我想出了基于以下代码。答案
使用系统;
// http://stackoverflow.com/questions/4474259/c-exception-handling-with-a-string-given-to-a-constructor
级WeekdayException:异常{
公共WeekdayException(字符串wday):基地(非法平日:+ wday){}
}
类TryCatchFinally
{
酒店的公共静态无效的主要()
{
试
{
抛出新WeekdayException(通过试抛出);
}
赶上(WeekdayException weekdayException){
Console.WriteLine(weekdayException.Message);
Console.WriteLine(weekdayException.StackTrace);
}
}
}
和它给了我这个消息
非法平日:上述<通过试
在TryCatchFinally.Main()[0x00000]抛出;文件名不明> :0
当你发现一个异常可以查看其堆栈跟踪,这应该给类似的结果:
赶上(例外五)
{
Console.WriteLine (e.StackTrace);
}
如果调试数据可以被加载(如果.pdb文件是在相同的目录作为可执行/你在调试模式下建立),这将包括文件名,行号。如果不是,将只包括方法名。
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 generates the exception?
ADDED
I came up with the following code based on the answers.
using System;
// http://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 it gives me this message:
Illegal weekday: thrown by try
at TryCatchFinally.Main () [0x00000] in <filename unknown>:0
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.
这篇关于C#相当于Python的回溯库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!