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

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

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

时间: 2024-08-01 20:42:09

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

关于vs2010新建MFC项目时弹出当前页面的脚本发生错误的解决方法

最近装了vs2010但是在新建项目的时候老是弹出当前页面的脚本发生错误,问了好多人网上也查了,结果还是不行,后来就自己尝试着解决,没想到竟然成功了,纪念一下,帮助需要解决此问题的人解决这样的问题. 正文: 首先看看问题,插入图片 我们就根据它提示错误的地方,找到它所提示的位置,比如第一张提示提示Program Files (x86)\Microsoft Visual Studio 10.0\VC\VCWizards\AppWiz\MFC\Application\html\2052\default

创建Web项目运行时出小错误及解决方法

1.目录结构 2.各文件内容 index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="/Servlet" method="

win7系统提示当前页面的脚本发生错误的解决方法

问题:服务器没问题,代码和svn一致,其他电脑OK,我的电脑就是不行,所以应该是电脑问题: 解决方法如下: 方法一: IE设置 1.打开IE浏览器,找到[设置]图标,打开[Internet 选项]: 2.打开[安全]选项,点击右下角的[将所有区域重置为默认级别],然后点击[应用] 3.选择[高级],点击[重置],然后勾选[删除个性化设置],点击[重置],后续按提示操作即可 方法二. 注册IE组件< 1.使用快捷键;Win+R;弹出[运行],输入:regsvr32 Actxprxy.dll,点击[

Linux 下段错误 core文件

什么是core dump? core的意思是内存,dump的意思是扔出来,堆出来:当一个程序奔溃时,在进程当前工作目录的core文件中复制了该进程的存储图像.core文件仅仅是一个内存映像(同时加上调试信息),主要用来调试的. 为什么没有core文件生成呢? 有时候程序down了,但是core文件却没有生成.core文件的生成跟你当前系统的环境设置有关系,可以用下面的语句设置一下便生成core文件了 在linux平台下,设置core dump文件生成的方法:1. 在终端中输入ulimit -c

php 算法之------------如何打印出下图

自己偶尔看到了下图,于是用php打印出下图. 两种方法解决此问题: 方法一:根据图分析该图是一个二维数组,可用二维数组解决此类问题,6行就是6个数组,每一行就代表数组有多少个元素.知道每个数组元素的个数,不就可以知道数组中有哪几个元素吗? function array_chunk_vertical($arr, $colun) { $arr_length = count($arr); $parem = floor($arr_length / $colun); $pare = $arr_length

jquery动态加载js/css文件方法

先来看jquery自带的getSrcript文件 方法 代码如下 复制代码 $.getScript(url,callback) 实例 代码如下 复制代码 var testVar = 'New JS loaded!'; alert(testVar); function newFun(dynParam) { alert('You just passed '+dynParam+ ' as parameter.'); } 动态调用方法 HTML代码 代码如下 复制代码 <script type="

C# .Net FrameWork3.5中异步HTTP请求时,由于安全协议的问题System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)方法抛出“基础连接已经关闭: 发送时发生错误”的解决办法

现象描述: C# .Net FrameWork3.5中异步HTTP请求时,由于安全协议的问题System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)方法抛出“基础连接已经关闭: 发送时发生错误”. 原因分析: 大多数情况下是由于客户端所设置的HTTP访问请求的安全协议不符合服务器端的安全协议要求.比如,NASA提供瓦片服务的http://worldwind25.arc.nasa.gov/wms?service=WMS&v

启动 Eclipse 弹出&ldquo;Failed to load the JNI shared library jvm.dll&rdquo;错误的解决方法!

启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/zyz511919766/article/details/7442633   原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 对策:要么两者都安装64位的,要

启动Eclipse 弹出“Failed to load the JNI shared library”错误的解决方法

原因1:eclipse的版本与jre或者jdk版本不一致 对策:要么两者都安装64位的,要么都安两个是32位一个是64位. 原因2:给定目录下jvm.dll不存在 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因1的概率更大一些,原因2不太可能发生. 启动Eclipse 弹出"Failed to load the JNI shared library"错误的解决方法,布布扣,bubuko.com