Reputation: 51
I am executing a stored procedure and returning a string. The string is set to return 1
,0
or "USER DOES NOT EXISTS"
depending on the conditions.
Just wanted to know if the following is a bad programming practice.
string result = _db.GetParameterValue(cmdObj, "@strMessage").ToString();
try
{
int a = int.Parse(result);
if (a == 1)
Console.WriteLine("A");
else
Console.WriteLine("B");
}
catch
{
Console.WriteLine(result);
}
Console.WriteLine(result);
Upvotes: 3
Views: 632
Reputation: 13185
It is always better to specifically match rather than presume it was "USER NOT EXISTS" based on catching a failed int parse.
It is always bad practice to try/catch/swallow. If you're going to catch an exception, log it or throw.
You've not specified a language, so presuming it's C#, int.TryParse()
is much cleaner than int.Parse
inside a try/catch
.
Upvotes: 8
Reputation: 148524
you should use tryParse
and not to include it in try catch block.
int outValue = -1;
int.TryParse(result, out outValue);
Upvotes: 4