This week's book giveaway is in the Reactive Progamming forum. We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line! See this thread for details.
We have a web application presentation layer is done using JSF which talks to business logic layer interfaces ( supplied as jar to JSF application) and business logic access DAO layer jar file to talk to backend.
My question is: Which is the best place ( in which layer) to put "log" statement when handling the exception? Should exception be logged when first caught? or let it propagate to UI layer and there it is logged?
Good question, and you will find many answers to it. And I wouldn't necessariy argue with any of them. They each have their pros and cons.
The design I like best is using AOP. With AOP I can create a single class that is the ExceptionHandler, and in one method that is a AfterThrows advice, catch the different Exceptions, wrap them into a single ApplicationException, and log the Exception to the logger.
What this means is that there is only one single source for Exception Handling. The client only ever needs to catch one Exception, so making a nice User friendly screen that handles that one exception is easier.
It will make you only need to put try catch code in your business code when you can actually handle that exception at that point in the code. It will also remove any logging code you would have had in your business code, keeping it clean.