3 ms·
Exceptions for null handling? I admit there's room for healthy debate about using exceptions for flow control in various situations, but for null handling it s
by dasil003 13y ago
Exceptions for null handling? I admit there's room for healthy debate about using exceptions for flow control in various situations, but for null handling it sounds like an absolute nightmare. Instead of considering null at each point it might occur in the code, you have to consider all the possible places a null value might be stacked above you.
- NateDad 13y agoExceptions for error handling... this is just error handling, that was my point. Returning a sentinel value (null) is just one (very suboptimal) way of handling an error. Instead of dataSource.getPersonById(id) returning null, it could throw a NotFoundException. This is better because it's a specific error (as opposed to a MalformedIdException, for example).
- Fistandantilus 13y agoExceptions can be used in strategic places to ensure that a value returned can never be null. Ex: public Person findPerson(String name) { for (Person p : personList) { if (p.getName().equals(name)) { return p; } } throw new PersonNotFoundException(name); } We can be sure that the return value of findPerson will never be null, as it must either return the correct value or throw an exception. Whether this is better, worse, or the same as a null check is debatable.