Handle Edge Cases
By the end of this lesson, you'll be able to:
- Guard getEmployee against a missing ID returning null unexpectedly
- Guard addEmployee against a null Employee argument
- Distinguish "not found" from "invalid input" as two different edge cases
Prerequisites: "Generate Reports from Collections"
Handling a missing employee ID
public Employee getEmployee(String employeeId) {
if (! employeesById.containsKey(employeeId)) {
System.debug('No employee found with ID ' + employeeId);
return null;
}
return employeesById.get(employeeId);
}
public Employee getEmployee(String employeeId) {— the lookup method.if (! employeesById.containsKey(employeeId)) {— checks explicitly before looking up.System.debug('No employee found with ID ' + employeeId);— without this check, callinggetEmployee('E999')for a nonexistent ID would already returnnullsilently (recall Module 16's "Maps" lesson) — this logs the reason clearly instead.return null;— still returnsnull— "not found" is a normal, expected outcome.}— closes theif.return employeesById.get(employeeId);— the normal, found case.}— closes the method.
Guarding against a null Employee
public Boolean addEmployee(Employee emp) {
if (emp == null) {
System.debug('Cannot add a null employee.');
return false;
}
if (employeesById.containsKey(emp.getEmployeeId())) {
return false;
}
employeesById.put(emp.getEmployeeId(), emp);
return true;
}
public Boolean addEmployee(Employee emp) {— the method, now with a null guard added first.if (emp == null) {— checks fornullbefore anything else touchesemp.System.debug('Cannot add a null employee.');— a clear reason logged.return false;— rejects cleanly. Without this guard,emp.getEmployeeId()further down would throw aNullPointerExceptionon anullemp— exactly the failure mode from Module 13's "Common Built-In Exceptions" lesson.}— closes theif.if (employeesById.containsKey(emp.getEmployeeId())) {— safe now, sinceempis guaranteed non-null.return false;— rejects a duplicate.}— closes theif.employeesById.put(emp.getEmployeeId(), emp);— only reached if every check passed.return true;— reports success.}— closes the method.
Two different kinds of edge case
- "Not found" (a valid lookup that simply has no match) is an expected outcome — a normal part of using the directory. Returning
nullwith a clear debug message is enough. - "Invalid input" (a
nullEmployee, or one with a blank employee ID) is a programming mistake on the caller's part — still handled without crashing, but worth treating as a more serious signal that something upstream is wrong.
Both get handled gracefully here, but recognizing the difference helps decide, in later modules, when a situation is serious enough to warrant a custom exception (Module 13) instead of just a false return value.
Exercise
Add a validation check to addEmployee rejecting an Employee whose getEmployeeId() is null or an empty String.
Show hint
String.isBlank(emp.getEmployeeId())
Handle Edge Cases Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson hardens the directory against two distinct edge cases — a lookup for an ID that doesn't exist, and an attempt to add a null Employee — following the same guard-clause discipline as Module 12.