Central Exception Handler
Posted
by
J-unior
on Programmers
See other posts from Programmers
or by J-unior
Published on 2012-11-15T08:59:36Z
Indexed on
2012/11/15
17:22 UTC
Read the original article
Hit count: 496
Recently I've been thinking about a general ExceptionHandler, that I could initialize once in my app context and inject it everywhere. The idea that it will have quite simple interface with just public void handle(Exception ex)
, and then according to exception type it should decide what to do, maybe just log it, or show an alert message to the user, or maybe kill the whole app.
The question is, what is the prettiest way to write such handler without lots of instanceof
s? Unfortunately googling gives me only the default exception handler for RuntimeException
that was introduced in Java 5.
My first idea is to create an enum, that will have Class field for exception type and it will return the appropriate execution point, for example a concrete exception handler that also implements the interface public void handle(Exception ex)
, but with the required casting already.
© Programmers or respective owner