显示的行数量在例外处理
-
22-08-2019 - |
题
怎么会显示什么线数造成的错误和为这甚至可能的方式。净汇编。前男友?
如果不是有自动化的方式为异常。消息来显示的子,放弃了?
try
{
int x = textbox1.Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
解决方案
使用ex.ToString()
得到完整的堆栈跟踪。
您必须在调试符号(.pdb文件),即使是在释放模式编译,得到行号(这是该项目的一个选项生成属性)。
其他提示
看看这堆栈跟踪对于给定的例外,使用 e.堆栈跟踪
如果你需要更多的详细信息,可以使用 系统。诊断。堆栈跟踪 类(在这里是一些代码为你尝试):
try
{
throw new Exception();
}
catch (Exception ex)
{
//Get a StackTrace object for the exception
StackTrace st = new StackTrace(ex, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0);
//Get the file name
string fileName = frame.GetFileName();
//Get the method name
string methodName = frame.GetMethod().Name;
//Get the line number from the stack frame
int line = frame.GetFileLineNumber();
//Get the column number
int col = frame.GetFileColumnNumber();
}
这只会的工作,如果有一个pdb文件可用于大会。看到该项目属性--建立项进行"调试"的信息的选择,以确保有一个pdb文件。
如果您使用 '堆栈跟踪' 和包括在工作目录的.pdb文件,栈跟踪应该包含行号。
string lineNumber=e.StackTrace.Substring(e.StackTrace.Length - 7, 7);
不隶属于 StackOverflow