Single Catch Block for Multiple Methods in a Class java -


i have class static methods handling memcache requests, has different methods different arguments.

whenever method called in class,i want increment totalrequest counter , if exception occur in of methods inside class want increment failed counter. there way ?

if being static in nature has problems, let me know ? should efficient.

i use aop. if use guice or maven it's quite easy put in place around beans. you'll declare interceptor, guice or maven wrap around methods.

there aspectj have never used it.

finally, wrap calls in caller method. java 8 , lambda, this:

protected <t> t callwithtrace(supplier<t> fn) throws exception {   //some code before call   try {      t output=fn.get();      //code after normal exec      return output;   }   catch(exception e) {      //code in case of error (increase counter??)      throw e;   } }  void someothermethod() {    object ret = callwithtrace({ () -> calltoyourdangerousmethod()});    object ret = callwithtrace({ () -> calltoanotherdangerousmethod()}); } 

with java 7, anonymous inner classes, hard read.


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -