【问题标题】:How can I avoid the general Exception thrown by the Server-class in Jetty?如何避免 Jetty 中的服务器类抛出的一般异常?
【发布时间】:2009-07-07 12:06:05
【问题描述】:

Jetty 可以用作库来将 servlet-server 嵌入到您的应用程序中。为此,您需要创建一个 Server 类的实例并在某个时间点调用 start。此方法抛出异常。捕获或抛出纯异常(不是专门的子类)是不好的风格。有谁知道,我如何避免这种情况并在我的应用程序中运行一个 Jetty 服务器而不处理这个一般异常?

【问题讨论】:

    标签: java exception jetty embedded-jetty


    【解决方案1】:

    捕捉Exception 是不好的做法除非只抛出Exception。没有解决方法。捕获 Exception 的不同子类的缺点是可能会错过其中的一些。


    Jetty 无法在您的应用程序中启动是什么意思?您可以有多种方法:

    在组件级别决定您应该继续进行

    try { 
        server.start();
        reportingAvailable = true;
    } catch ( Exception e ) { 
        log("Jetty failed to start. Reporting will we unavailable", e);
    }
    

    将其视为致命异常

    try { 
        server.start();
    } catch ( Exception e ) { 
        throw new RuntimeException("Jetty failed to start", e);
    }
    

    将其视为可恢复的异常

    try { 
        server.start();
    } catch ( Exception e ) { 
        throw new JettyFailedToStartException(e); // JettyFailedToStartException !instanceof RuntimeException
    }
    

    【讨论】:

      【解决方案2】:

      我会强烈建议你以某种方式处理它。

      如果 Jetty 无法启动(例如绑定到其指定端口),那么您将需要以某种方式管理它。至少记录一下,这样你就知道你没有运行 Web 服务器。也许过一会再试?

      关键是 Jetty 无法启动,你应该关心它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多