I want to write a json file recording the execution duration and unhandled exception if any once.
Java provides Runtime.addShutdownHook and Thread.setDefaultUncaughtExceptionHandler
However, Runtime.addShutdownHook is called regardless of whether the app is shutting down due to unhandled exception or normal completion.
What is a good way to log the unhandled exception only when it happens. I have a few different cmd line apps that require similar logging so i am avoiding using try-catch-finally construct.