I have two RuntimeExceptions as you can see below:
@Getter
@RequiredArgsConstructor
public class MyCustomRuntimeException1 extends RuntimeException {
private final String message;
private final int code;
}
@Getter
@RequiredArgsConstructor
public class MyCustomRuntimeException2 extends RuntimeException {
private final String message;
private final int code;
}
Then I have my doSomethingMethod where I catch them in the same catch clause:
public void doSomething() {
[....]
[....]
try {
[....]
[....]
} catch (MyCustomRuntimeException1 | MyCustomRuntimeException2 ex) {
log.error("!! doSomething: An error occurred", ex);
var code = ex.getCode();
var message = ex.getMessage();
[......]
[......]
}
}
I'm receiving a compile-time error that says Cannot resolve method 'getCode' in 'RuntimeException' on the following line:
var code = ex.getCode();
On the other hand, this snippet code works properly:
public void doSomething() {
[....]
[....]
try {
[....]
[....]
} catch (MyCustomRuntimeException1 ex) {
log.error("!! doSomething: An error occurred", ex);
var code = ex.getCode();
var message = ex.getMessage();
[......]
[......]
} catch (MyCustomRuntimeException2 ex) {
log.error("!! doSomething: An error occurred", ex);
var code = ex.getCode();
var message = ex.getMessage();
[......]
[......]
}
}