}
// the goal is to find the exception with the same/closet inheritance level as the target exception being thrown
int targetLevel = getInheritanceLevel(exception.getClass());
// candidate is the best candidate found so far to return
ExceptionType candidate = null;
// difference in inheritance level between the current candidate and the thrown exception (target level)
int candidateDiff = Integer.MAX_VALUE;
// loop through all the entries and find the best candidates to use
Set<Map.Entry<ExceptionPolicyKey, ExceptionType>> entries = exceptionPolicices.entrySet();
for (Map.Entry<ExceptionPolicyKey, ExceptionType> entry : entries) {
Class clazz = entry.getKey().getExceptionClass();
ExceptionType type = entry.getValue();
if (filter(type, clazz, exception)) {
// must match
if (!matchesWhen(type, exchange)) {