C# 如何获取错误所在行数
来源:互联网 发布:软件开发好学吗? 编辑:程序博客网 时间:2024/06/16 11:09
三种思路,一种是利用error.StackTrace,第二种是try-catch找到错误行数, 第三种是: System.Diagnostics.Debug.WriteLine() + DebugView工具
三. System.Diagnostics.Debug.WriteLine() + DebugView工具
参考文章:
一、error.StackTrace代码
ex.StackTrace.Substring(ex.StackTrace.IndexOf("行号"), ex.StackTrace.Length - ex.StackTrace.IndexOf("行号"))
二、try-catch代码
try{ //////////////// 代码断 ////////////////}catch(Exception ex){ MessageBox.Show(ex.StackTrace);}
三. System.Diagnostics.Debug.WriteLine() + DebugView工具
1.引用
using System.Diagnostics;
2.显示在DebugView的信息
Debug.WriteLine(DateTime.Now.ToString("HH-mm-ss")+" "+DateTime.Now.Millisecond.ToString() + " cti_message", "my");
3.在Dbgview.exe 过滤其它信息
Edit -> Filter/Hightlight... -> include: 中输入 *my
点击OK后,便可用DebugView调试C#程序了。
MSDN StackTrace示例
下面的代码示例引发一个 Exception,然后捕捉该异常,并使用 StackTrace 属性显示堆栈跟踪。
// Example for the Exception.HelpLink, Exception.Source,// Exception.StackTrace, and Exception.TargetSite properties.using System;namespace NDP_UE_CS{ // Derive an exception; the constructor sets the HelpLink and // Source properties. class LogTableOverflowException : Exception { const string overflowMessage = "The log table has overflowed."; public LogTableOverflowException( string auxMessage, Exception inner ) : base( String.Format( "{0} - {1}", overflowMessage, auxMessage ), inner ) { this.HelpLink = "http://msdn.microsoft.com"; this.Source = "Exception_Class_Samples"; } } class LogTable { public LogTable( int numElements ) { logArea = new string[ numElements ]; elemInUse = 0; } protected string[ ] logArea; protected int elemInUse; // The AddRecord method throws a derived exception if // the array bounds exception is caught. public int AddRecord( string newRecord ) { try { logArea[ elemInUse ] = newRecord; return elemInUse++; } catch( Exception e ) { throw new LogTableOverflowException( String.Format( "Record \"{0}\" was not logged.", newRecord ), e ); } } } class OverflowDemo { // Create a log table and force an overflow. public static void Main() { LogTable log = new LogTable( 4 ); Console.WriteLine( "This example of \n Exception.Message, \n" + " Exception.HelpLink, \n Exception.Source, \n" + " Exception.StackTrace, and \n Exception." + "TargetSite \ngenerates the following output." ); try { for( int count = 1; ; count++ ) { log.AddRecord( String.Format( "Log record number {0}", count ) ); } } catch( Exception ex ) { Console.WriteLine( "\nMessage ---\n{0}", ex.Message ); Console.WriteLine( "\nHelpLink ---\n{0}", ex.HelpLink ); Console.WriteLine( "\nSource ---\n{0}", ex.Source ); Console.WriteLine( "\nStackTrace ---\n{0}", ex.StackTrace ); Console.WriteLine( "\nTargetSite ---\n{0}", ex.TargetSite ); } } }}/*This example of Exception.Message, Exception.HelpLink, Exception.Source, Exception.StackTrace, and Exception.TargetSitegenerates the following output.Message ---The log table has overflowed. - Record "Log record number 5" was not logged.HelpLink ---http://msdn.microsoft.comSource ---Exception_Class_SamplesStackTrace --- at NDP_UE_CS.LogTable.AddRecord(String newRecord) at NDP_UE_CS.OverflowDemo.Main()TargetSite ---Int32 AddRecord(System.String)*/
参考文章:
百度知道:C# 如何获取错误所在行数
http://blog.csdn.net/gaoxiang19820514/article/details/6649907
https://msdn.microsoft.com/zh-cn/library/system.exception.stacktrace(VS.80).aspx
0 0
- C# 如何获取错误所在行数
- C# 如何获取错误所在行数
- jquery获取当前对象所在的行数
- C#中如何获取应用程序运行所在的目录
- C#获取应用程序所在路径
- QTableWidget获取选中的内容及所在行数
- QTableWidget获取选中的内容及所在行数
- 如何获取ResultSet记录行数
- PHP如何获取文件行数
- php如何获取文件行数
- C#如何统计代码行数
- C#里面如何利用流获取一个记事本内容的行数
- C#获取当前应用程序所在的路径
- c#获取当前应用程序所在路径
- C#获取当前应用程序所在的路径
- C#获取当前应用程序所在的路径
- c#获取当前应用程序所在路径
- c#获取当前应用程序所在路径
- 进阶
- cudnn的安装,路径和版本问题
- JAVA常用的定时任务方法
- ORACLE SGA区基础知识与查看命令
- 大数取余
- C# 如何获取错误所在行数
- 均值滤波的快速解法
- 用VS2015编译MPC-HC
- Android学习笔记44:JSON数据解析
- Lightoj1203 Guarding Bananas(凸包)
- cube-sdk
- eclipse Android开发百度地图只显示网格问题
- Flume学习笔记之初识(一)
- Android学习笔记45:JSON数据解析(GSON方式)