Wednesday, 18 September 2013

No RuntimeException in stack trace when implementing HttpHandler.handle() in Java

No RuntimeException in stack trace when implementing HttpHandler.handle()
in Java

I am developing an application which has to serve some XML file through
HTTP. For the HTTP server implementaion I use the
com.sun.net.httpserver.HttpServer and I created a handler class which
implements HttpHandler. The very weird thing is that I cannot get any
RuntimeException from the MyHandler.handle() method. I put together a
sample code to reproduce the case I am struggling with:
Server code:
public class HttpServerTest {
public static void main(String[] args) {
HttpServer server;
try {
server = HttpServer.create(new InetSocketAddress(8080),0);
server.createContext("/", new MyHandler());
server.start();
} catch (IOException ex) {
Logger.getLogger(HttpServerTest.class.getName()).log(Level.SEVERE,
null, ex);
}
throw new RuntimeException("Very Important Exception from main");
}
}
And the handler:
class MyHandler implements HttpHandler {
public MyHandler() {
}
@Override
public void handle(HttpExchange he) throws IOException {
System.out.println("hello");
throw new RuntimeException("Very Important Exception from
MyHandler.handle");
}
}
Output:
Exception in thread "main" java.lang.RuntimeException: Very Important
Exception from main
at httpservertest.HttpServerTest.main(HttpServerTest.java:26)
hello
hello
hello
hello
hello
hello
So as you can see, I can get the exception from the main class main
method, but I do not get anything from the handler method. Has anyone seen
something like this before? Runtime exceptions should be in the stack
track trace always.
Thanks, Zoltan

No comments:

Post a Comment