Niklas
Niklas

Reputation: 1000

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 :

 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); fails,the IDE directs me to the method 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 !

Upvotes: 0

Views: 49

Answers (2)

Developer
Developer

Reputation: 4321

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

Upvotes: 1

Adeeb Arangodan
Adeeb Arangodan

Reputation: 152

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.

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

Upvotes: 1

Related Questions