首页 > 编程语言 >C# 如何获取错误所在行数

C# 如何获取错误所在行数

时间:2023-02-09 10:12:07浏览次数:49  
标签:StackTrace Exception HelpLink C# 所在 --- 获取 ex WriteLine

三种思路,一种是利用error.StackTrace,第二种是try-catch找到错误行数, 第三种是:  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.TargetSite
generates the following output.
Message ---
The log table has overflowed. - Record "Log record number 5" was not logged.
HelpLink ---
http://msdn.microsoft.com
Source ---
Exception_Class_Samples
StackTrace ---
   at NDP_UE_CS.LogTable.AddRecord(String newRecord)
   at NDP_UE_CS.OverflowDemo.Main()
TargetSite ---
Int32 AddRecord(System.String)
*/

 

转载自:

C# 如何获取错误所在行数

参考文档:

Exception.StackTrace 属性 | 微软

标签:StackTrace,Exception,HelpLink,C#,所在,---,获取,ex,WriteLine
From: https://www.cnblogs.com/Yan3399/p/17104257.html

相关文章

  • 2023 年 2 月国内使用ChatGPT的方式汇总,亲测可用
    刚过完年,不知道大家有没有在春节档期间,跟家人一起观看《流浪地球2》,里面的MOSS,是一款强大的智能机器人。那么当时我们就可以联想到的最近发布的ChatGPT,同样作为一款现实中......
  • centos7 安装docker
    1、卸载老版本docker(可选)sudoyumremovedocker\docker-client\docker-client-latest\docker-common......
  • 编程口通讯协议下,三菱FX2N与MCGS能否建立无线通讯?
    三菱PLC的位元件主要有X、Y、M、S、T、C,字元件有D。编程口通信协议采用异步格式,由1位起始位、7位数据位、1位偶校验位及1位停止位组成,字符为ASCII码形式。昆仑通态可使用三......
  • QT comboBox简单使用
    拖一个控件双击然后就点加号生成下拉菜单connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(deal(int)));这样就可以接收你选择的菜单信号了你可以把初始......
  • C++变参模板简单使用
    为什么简单使用呢因为目前没遇到实际应用的地方就大概学一下吧template<typenameT,typename...A>voidprint(Tt,A...a){std::cout<<t;print(a...);//这个函数是通用......
  • C# ref out两个关键字学习一下
    ref这个名字和C++的引用应该是一样的噢。功能也是传指针,好东西。out这个关键字我第一次见是在glsl里头,你给个变量,在函数执行过程赋值,保存出来,或者在着色器每个阶段结束的时......
  • C# List 学一下
    Add可以直接添加一个元素AddRange添加一个集合在尾部Insert(i,ele)可以插入一个元素InsertRange(i,list2)插入一个listCount是数量Contains是是否包含Remove单个元素Remove......
  • eduSrc域名收集脚本
    eduSrc主域名查找脚本二改版本,原作者项目地址:https://github.com/Ernket/edu-crawler原作者的可能下载下来不能直接用,这个则可以。刚改完,热乎的哈哈哈效果,可以直接拿来......
  • 编程口通讯协议下,三菱FX2N与MCGS能否建立无线通讯?
    三菱PLC的位元件主要有X、Y、M、S、T、C,字元件有D。编程口通信协议采用异步格式,由1位起始位、7位数据位、1位偶校验位及1位停止位组成,字符为ASCII码形式。昆仑通态可使用三......
  • color bass推荐
    au5......