I noticed an issue that an exception in my static block was not captured by a try-catch in the main() method. Do I miss anything? Below is my code:
public class MyService {
......
public static void main(String[] args) throws Exception {
try {
MyService.getInstance().run(args);
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
public class MyBo {
static {
aMethod();
}
......
private static void aMethod() {
......
throw new RuntimeException("blar");
}
}
When I run above code and the throw step got hit, the application will exit with -1 but the error is not captured and no stack trace printed out.
I know I can add a try catch at the static block and print it, but just don't know why the try catch at very high level (in the main class) didn't capture it. Can anyone explain it? Thanks in advance.