Skip to content
KushalP edited this page Feb 15, 2012 · 2 revisions

There are two types of exceptions that can occur in Webbit...

  1. Exceptions from user-code. This is typically signifies something you care about - your application responding in a way you did not expect.
  2. Exceptions caused by when reading/writing from/to the socket. This is typically a network issue, and in most cases something you should not care about. The Internet is flaky - expect browser connections to die.

You can catch these types exceptions by implementing java.lang.Thread.UncaughtExceptionHandler and plugging this in to WebServer.uncaughtExceptionHandler() or WebServer.connectionExceptionHandler() respectively.

By default, the behavior is:

  • Exceptions from user code cause a stack trace to be written to System.err. You almost certainly want to adapt this to your own application (e.g. send to a logging system, notify someone).
  • Exceptions caused when reading/writing from/to the socket are silently discarded.

Custom exception handler example

class MyCustomExceptionHandler implements Thread.UncaughtExceptionHandler {
  @Override
  void uncaughtException(Thread t, Throwable e) {
    // Log it, or whatever.
  }
}

Plugging it in:

WebServer webServer = WebServers.createWebServer(port)
  .add(new SomeHandler())
  .add(new AnotherHandler())
  .uncaughtExceptionHandler(new MyCustomExceptionHandler())
webServer.start();