Uncaught Exception Handlers

This is a feature that every programmer ought to know. Everyone should already be familiar with the try catch throw keywords.

Uncaught exception handlers allow us to write code that gets executed once a exception falls through all possible catch(ExceptionType ex) blocks.
This is very useful because no matter where our program crashes, we get the opportunity to log the error.
This has saved me countless hours of debugging hard to catch bugs, because I get the last exception logged, before a program/thread exits. Otherwise it would just exit quietly.

Almost every language should have some form of these handlers, and it is always a wise choice to attach your logger to those.

Here is a example of how it's done in java:


Thread.UncaughtExceptionHandler uncaught_handler = (t, e) -> {
    e.printStackTrace();
    // additional logging logic
};
// set the default handler for all existing and new threads
Thread.setDefaultUncaughtExceptionHandler(uncaught_handler);

©
Tobias Hübner