Skip to content

Commit b796447

Browse files
committed
Merge pull request #39107 from
* pr/39107: Polish 'Remove unreachable throw code' Remove unreachable throw code Closes gh-39107
2 parents dee709e + e23e431 commit b796447

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import org.springframework.util.ClassUtils;
9696
import org.springframework.util.CollectionUtils;
9797
import org.springframework.util.ObjectUtils;
98-
import org.springframework.util.ReflectionUtils;
9998
import org.springframework.util.StringUtils;
10099
import org.springframework.util.function.ThrowingConsumer;
101100
import org.springframework.util.function.ThrowingSupplier;
@@ -172,6 +171,7 @@
172171
* @author Ethan Rubinson
173172
* @author Chris Bono
174173
* @author Tadaya Tsuyukubo
174+
* @author Lasse Wulff
175175
* @since 1.0.0
176176
* @see #run(Class, String[])
177177
* @see #run(Class[], String[])
@@ -331,11 +331,7 @@ public ConfigurableApplicationContext run(String... args) {
331331
callRunners(context, applicationArguments);
332332
}
333333
catch (Throwable ex) {
334-
if (ex instanceof AbandonedRunException) {
335-
throw ex;
336-
}
337-
handleRunFailure(context, ex, listeners);
338-
throw new IllegalStateException(ex);
334+
throw handleRunFailure(context, ex, listeners);
339335
}
340336
try {
341337
if (context.isRunning()) {
@@ -344,11 +340,7 @@ public ConfigurableApplicationContext run(String... args) {
344340
}
345341
}
346342
catch (Throwable ex) {
347-
if (ex instanceof AbandonedRunException) {
348-
throw ex;
349-
}
350-
handleRunFailure(context, ex, null);
351-
throw new IllegalStateException(ex);
343+
throw handleRunFailure(context, ex, null);
352344
}
353345
return context;
354346
}
@@ -790,8 +782,11 @@ private <R extends Runner> void callRunner(Class<R> type, Runner runner, Throwin
790782
.accept((R) runner);
791783
}
792784

793-
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
785+
private RuntimeException handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
794786
SpringApplicationRunListeners listeners) {
787+
if (exception instanceof AbandonedRunException abandonedRunException) {
788+
return abandonedRunException;
789+
}
795790
try {
796791
try {
797792
handleExitCode(context, exception);
@@ -810,7 +805,8 @@ private void handleRunFailure(ConfigurableApplicationContext context, Throwable
810805
catch (Exception ex) {
811806
logger.warn("Unable to close ApplicationContext", ex);
812807
}
813-
ReflectionUtils.rethrowRuntimeException(exception);
808+
return (exception instanceof RuntimeException runtimeException) ? runtimeException
809+
: new IllegalStateException(exception);
814810
}
815811

816812
private Collection<SpringBootExceptionReporter> getExceptionReporters(ConfigurableApplicationContext context) {

0 commit comments

Comments
 (0)