5

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?

Reporter
  • 3,897
  • 5
  • 33
  • 47
dev
  • 132
  • 1
  • 3
  • 11

7 Answers7

10

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;
        }
    }
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
3

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;
}
davioooh
  • 23,742
  • 39
  • 159
  • 250
2

I believe that it is okay if you know what you want to return. If I could give a hint on the number of exits(returns) you use. I would use a boolean field and just return once after the try / catch Block

private boolean CheckTerm()
{
  boolean b = true;
  String str = lGskCompoundNumber;
      if (lPrefix.trim() == "" || lNumber.trim() == "")
          b = false;

      try
      {
          Integer.parseInt(lNumber);
      }
      catch (Exception ex)
      {
          b =  false;
      }
      if (lHasAmpersand)
          str = lGskCompoundNumber.replace("&", "");
      b = str.equals(lPrefix + lInitSep + lNumber + lEndSep + lSuffix);
      return b;
  }
Ben McDougall
  • 251
  • 2
  • 12
1

you can return some value in catch block, and even in finally block.

It's totally okay.

meadlai
  • 895
  • 1
  • 9
  • 22
1

If one of the conditions of CheckTerm is that lNumber should be an integer, then your code is ok, but if you're expecting lNumber to be an integer and it's not, you'll better throw an exception.

tibtof
  • 7,857
  • 1
  • 32
  • 49
1

Its fine as is though I would catch the exception differently.

That method your calling throws a NumberFormatException

it seems as though just returning false would be fine, i would do some sort of logging to keep track of the exception though discussion on how to handle your exception

Community
  • 1
  • 1
Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71
1

if you afraid there is no default returns, using finally, then return something.

try
    {
        Integer.parseInt(lNumber);
        if (lHasAmpersand)
            str = lGskCompoundNumber.replace("&", "");
        res = str.equals(lPrefix + lInitSep + lNumber + lEndSep + lSuffix);
    }
    catch (NumberFormatException ex)
    {
       return false;
    }finally{return false;}
hello you
  • 66
  • 3