@ SuppressWarnings ( "PMD.AvoidThrowingRawExceptionTypes" ) private T fallback ( final Throwable exp ) throws Exception { final Sorted < Map . Entry < FallbackFrom < T > , Integer > > candidates = new Sorted <> ( Comparator . comparing ( Map . Entry :: getValue ) , new Filtered <> ( entry -> new Not ( new Equals <> ( entry :: getValue , ( ) -> Integer . MIN_VALUE ) ) . value ( ) , new MapOf <> ( fbk -> fbk , fbk -> fbk . support ( exp . getClass ( ) ) , this . fallbacks ) . entrySet ( ) . iterator ( ) ) ) ; if ( candidates . hasNext ( ) ) { return candidates . next ( ) . getKey ( ) . apply ( exp ) ; } else { throw new Exception ( "No fallback found - throw the original exception" , exp ) ; } }
Finds the best fallback for the given exception type and apply it to the exception or throw the original error if no fallback found .