views:

90

answers:

2

Jetty can be used as a library to embed a servlet-server into your application. To do that, you create an instance of the class Server and call start at some point. This method throws Exception. Catching or throwing a pure Exception (not a specialized subclass) is bad style. Does anyone know, how I can avoid this and get a Jetty-server in my application running without handling this general Exception?

A: 

I would strongly advise you to handle it somehow.

If Jetty can't start (e.g. bind to its nominated port) then you're going to want to manage that somehow. At the very least log this so you know you don't have a web server running. Perhaps retry after a while ?

The point is that Jetty is unable to start and you should care about that.

Brian Agnew
+4  A: 

Catching Exception is bad practice unless only Exception is thrown. There is no workaround for it. Catching distinct subclasses of Exception has the disadvantage of possibly missing out on some of them.


What is the meaning of Jetty failing to start in your application? You can have multiple approaches:

Decide at the component level that you should proceeed

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

Treat it as a fatal exception

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

Treat it as a recoverable exception

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