refactor try-catch across class hierarchy - java

Suppose you have this code:
class Base {
void method() throws Exception {
try {
//Lots of code here
} catch (Exception e) {
//handle it (very little code here)
}
}
class Derived extends Base {
#Override
void method() throws Exception {
try {
//Lots of code here (same code as in base class)
} catch (Exception e) {
//handle it (very little code here; also same as base class)
} catch (Error e) {
e.printStackTrace();
System.exit(1);
}
}
So, as we can see, the methods are the same except the derived class has an extra catch clause. Is there a nice way to have a bit less duplicated code here?

Related

Execute same piece of code in catch clause for all methods in a class

I have a class that has many methods. All the methods throw one exception when data is not ready. In that case, I want to retry the method after a certain interval. So in catch, I need to add retry logic. The same logic i need to add for all methods.
Is there some way/pattern to execute same logic for all catch clause without copy paste
One way I could think of is to write my own Exception class and Throw that exception. And do this retry logic from My Exception class.
Is there any other better way to this?
class MyClass {
public void method1() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
public void method2() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
public void method3() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
}
EDIT:
class MyClass {
public void method1(int a, int b) {
try {
//do some logic
} catch (Exception e) {
Object args[] = {a,b};
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public void method2() {
try {
//do some logic
} catch (Exception e) {
Object args[] = null;
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public void method3(String abcd, int a) {
try {
//do some logic
} catch (Exception e) {
Object args[] = {abcd,a};
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public boolean executeLater(String methodName, Object args[]){
//Execute given method with the supplied args
return true;
}
}
Added code that shows what i would be doing in each catch clause
boolean processCompleted=false;
while(!processCompleted){
try{
doProcess();
processCompleted=true;
}catch(Exception e){
Thread.sleep(10000);
}
}
This might give you an idea. It keeps try to call doProcess until it doesn't throw exception. If any exception occurs, waits 10 seconds.
Well, you could extract the whole catch block content to a method and call that one, but this only works if your retry logic is not dependent on the specific method. And it also requires a try-catch in every method.
Instead, use functional programming to shorten it:
public class Playground
{
public static void main(String[] args)
{
new Playground().method2(1, 2);
new Playground().method1();
}
public void method1()
{
tryAndTryAgain(() -> {
// logic 1
System.out.println("no params");
throw new RuntimeException();
});
}
public void method2(int a, int b)
{
tryAndTryAgain(() -> {
// logic 2
System.out.println(a + " " + b);
throw new RuntimeException();
});
}
public static void tryAndTryAgain(Runnable tryThis)
{
try
{
tryThis.run();
}
catch (Exception e)
{
new Timer().schedule(new TimerTask()
{
#Override
public void run()
{
tryAndTryAgain(tryThis);
}
}, 1000);
// existing exception handling logic
}
}
}
The exact structure depends on your specfic implementation, but it should give you an idea how to structure it. The benefit is that all those methods can concentrate on the business logic, and the retry logic and exception handling are done in a util method. And that util method doesn't even need to know anything about parameters, methods, or anything, because all the business logic is contained in the Runnable.

Java SE 7: alternative to try-catch for testing

I have a bunch of similar methods called from #Before beforeTest() in a test class:
//...
private void addClientDetails() {
try {
clientDetailsService.addClientDetails(testClient);
} catch (Exception e) {
}
}
private void addUserRoles() {
try {
adminController.addUserRoles(addedRoles);
} catch (Exception e) {
}
}
private void deleteAddedRoles() {
for (String role : addedRoles) {
try {
adminController.deleteUserRole(role);
} catch (Exception e) {
}
}
}
private void deleteClients() {
try {
clientsController.deleteClient(testClient.getClientId());
} catch (Exception e) {
}
}
//...
It is really unnecessary to catch possible exceptions and inconvenient to add some ifs here. These are the auxiliary methods to prepare tests or clean up after tests.
How to get rid of those ridiculous try {...} catch (...) {} constructs?
The idea was to create a new method with Runnable argument but this leads to even more cumbersome syntax:
private void deleteClients() {
trySilently(new Runnable() {
#Override
public void run() {
}
});
}
private void trySilently(Runnable task) {
try {
task.run();
} catch (Exception e) {
//do nothing
}
}
In JDK 1.8 method reference can help. But is there any beautiful solution in terms of JDK 1.7?
It is understood ignoring exceptions is a bad practice. Nevertheless the question is exactly how to do it in a graceful way.
You can declare that those methods throw exceptions, e.g.:
private void addClientDetails() throws Exception {
clientDetailsService.addClientDetails(testClient);
}
...then use reflection to call them:
String[] methods = {"addClientDetails", "addUserDetails" /*, ...*/};
for (String method : methods) {
try {
TestClass.class.getMethod(method).invoke(testObject);
}
catch (Exception e) {
// STRONGLY RECOMMEND DOING SOMETHING HERE SO YOU'RE NOT SILENTLY
// IGNORING EXCEPTIONS
}
}
(You'll need to keep the handler in deleteAddedRoles, though, since it loops, if you really want to ignore exceptions from adminController.deleteUserRole.)
Note: It seems very strange to be completely ignoring those exceptions. It's hard to imagine how you can trust your test results if you silently ignore exceptions from the test code. But I'm assuming you know what you're doing... :-)
In TestNG there is no problem for method annotated with #BeforeClass/#BeforeMethod to throw exception.
Why wouldn't you just
#BeforeClass
private void addClientDetails() throws Exception{
clientDetailsService.addClientDetails(testClient);
}
This also works for #Test methods.
Silenty catching exception is very bad idea. How could you trust your tests then? are you sure that exceptions tha occured is no problem indeed? If yes, then exception should not be thrown in first place.
Also, you could redesing your API to use unchecked exceptions. Just wrap any checked exceptions in RuntimeException, and throw RuntimeException.

Is the same code with different exception handling considered as repeated code?

As refactoring my code, I realize some of code is repeated except the exception handling of them. I am wondering whether these codes are considered to be repeated to refactor, if so, how?
Specific sample:
E.g I have two methods.
void fun1() {
try {
foo();
} catch (Exception ex) {
handle1();
}
}
void fun2() {
try {
foo();
} catch (Exception ex) {
handle2();
}
}
Why not do like this:
try {
foo();
} catch (Exception1 ex) {
handle1();
} catch (Exception2 ex) {
handle2();
}
Ok, so first of all, catching all Exceptions is a bad habit, you shouldn't really do it, only catch the ones that you expect.
I woudln't consider it repeated, because in one case you may expect one exception in another you may expect a different one. That's another reason to be specific in you catch clauses. Then, you can also use a multicatch statement, like this:
void fun() {
try {
foo();
} catch (MyFirstException ex) {
handle1();
} catch (MySecondException ex) {
handle2();
}
}
There is some repetition here, but not much. You could consolidate the code somewhat better, by passing the expected error-handling function into a helper method as an argument. Note that this would be much more verbose prior to Java 8.
void fun1() {
funHelper(this::handle1);
}
void fun2() {
funHelper(this::handle2);
}
void funHelper(Runnable errorHandler) {
try {
foo();
} catch (Exception ex) {
errorHandler.run();
}
}

Java generic interfaces

I have generic class type, T as follow:
class MyClass<T>
Also I know that T is interface with only one method inside but I don't know what interface, I can't write this:
class MyClass< T extends TheInterface >
So is there a way to invoke this method?
public void callMe(T me, Object...params){
// How can I invoke T interface method?
}
I been trying this:
public void callMe(T me, Object... params) {
// methods.length is 244, just as in my activity class
Method[] methods = me.getClass().getMethods();
try {
methods[0].invoke(me, params);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
e.printStackTrace();
return;
}
}
But it's not working
Edit: I posted new question that explain why I need this for
Yes, you have to write:
class MyClass< T extends TheInterface >
With this information, the compiler know that T have an operation named callMe
I'm not sure, are you looking for the Strategy pattern?
If you can not change declaration inside MyClass then you can just typecast it.
public void callMe(T me, Object... params) {
Someinterface instance = (Someinterface) me;
list.callMe(me, params);
}
But still declaring it like below should be the best solution
class MyClass<T extends Someinterface >
Below is the simple demonstration of how to do it using reflection
class MyClass<T> {
public void callMe(T me, Object... params) throws SecurityException,
NoSuchMethodException {
// How can I invoke T interface method?
Method size = me.getClass().getMethod("size", null);
Method add = me.getClass().getMethod("add", Object.class);
try {
add.invoke(me, 10);
System.out.println(size.invoke(me, params));
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
e.printStackTrace();
return;
}
}
}
MyClass<List> myclass = new MyClass<List>();
myclass.callMe(new ArrayList(), null);
Output :
1

Access Try-Catch block's code in another class

May be it could be silly,but I want to clear my the technical understanding of this code:
import netscape.*;//ldap jar
public class A {
public void method() {
...
try {
//code is written here.
LDAPSearchResults lsr = ldi.search(LDAPConnectionInfo.MY_SEARCHBASE,LDAPConnectionInfo.MY_SCOPE,LDAPConnectionInfo.MY_FILTER,null,false);
while(lsr.hasMoreElements()){
LDAPEntry findEntry = (LDAPEntry)lsr.nextElement();
} catch(...) {
}
}
}
Now I call another class
public class B {
A a = new A();
//here I want to use attributeName
}
How could I access A class's member(in try block) in B class.
Any way to handle try block code for reuse in another class.
How could I handle all those exception in another class.
Any modification should I need...
Calling method of Object type.
public class C{
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public Object method(){
attributeName=lAttribute.getName();
}
}
How could print this Object type method into String(in a jsp page)... any inputs
You'll need a member in class A and a getter:
public class A {
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public void method(){
...
try {
//code is written here.
attributeName = lAttribute.getName();
}
catch() {
}
}
}
Then:
public class B {
A a = new A();
// somewhere
String str = a.getAttributeName();
}
There's no way to access a method's private variables like you did in the original example, as they only exist on the stack during the method call.
Edit: I noticed another question:
How could I handle all those exception in another class.
I assume you want to call your method somewhere else and catch the exceptions there. In that case you can use the throws keyword to communicate that your method will pass exceptions to the caller:
public class A {
public void method() throws IOException {
//code is written here.
String attributeName = lAttribute.getName();
}
public void anotherMethod() {
try {
method();
} catch(IOException ex) {
...
}
}
}
then if some other piece of code calls method it will be forced to either handle or further propagate the exception.
How could I handle all those exception in another class.
In your calling class you can catch Throwable (which will catch all exceptions and errors)
try {
....
}
catch (Throwable t) {
//do something with the throwable.
}
if you do not want to catch Errors (Ive only done this when messing around with ImageIO and had memory problems) in Java then catch Exception instead
Any way to handle try block code for reuse in another class
here you could create a method in another class and then call it within your try /catch block
public class XYX {
public void methodForTry() throws Exception {
//do something
}
}
try {
new XYZ().methodForTry();
}
catch (Exception e){
}
You may or may not want to create new XYZ here. It depends what state this object may or may not hold.
As to the last questions I think Tudor's answer covers this
Your question may be about extracting the code template
try { ... do stuff ... }
catch (MyFirstException e) { ...handle ... }
catch (MySecondException e) { ...handle ... }
... more catch ...
Where you only want to change the ... do stuff ... part. In that case you'd need closures, which are coming with Java 8, and today you'd need something quite cumbersome, of this sort:
public static void tryCatch(RunnableExc r) {
try { r.run(); }
catch (MyFirstException e) { ...handle ... }
catch (MySecondException e) { ...handle ... }
... more catch ...
}
where RunnableExc would be an
interface RunnableExc { void run() throws Exception; }
and you'd use it this way:
tryCatch(new RunnableExc() { public void run() throws Exception {
... do stuff ...
}});
why not return it?
public String method() {
String attributeName
try {
//code is written here.
attributeName = lAttribute.getName();
} catch(...) {
}
return attributeName;
}
public class B {
A a = new A();
String attributeName = a.method();
}

Categories