This is the piece of code.
private boolean CheckTerm()
{
String str = lGskCompoundNumber;
if (lPrefix.trim() == "" || lNumber.trim() == "")
return false;
try
{
Integer.parseInt(lNumber);
}
catch (Exception ex)
{
return false;
}
if (lHasAmpersand)
str = lGskCompoundNumber.replace("&", "");
return str.equals(lPrefix + lInitSep + lNumber + lEndSep + lSuffix);
}
Should I return a certain value from catch block or is the usage right?
This code is correct and does not look suspicious. When parsing fails (note that you should catch the most narrow exception possible, NumberFormatException
in this case), whole validation failed, so you are returning false
. Otherwise you are performing additional checks (after catch
block) and returning their result. This code is fine.
If you find it a bit hard to read, consider the following refactoring:
private boolean checkTerm() {
try
{
String str = lGskCompoundNumber;
if (lPrefix.trim() == "" || lNumber.trim() == "")
return false;
Integer.parseInt(lNumber);
if (lHasAmpersand)
str = lGskCompoundNumber.replace("&", "");
return str.equals(lPrefix + lInitSep + lNumber + lEndSep + lSuffix);
}
catch (NumberFormatException ex)
{
return false;
}
}
There's nothing wrong in your code but I always prefer to have a single exit point in my methods, so I prefer to write something like:
private boolean CheckTerm()
{
boolean res = false;
String str = lGskCompoundNumber;
if (lPrefix.trim() == "" || lNumber.trim() == "")
{
}
else
{
try
{
Integer.parseInt(lNumber);
if (lHasAmpersand)
str = lGskCompoundNumber.replace("&", "");
res = str.equals(lPrefix + lInitSep + lNumber + lEndSep + lSuffix);
}
catch (NumberFormatException ex)
{
}
}
return res;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With