zoukankan      html  css  js  c++  java
  • C# 如何获取错误所在行数

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

    wangzhiqing999 
    推荐于2018-02-26

    catch (Exception ex)
    {
    Console.WriteLine(ex.StackTrace);
    }

    异常的 StackTrace 里面,可以看到错误发生的行号.
    不过好像仅仅对于 DEBUG 的有效。
    RELEASE 的,好像是看不到行号的。

    两种思路,一种是利用error.StackTrace,另外一种是try-catch找到错误行数,具体如下:

    一、error.StackTrace代码

    1

    ex.StackTrace.Substring(ex.StackTrace.IndexOf("行号"), ex.StackTrace.Length - ex.StackTrace.IndexOf("行号"))

    二、try-catch代码

    1

    2

    3

    4

    5

    6

    7

    8

    9

    try

    {

       ////////////////

       代码断

      ////////////////

    }catch(Exception ex)

    {

        MessageBox.Show(ex.StackTrace);

    }

  • 相关阅读:
    学习进度14
    学习进度13
    学习进度12
    2020.12.01 表单元素
    2020.12.02 表单属性
    2020.12.04
    2020.12.06
    2020.12.07
    2020.12.08 URL
    2020.12.09
  • 原文地址:https://www.cnblogs.com/grj001/p/12224624.html
Copyright © 2011-2022 走看看