Skip to content

Commit a4ae660

Browse files
BenchmarkingBuffalophilwebb
authored andcommitted
Remove unreachable throw code
Improve `SpringApplication` by removing the unreachable throw statement in favor of returning an exception from `handleRunFailure`. This commit also removes the if statements in favor of dedicated catch blocks. See gh-39107
1 parent dee709e commit a4ae660

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

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

Lines changed: 14 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[])
@@ -330,25 +330,23 @@ public ConfigurableApplicationContext run(String... args) {
330330
listeners.started(context, timeTakenToStartup);
331331
callRunners(context, applicationArguments);
332332
}
333+
catch (AbandonedRunException ex) {
334+
throw ex;
335+
}
333336
catch (Throwable ex) {
334-
if (ex instanceof AbandonedRunException) {
335-
throw ex;
336-
}
337-
handleRunFailure(context, ex, listeners);
338-
throw new IllegalStateException(ex);
337+
throw handleRunFailure(context, ex, listeners);
339338
}
340339
try {
341340
if (context.isRunning()) {
342341
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
343342
listeners.ready(context, timeTakenToReady);
344343
}
345344
}
345+
catch (AbandonedRunException ex) {
346+
throw ex;
347+
}
346348
catch (Throwable ex) {
347-
if (ex instanceof AbandonedRunException) {
348-
throw ex;
349-
}
350-
handleRunFailure(context, ex, null);
351-
throw new IllegalStateException(ex);
349+
throw handleRunFailure(context, ex, null);
352350
}
353351
return context;
354352
}
@@ -790,7 +788,7 @@ private <R extends Runner> void callRunner(Class<R> type, Runner runner, Throwin
790788
.accept((R) runner);
791789
}
792790

793-
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
791+
private RuntimeException handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
794792
SpringApplicationRunListeners listeners) {
795793
try {
796794
try {
@@ -810,7 +808,10 @@ private void handleRunFailure(ConfigurableApplicationContext context, Throwable
810808
catch (Exception ex) {
811809
logger.warn("Unable to close ApplicationContext", ex);
812810
}
813-
ReflectionUtils.rethrowRuntimeException(exception);
811+
if (exception instanceof RuntimeException runtimeException) {
812+
return runtimeException;
813+
}
814+
return new IllegalStateException(exception);
814815
}
815816

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

0 commit comments

Comments
 (0)