C# 打印出发生错误的文件,方法,代码所在行和列
2014-06-19 17:29
477 查看
Can someone please tell me how to get the line number of the code where the error occurred and display it to the console?
If you want the file and line numbers, you do not need to parse the StackTrace string. You can use System.Diagnostics.StackTrace to create a stack trace from an exception, with this you can enumerate the stack frames and get the filename, line number and column that the exception was raised. Here is a quick and dirty example of how to do this. No error checking included. For this to work a PDB needs to exist with the debug symbols, this is created by default with debug build.
![](http://images.cnitblog.com/i/127669/201406/191734052547637.png)
http://stackoverflow.com/questions/2723607/exception-handling-display-line-number-where-error-occurred
http://208.71.46.190/search/srpcache?ei=UTF-8&p=C%23+error+code+line+num&fr=yfp-t-950&u=http://cc.bingj.com/cache.aspx?q=C%23+error+code+line+num&d=4975892695745249&mkt=en-US&setlang=en-US&w=8LK2LxEPWkHWxjo07q5TTWPArOhQfQ6u&icp=1&.intl=us&sig=AJMgmyuWl4twmW4AURZ0Dg--
If you want the file and line numbers, you do not need to parse the StackTrace string. You can use System.Diagnostics.StackTrace to create a stack trace from an exception, with this you can enumerate the stack frames and get the filename, line number and column that the exception was raised. Here is a quick and dirty example of how to do this. No error checking included. For this to work a PDB needs to exist with the debug symbols, this is created by default with debug build.
using System; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { TestFunction(); } catch (Exception ex) { StackTrace st = new StackTrace(ex, true); StackFrame[] frames = st.GetFrames(); // Iterate over the frames extracting the information you need foreach (StackFrame frame in frames) { Console.WriteLine("{0}:{1}({2},{3})", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber()); } } Console.ReadKey(); } static void TestFunction() { throw new InvalidOperationException(); } } }
catch (Exception ex) { StackTrace st = new StackTrace(ex, true); StackFrame[] frames = st.GetFrames(); string Mssg=""; // Iterate over the frames extracting the information you need foreach (StackFrame frame in frames) { //Console.WriteLine("{0}:{1}({2},{3})", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber()); //Mssg =frame.GetFileName(); Mssg=frame.GetMethod().Name; Mssg+="("+frame.GetFileLineNumber()+":"; Mssg += frame.GetFileColumnNumber()+")"; } MessageBox.Show(Mssg.ToString(), "提示"); }
![](http://images.cnitblog.com/i/127669/201406/191734052547637.png)
http://stackoverflow.com/questions/2723607/exception-handling-display-line-number-where-error-occurred
http://208.71.46.190/search/srpcache?ei=UTF-8&p=C%23+error+code+line+num&fr=yfp-t-950&u=http://cc.bingj.com/cache.aspx?q=C%23+error+code+line+num&d=4975892695745249&mkt=en-US&setlang=en-US&w=8LK2LxEPWkHWxjo07q5TTWPArOhQfQ6u&icp=1&.intl=us&sig=AJMgmyuWl4twmW4AURZ0Dg--
相关文章推荐
- jsp中上传大文件(200M已测试),存到数据库,易发生连接超时的ie找不到网页的错误,其解决方法。略写代码,参考(.net修改代码应该可以,改天修改)
- Installshield 在安装过程中发生 错误代码:-6001 -6003 -6002 -6004 -6005 -6006等解决方法
- 在VS2008中使用C#代码打印 RichTextBox 控件的内容[方法采自微软支持][Part 1]
- C#文件下载方法代码
- .NET下编译C++代码时出现fatal error C1083: 无法打开包含文件:“iostream.h”: No such file or directory错误的解决方法.
- ASP 在Windows 2003 server使用纯代码上传大文件遇到的错误,及解决方法
- C#清除IE临时文件缓存cookies的方法及核心代码
- .NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#软件反编译后代码错误的改错方法
- Access文件(.mdb)导入SQL Server时"错误代码为 0xC0202009"的解决方法
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- # include <errno.h >查看错误代码errno是调试程序的一个重要方法。当Linux C API函数发生异常时,一般会将errno变量赋值一个整数,不同的值表示不同的含义,可以通过查看
- Access文件(.mdb)导入SQL Server时"错误代码为 0xC0202009"的解决方法
- 用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
- [转]发生 LC.exe 已经退出 代码-1 错误的解决方法
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- 安装AJAX Documents 时候发生的web.config文件配置错误的解决方法
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)