Created
September 16, 2019 09:38
-
-
Save tmanOC/da327617c8d0db834276fd5ad8b2773d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional; | |
public class ExceptionUtil { | |
public interface MayThrow<R>{ | |
public R method() throws Exception; | |
} | |
public static <T> T tryBlock(MayThrow<T> throwing) { | |
try{ | |
T obj = throwing.method(); | |
return obj; | |
} catch(Exception e) { | |
System.out.println(e.getLocalizedMessage()); | |
return null; | |
} | |
} | |
public static <T> Optional<T> tryBlockOptional(MayThrow<T> throwing) { | |
try{ | |
return Optional.ofNullable(throwing.method()); | |
} catch(Exception e) { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment