public static < T extends Throwable > Optional < T > findThrowable ( Throwable throwable , Class < T > searchType ) { if ( throwable == null || searchType == null ) { return Optional . empty ( ) ; } Throwable t = throwable ; while ( t != null ) { if ( searchType . isAssignableFrom ( t . getClass ( ) ) ) { return Optional . of ( searchType . cast ( t ) ) ; } else { t = t . getCause ( ) ; } } return Optional . empty ( ) ; }
Checks whether a throwable chain contains a specific type of exception and returns it .