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
protected Throwable(String message, | |
Throwable cause, | |
boolean enableSuppression, | |
boolean writableStackTrace) { | |
if (writableStackTrace) { | |
fillInStackTrace(); | |
} | |
else { | |
stackTrace = null; |
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
public float divide(float divident, float divisor) { | |
try { | |
return divident / divisor | |
} | |
catch (ArithmeticException e) { | |
System.out.println("Division error"); | |
} | |
return 0.0f; | |
} |
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
public void doWork(List<MyBean> beans) { | |
beans.stream() | |
.map(this::mapToItem) | |
.forEach(this::workOnItem); | |
} | |
private MyItem mapToItem(MyBean bean) { | |
try { | |
MyItem item = ...; | |
return item; |
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
@FunctionalInterface | |
interface ExFunction<T, R, E extends Exception> { | |
R apply(T t) throws E; | |
} | |
<T, R, E extends Exception> Function<T, R> uncheck(ExFunction<T, R, E> fn) { | |
return arg -> { | |
try { | |
return fn.apply(arg); |
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
void doWork(List<MyBean> beans) throws ValidationException, IOException { | |
beans.stream() | |
.map(bean -> ...) | |
.forEach(item -> ...); | |
} |
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
void doWork(List<MyBean> beans) { | |
beans.stream() | |
.map(bean -> { | |
try { | |
// do some work that might throw | |
} | |
catch (ValidationException e) { | |
// handle appropriately | |
} | |
}) |
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
class CustomerRegistrationException extends Exception { | |
private final String email; | |
public CustomerRegistrationException(String email) { | |
super("Registration failed for " + email); | |
this.email = email; | |
} | |
public String getEmail() { |
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
public class ValidationException extends Exception { | |
private final String field; | |
public ValidationException(String field, | |
String message, | |
Throwable cause) { | |
super(message, cause); | |
this.field = field; | |
} |
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
try { | |
... | |
} | |
catch (IOException e) { | |
throw new MyCustomException("More information", e); | |
} |
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
interface WithException { | |
void doWork() throws IOException; | |
} | |
class WithoutException implements WithException { | |
// We omit the exception in the implementation | |
@Override | |
public void doWork() { |