0

Is it better using exception or flow control while testing for argument inserted by end-user? pseudocode:

class A{
List listOfPeople;
public void insertName(String name)
{
//if the name is not inserted in the registry it throws a runtime exception
throw new IllegalArgumentException("Please insert a registered name");
}
}

or maybe it's better doing the following:

class A{
List listOfPeople;
public void insertName(String name)
{
if (!listOfPeople.contains(name){
System.out.println("name not in the list, please insert a registered name");
reInsertName();
}
else
..do stuff..
}
}

I have read that throwing exceptions is very expensive.. does it mean that whenever it's possible to not throw them we have to avoid throwing them trying to flow-control the issue?

Thanks, Indeed ItIs

Jonas
  • 121,568
  • 97
  • 310
  • 388
Indeed ItIs
  • 257
  • 3
  • 10

2 Answers2

0

but in your second implementation you have to handle error case in same class, i mean a seperated error handler module is a better approach, seperating aspects and applying single responsibility for classes is a better way of implementation in my opinion

nik
  • 241
  • 2
  • 6
  • Would it cover the expenses of using exception the gain in cohesion by having single responsabilities? – Indeed ItIs Sep 29 '13 at 19:41
  • it depends on the size of the project, if it's a big and complex project than you have to think about updates also, how easily you can do updates and add new features. in small projects you may not apply that. – nik Sep 29 '13 at 19:48
  • I am going to use a state-testing method so I will separate responsabilities and not use expensive exceptions. As stated in Effective Java by J.B. – Indeed ItIs Sep 29 '13 at 20:09
0

IllegalArgumentException should be thrown when an API method (i.e. public or protected) receives arguments that violate its contract, e.g. your method insertName could throw one if the argument was null or empty String.

Such simple checks are placed at the beginning and the constraints of arguments should be documented. The exception is thrown to signal errors in the client code (code that uses the method).

In your case, the constraint on arguments are more on the business logic side, and they depend on the program state (that the name has been previously added to some list). Using IllegalArgumentException to indicate, that the program state is invalid doesn't seem right. If any, then IllegalStateException is much better here.

But I wouldn't design it that way whatsoever. How about a bit more OO:

public class A {

    List listOfPeople;

    public static class Registration {

        private final String name;            

        private Registration(String name) {
            this.name = name;
        }
        public void insert() {
            // ha, no need of exception throwing, because the person had to be registered first!
        }
    }

    public Registration register(String name) {
        listOfPeople.add(name);
        return new Registration(name);
    }
}
siledh
  • 3,268
  • 2
  • 16
  • 29