Disclaimer This question is about, If and How we can use the
Parse
method exceptions, to find which argument/variable had actually failed the conversion. It is NOT, to argue if this is best or the correct way neither to find alternatives such asTryParse
or subsequentIf
statements. However, each and every alternative or workaround is appreciated.
I have the following code-block in C#
string a = "1"
string b = "b"
string c = "3"
// add many more similar
try
{
int a1 = int.Parse(a);
int b1 = int.Parse(b);
int c1 = int.Parse(c);
}
catch (Exception e)
{
// Here I will do some logging
}
Here the int b1 = int.Parse(b);
line will fail and catch block will catch the exception.
Is it possible by examining, programmatically, the Exception
info, to find the actual value (the value of string b
, not the name of the variable) that caused the error. For example I would like to get something like:
Could not parse "b"