简体   繁体   English

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

[英]How can i find the Line that Contains the exception when Using methods that Parse the Data in C# WinForms

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 就像这种方法建议的那样,它可以让我做。对项目中的不同变量进行十进制运算,这是一种提高编码速度的方法

now here is my problem: whenever the parse of the decimal.parse(str); 现在这是我的问题:每当对十进制进行解析时,parse(str); fails,the IDE directs me to the method ToDecimal... 失败,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? 而不是调用该方法的实际行...并且让我在项目上停留了一天以找出真正的异常...所以我的问题是:有没有办法在解决方案中找到该行到底是造成异常的原因? ie 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 ! 提醒大家我在解决方案中已在1k次内调用了相同的方法(ToDecimal())...因此,我正在尝试找出那1000次中的哪一次是导致异常的原因...谢谢!

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语句中附加一个断点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#滚动到WinForms文本框中的指定行? - How can I scroll to a specified line in a WinForms TextBox using C#? 使用带有枚举项的c#winforms数据绑定时,如何在组合框中获得友好的文本描述? - How can I get friendly text descriptions in a combo box when using c# winforms data binding with enum items? 如何使用C#将网站中的数据检索到Winforms - How can I retrieve data from a website to my winforms using C# C#Winforms使用EF6时如何测试SQL连接? - C# Winforms how can I test for SQL connection when using EF6? 使用C#/ WPF(非WinForms!)时如何让显示器进入睡眠状态 - How can I put the monitor to sleep when using C#/WPF (Not WinForms!) 如何使用 WinForms c# 制作加速机制 - How can I make an acceleration mechanic using WinForms c# 使用C#Winforms从文本文件解析JSON数据 - To parse JSON data from text file using c# winforms 如何解析带有C#的CSV数据和逗号? - How can I parse data in a CSV with C# with commas in the data? 如何使用winforms在c#中的richtextbox中获取某行的字体大小 - how to get the fontsize of a certain line in a richtextbox in c# using winforms 我如何找到打开了当前打开的窗体的窗体? (C#,WinForms) - How can I find what Form opened the form currently open? (C#, WinForms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM