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

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();
}
}

Related

refactor try-catch across class hierarchy

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?

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 it necessary to use throw keyword with Throwables.propagate(e)?

In this code, do I need the throw keyword in order to propagate the exception?
try {
//try something
} catch (Exception e) {
throw Throwables.propagate(e);
}
The Throwables documentation says that this method always throws an exception - is adding the throw superfluous? Could I have written the following instead?
try {
//try something
} catch (Exception e) {
Throwables.propagate(e);
}
The javadoc also states
The RuntimeException return type is only for client code to make Java
type system happy in case a return value is required by the enclosing
method.
and then provides this example
T doSomething() {
try {
return someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
return handle(e);
} catch (Throwable t) {
throw Throwables.propagate(t);
}
}
In other words, the return type (RuntimeException) is necessary so you can use the method in return or throws statements.
In the above example, if you omitted the throw in the last catch block, then the Java compiler would report an error because it cannot guarantee that a value will be returned from that catch block. A return or throws instead indicate that the method completes at that point, abruptly.

Return from method, in the "try" block or after "catch" block?

Is there any difference between following two methods?
Which one is preferable and why?
Prg1:
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
Prg2:
public static boolean test() throws Exception {
try {
doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return true;
}
Consider these cases where you're not returning a constant expression:
Case 1:
public static Val test() throws Exception {
try {
return doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
// Unreachable code goes here
}
Case 2:
public static Val test() throws Exception {
Val toReturn = null;
try {
toReturn = doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return toReturn;
}
I would prefer the first one. The second is more verbose and might cause some confusion when debugging.
If test() incorrectly returns null, and you see toReturn being initialized to null, you might think the problem is in test() (especially when test() is not just a simple example like this).
Even though it can only return null if doSomething returns null. But that might be hard to see at a glance.
You could then argue that, for consistency's sake, it's better to always use the first form.
Nope there is no difference between both the methods.
It will return true value in both the cases effectively by resuming the flow of the program as soon an and exception is handled.
Catch will be accessed only when an exception occurs.
I'm assuming this is a general question. Otherwise I might comment on other aspects of your method(s).
I think in the case or small methods like these it doesn't really matter. The method is short enough to understand immediately what's going on, what's related to what etc.
However, in the case of longer methods the flow is much easier to follow in the first example. In my opinion. It keeps together related code and related scenarios. When you're reading the method, the normal execution flow is not broken by the catch block, making it more obvious and "fluent".
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
But I won't generalize this for all methods; it's all about the context.
There is no difference, but the first Prg1 is faster than the Prg2.

Categories