• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#打印出发生错误的文件,方法,代码所在行和列

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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.

 1 using System;
 2 using System.Diagnostics;
 3 namespace ConsoleApplication1
 4 {
 5   class Program
 6   {    
 7     static void Main(string[] args)
 8     {      
 9       try
10       {
11         TestFunction();
12       }
13       catch (Exception ex)
14       {
15         StackTrace st = new StackTrace(ex, true);
16         StackFrame[] frames = st.GetFrames();
17 
18         // Iterate over the frames extracting the information you need
19         foreach (StackFrame frame in frames)
20         {
21           Console.WriteLine("{0}:{1}({2},{3})", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber());
22         }
23       }
24 
25       Console.ReadKey();
26     }
27 
28     static void TestFunction()
29     {      
30       throw new InvalidOperationException();
31     }
32   }
33 }

 

 1             catch (Exception ex)
 2             {
 3                 StackTrace st = new StackTrace(ex, true);
 4                 StackFrame[] frames = st.GetFrames();
 5                 string Mssg="";
 6                 // Iterate over the frames extracting the information you need
 7                 foreach (StackFrame frame in frames)
 8                 {
 9                     //Console.WriteLine("{0}:{1}({2},{3})", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber());
10                            //Mssg =frame.GetFileName();
11                            Mssg=frame.GetMethod().Name;
12                            Mssg+="("+frame.GetFileLineNumber()+":";
13                            Mssg += frame.GetFileColumnNumber()+")";
14                 }
15                 MessageBox.Show(Mssg.ToString(), "提示");
16             }

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--

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#TaskwaitAll,WhenAll,ContinueWith发布时间:2022-07-10
下一篇:
【转】C#中属性PropertyInfo的使用发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap