开发者

Best practice for handling multiple exceptions in similar ways in Java

Is there a canonical best-way-to-do-this for the following situation?

I have a block of code that can generate a number of different exceptions, each of which is handled by hiding a dialog, displaying an error message and running an onDisconnect() method. The catch is that, for each exception, the error message needs to be different. As I see it, there are two choices. The first is to catch Exception, then handle the various exceptions inside the catch block using instanceof, like so:

    } catch (Exception e) {
        dialog.dismiss();

        String errorMessage = getString(R.string.default_error);
        if (e instanceof ArrayIndexOutOfBoundsException) 
            errorMessage = getString(R.string.bad_host); 
        else if (e instanceof UnknownHostException) 
            errorMessage = getString(R.string.unknown_host);
        else if (e instanceof NumberFormatException) 
            errorMessage = getString(R.string.bad_port);
        else if (e instanceof IOException)
            errorMessage = getString(R.string.no_connection);
        showError(errorMessage);

        onDisconnect();
    }

The other option is to catch all of them separately, like so:

    } catch (ArrayIndexOutOfBoundsException e) {
        dialog.dismiss();
        showError(getString(R.string.bad_host));
        onDisconnect();
    } catch (UnknownHostException e) 
        dialog.dismiss();
        showError(getString(R.string.unknown_host));
        onDisconnect();
    } // ...etc.

Is there a preferred way to do t开发者_运维知识库his? I opted for the first case (at least for now) because it minimizes duplicated code, but I've also heard that instanceof and catch (Exception) are the works of Satan.


My preference is to have a separate method like this:

void handleException(String msg) {
   dialog.dismiss();
   showError(getString(msg));
   onDisconnect();
}

and then in your code that throws the exception just like this:

} catch (ArrayIndexOutOfBoundsException e) {
        handleException(getString(R.string.bad_host));
    } catch (UnknownHostException e) 
        handleException(getString(R.string.unknown_host));
    } // ...etc.


You want to catch them separately. If you catch the generic Exception, you might end up catching Exceptions that you're not expecting (that could be pushed up from somewhere else in the stack for example) and you're stopping them from propagating up to wherever they were actually meant to be handled.

(Later edit): You may also want to look into using finally to avoid some of the code repetition problem.


make a (private void) method for handling the error. You may be required to pass it in some parameters about what's going on, but the behavior will be consistent. When you make a change to the method it changes all places where it's used, so you get to reduce your boilerplate code.

try {
   // stuff

} catch(OneException) {
    handleSimilarExceptions();
} catch(TwoException) {
    handleSimilarExceptions();
} catch(DifferentException) {
    log.write("Something wierd happened, handling it");
    somethingDifferent();
}


What if you do smth like modified second version:

   catch (ArrayIndexOutOfBoundsException e) {
        handleException(R.string.bad_host);
    } catch (UnknownHostException e) 
        handleException(R.string.unknown_host);
    } // ...etc.

  void handleException(String s) {
        dialog.dismiss();
        showError(getString(s));
        onDisconnect();
  }

And I agree, instanceof usage like this is a work of Satan...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜