user684434
user684434

Reputation: 1165

java class leve exception handling - catch exceptions of all methods in single place

I have a Java exception handling design question .

I have a Backing layer(java bean class) in my web app.
I would like to catch the exceptions(RunTimeE) thrown by all the methods in a class in single place. My backing bean class extends the AbstractBackingBean class.

Is there any way ,can i catch the exceptions from all the methods and log it in one place.

Upvotes: 0

Views: 106

Answers (1)

Bozho
Bozho

Reputation: 597324

Yes. For a web application you can do that in a Filter:

public void doFilter(..) {
  try {
      chain.doFilter(req, resp);
   } catch (Exception ex) {
       // do something
   }
}

If a filter is not an option for some reason, take a look at AOP. For example spring has good AOP support. It's similar to the filter approach, but you specify exactly which classes and methods you want to attach the handler to.

Upvotes: 1

Related Questions