Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code to select an image using Selenium, only now I have added an errorRecovery. Is there is a better way to right the code in the catch?

    public static void selectHeader() {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 20);
                .xpath("//*[contains(@id, 'picture-und-0-select')]")));
                By.xpath("//a[contains(@id, 'picture-und-0-select')]"))
    } catch (UnhandledAlertException alert) {
        errorRecovery error = _TestSuite.errorRecovery;
        if (error.getCount() == 0) {
        } else {
            throw new UnhandledAlertException(alert.toString());
share|improve this question

Judging by the overall structure it looks quite good, I would change a few things though with respect to formatting:

  1. Your public static void selectHeader() { line seems to be off, might've been due to putting the code here, you should remove 4 blanks.
  2. Take as example this line:

           .xpath("//*[contains(@id, 'picture-und-0-select')]")));

    I think it would read better as:

           By.xpath("//*[contains(@id, 'picture-und-0-select')]")));

    Note that now every argument is on one line.

  3. Similar situations a few lines below.

Then onto the exception handling:

  1. First I would change UnhandledAlertException alert to UnhandledAlertException alertEx, to denote that it is about an exception.
  2. errorRecovery error = _TestSuite.errorRecovery confuses me to no ends, hence these suggestions:

    • _TestSuite should not have the underscore in front of it, I am still assuming it is a class though.
    • errorRecovery appears to be a class, which really should and needs to be upper-cased to prevent any confusion I had so far.
  3. Your header_afbeelding_selecteer() method call is in Dutch and not in camelCase, it should be something like headerImageSelect().

  4. Lastly, your ErrorRecovery error is not an error, it is an recovery or an errorRecovery, please name your variable like that.

I do not fully get what your error handling code does though, there also seems to be logic ongoing which is not neccessary, all I see that you are really using is the method call to headerImageSelect().

Therefore I suggest to add the following to your ErrorRecovery class:

public void handleRecovery(final Runnable methodToRun, final String exceptionMessage) throws UnhandledAlertException {
    if (getCount() == 0) {
    else {
        throw new UnhandledAletException(exceptionMessage);

Which can then be called as:

errorRecovery.handleRecovery(new Runnable() {
    public void run() {
    }, alertEx.toString());

Or if you are using Java 8, just:

errorRecovery.handleRecovery(() -> headerImageSelect(), alertEx.toString());

or even better:

errorRecovery.handleRecovery(this::headerImageSelect, alertEx.toString());

The design still seems weird to me though, which perhaps now is even more obvious, why does your error recovery method throw an UnhandledAlertException? Indicating that your error would need recovery again!

share|improve this answer
the browser used by Selenium sometimes throws a random pop-up after wich an UnhandledAlertException is thrown, what I want do with this errorRecovery is that when the exception occurs the method is run again and if that also fails, that only then an error will be given. – Apjuh Jul 23 '14 at 11:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.