9595import org .springframework .util .ClassUtils ;
9696import org .springframework .util .CollectionUtils ;
9797import org .springframework .util .ObjectUtils ;
98- import org .springframework .util .ReflectionUtils ;
9998import org .springframework .util .StringUtils ;
10099import org .springframework .util .function .ThrowingConsumer ;
101100import org .springframework .util .function .ThrowingSupplier ;
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