Java 在main中引发的异常没有任何错误

Java 在main中引发的异常没有任何错误,java,spring,spring-boot,exception-handling,exit-code,Java,Spring,Spring Boot,Exception Handling,Exit Code,我在IntelliJ中有一个Spring引导应用程序,最初配置为正常启动,如下所示: public static void main(String[] args) throws Exception { SpringApplication.run(AccountApiApplication.class, args).close(); } 我在main方法中添加了一个try-catch块,用于处理启动过程中出现的任何错误(如缺少配置文件等),现在看起来如下所示: public static

我在IntelliJ中有一个Spring引导应用程序,最初配置为正常启动,如下所示:

public static void main(String[] args) throws Exception {
    SpringApplication.run(AccountApiApplication.class, args).close();
}
我在
main
方法中添加了一个try-catch块,用于处理启动过程中出现的任何错误(如缺少配置文件等),现在看起来如下所示:

public static void main(String[] args) throws Exception {
        try {
            SpringApplication.run(AccountApiApplication.class, args).close();
        }
        catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
添加此项后,我的应用程序总是以1的退出代码退出。即使没有错误。我尝试打印正在发生的异常,结果如下:

org.springframework.boot.devtools.restart.SilentExitExceptionHandler$SilentExitException
    at org.springframework.boot.devtools.restart.SilentExitExceptionHandler.exitCurrentThread(SilentExitExceptionHandler.java:90)
    at org.springframework.boot.devtools.restart.Restarter.immediateRestart(Restarter.java:184)
    at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:163)
    at org.springframework.boot.devtools.restart.Restarter.initialize(Restarter.java:552)
    at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationStartingEvent(RestartApplicationListener.java:67)
    at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:45)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at edu.iu.es.ebs.AccountApiApplication.main(AccountApiApplication.java:90)

Process finished with exit code 1

为什么即使应用程序中没有错误,我也会看到此异常?当我们在做这件事时,有没有比在
main
中添加try-catch更好的方法来处理不可预测的启动错误?

首先,我认为问题在于调用
close()
。该方法可以生成一个
silenexitException
,被
silenexitExceptionHandlerExample
吞没,正如您在Stacktrace中看到的那样


其次,最好使用接口
FailureAnalizer
在应用程序启动时捕获错误,它可以为您提供有关错误的更多信息。

首先,我认为问题在于调用
close()
。该方法可以生成一个
silenexitException
,被
silenexitExceptionHandlerExample
吞没,正如您在Stacktrace中看到的那样


其次,最好使用接口
FailureAnalizer
来捕获应用程序启动时的错误,它可以为您提供有关错误的更多信息。

这是一种正常的工作方式,Spring Boot应用程序的依赖项中包含
org.springframework.Boot:Spring Boot devtools
。默认情况下,
org.springframework.boot.devtools.restart.Restarter
在初始化后立即重新启动应用程序。要摆脱
silenexitexception
您应该通过将属性
spring.devtools.restart.enabled
设置为
false
来摆脱restarter,但我不认为这个异常是一个大问题。

这是一种正常的方式来处理已经启动的spring启动应用程序
org.springframework.boot:springboot devtools
在其依赖项中。默认情况下,
org.springframework.boot.devtools.restart.Restarter
在初始化后立即重新启动应用程序。要摆脱
silenexitexception
您应该通过将属性
spring.devtools.restart.enabled
设置为
false
来摆脱restarter,但我不认为这个异常是个大问题。

为什么要用这个
.close()
?@pvpkiran关闭应用程序,因为如果我不这样做,它不会退出。我的意思是,它不会以“退出代码为0的流程结束”或“退出代码为1的流程结束”之类的内容结束。它以“启动应用程序…”结束。我最终将通过shell脚本调用应用程序,因此我需要它结束并向脚本返回退出代码。@lebowski这是另一个问题。如果您检查springboot应用程序生命周期(),您可以看到,当应用程序准备好工作时,应用程序将处于“startedapplication”状态是正常的。如果您需要“最终”执行之类的操作,您应该检查
计划
功能为什么要使用此
.close()
?@pvpkiran关闭应用程序,因为如果我不关闭,它不会退出。我的意思是,它不会以“退出代码为0的流程结束”或“退出代码为1的流程结束”之类的内容结束。它以“启动应用程序…”结束。我最终将通过shell脚本调用应用程序,因此我需要它结束并向脚本返回退出代码。@lebowski这是另一个问题。如果您检查springboot应用程序生命周期(),您可以看到,当应用程序准备好工作时,应用程序将处于“startedapplication”状态是正常的。如果您需要“最终”执行之类的操作,您应该检查
计划
功能。我尝试删除
close()
方法,但仍然出现该异常。我尝试删除
close()
方法,但仍然出现该异常。