[英]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.