HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Exception handling continuing the excecution

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingexcecutiontheexceptioncontinuing

Problem

This code is for continuing the execution after an exception, and this is ugly:

int step=0;

    do{
        try{  
                switch(step)
                {
                case 0:
                    step=1;
                    methodAAAAACanThrowException();
                    break;
                case 1:
                    step=2;
                    methodBBBBBCanThrowException();
                    break;
                case 2:
                    step=3;
                    methodCCCCCanThrowException();
                    break;
                    //....

                    //more and more

                    //....
                case 25:
                    step=26;
                    methodZZZZZCanThrowException();                        
                }                      
        }catch( Exception ex)
        {  

            Logger.getLogger(ABC.class.getName()).log(Level.SEVERE, null, ex);
        }
    }while(step<26);
}


but writing a single try-catch loop for every call, seems even uglier..

Is there any other more elegant option for doing that?
thanks

Solution

How about something like this?

Define a one-method interface like so:

public interface TryAndLogFailure { void execute(); }


Then define a method which takes an object which implements the interface and does your exception handling for you:

public void tryAndLogFailure(TryAndLogFailure talf) {
  try {
    talf.execute();
  } catch (Exception e) {
    logger.log(...);
  }
}


Now your calls to the methods can be sequential:

tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodAAAACanThrowException(); } });
tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodBBBBCanThrowException(); } });
// etc...
tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodZZZZCanThrowException(); } });


Admittedly, this could probably be worked a little more to make it prettier, but this is my first crack off the top of my head. If only Java had support for closures.

(Also - I should note that I agree with Winston. The reason why this code feels ugly is because the concept itself is ugly. Catching Exception is almost never a good idea and requires strong justification. Given more context, we may be able to help you redesign the code to avoid this sort of code smell.)

Code Snippets

public interface TryAndLogFailure { void execute(); }
public void tryAndLogFailure(TryAndLogFailure talf) {
  try {
    talf.execute();
  } catch (Exception e) {
    logger.log(...);
  }
}
tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodAAAACanThrowException(); } });
tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodBBBBCanThrowException(); } });
// etc...
tryAndLogFailure(new TryAndLogFailure{ public void execute() { methodZZZZCanThrowException(); } });

Context

StackExchange Code Review Q#8222, answer score: 6

Revisions (0)

No revisions yet.