Try to catch bad programming practice

I am executing a stored procedure and returning a string. The string is set to return 1, 0or "USER DOES NOT EXISTS"depending on the conditions.

Just wanted to know if the next one is 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);
+3
source share
2 answers

It is always better to specifically match, rather than assume that this is a "USER DOES NOT EXIST" based on the trap of a failed match.

It is always a bad practice to try / catch / learn. If you are going to catch an exception, write it down or throw it.

You did not specify a language, so suppose C # is int.TryParse()much cleaner than int.Parseinside a try/catch.

+8
source

tryParse not, try catch.

 int outValue = -1;
 int.TryParse(result, out outValue);
+4

All Articles