在使用在C#WinForms中解析数据的方法时,如何找到包含异常的Line

时间:2021-12-21 20:05:06

so this is how my trouble began...i made a cs file that contains all of my helper methods within my projects,its somewhat of a toolbox for me...one of the methods is the following :

所以这就是我的麻烦开始......我制作了一个cs文件,其中包含我项目中的所有帮助方法,它对我来说有点像工具箱......其中一种方法如下:

 static public decimal ToDecimal(this string str)
        {
            return decimal.Parse(str);
        }

as this method suggests,it lets me do .ToDecimal to different variables within my project,its a way of improving the speed while coding

正如这个方法所暗示的那样,它允许我对我项目中的不同变量执行.ToDecimal,它是一种在编码时提高速度的方法

now here is my problem: whenever the parse of the decimal.parse(str); fails,the IDE directs me to the method ToDecimal...

现在这里是我的问题:每当解析decimal.parse(str);失败,IDE指示我到方法ToDecimal ...

NOT to the actual line that calls the method...and that had me stuck for a day on a project to figure the real exception out... so my question is this : is there a way to find the line within the solution that is actually causing the exception? i.e the line that the exceptioned method was called at...

不是调用方法的实际行...并且我让我在一个项目上停留了一天才能找到真正的异常...所以我的问题是:有没有办法在解决方案中找到该行实际上是导致异常?即在...处调用异常方法的行

reminding you guys that i have called the same method (ToDecimal()) over 1k times within my solution... so im tryin to figure out Which of those 1k times is the one thats causing the exception... thank you !

提醒你们,我已经在我的解决方案中调用了相同的方法(ToDecimal())超过1k次...所以我试着弄清楚那些1k次中的哪一个是引起异常的那个......谢谢!

2 个解决方案

#1


try using this code

尝试使用此代码

static public decimal ToDecimal(this string str){
   decimal dec;
   if (decimal.TryParse(str, out dec))
   {
      return dec;
   }
   else
   {
      MessageBox.Show(str);
      return 0.0;
   }
}

Whenever parsing throw an exception, the if statement will fail and else part will give you the string which caused exception.

每当解析抛出异常时,if语句将失败,否则part将为您提供导致异常的字符串。

You can also attach a break point in the else statement.

您还可以在else语句中附加断点。

#2


Adding a try/catch and looking into the Call Stack is hard?

添加try / catch并查看调用堆栈很难吗?

#1


try using this code

尝试使用此代码

static public decimal ToDecimal(this string str){
   decimal dec;
   if (decimal.TryParse(str, out dec))
   {
      return dec;
   }
   else
   {
      MessageBox.Show(str);
      return 0.0;
   }
}

Whenever parsing throw an exception, the if statement will fail and else part will give you the string which caused exception.

每当解析抛出异常时,if语句将失败,否则part将为您提供导致异常的字符串。

You can also attach a break point in the else statement.

您还可以在else语句中附加断点。

#2


Adding a try/catch and looking into the Call Stack is hard?

添加try / catch并查看调用堆栈很难吗?