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
Post a Comment