Allow ExitCodeGenerator to be used on Exceptions

Update exit code support to allow the ExitCodeGenerator interface to
be placed on an Exception. Any uncaught exception implementing the
interface and returning a non `0` status will now trigger a System.exit
with the code.

Fixes gh-4803
This commit is contained in:
Phillip Webb
2016-01-13 11:56:24 +00:00
parent d2fed8bb07
commit 7397dbaf57
10 changed files with 186 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.boot.loader;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Method;
/**
@@ -53,7 +54,14 @@ public class MainMethodRunner implements Runnable {
mainMethod.invoke(null, new Object[] { this.args });
}
catch (Exception ex) {
ex.printStackTrace();
UncaughtExceptionHandler handler = Thread.currentThread()
.getUncaughtExceptionHandler();
if (handler != null) {
handler.uncaughtException(Thread.currentThread(), ex);
}
else {
ex.printStackTrace();
}
System.exit(1);
}
}