简体   繁体   中英

Handling exceptions in Java (GWT)

I'm currently dealing with exceptions handling and I'm wondering where should I catch them.

Here is an stack from the GWT app :

  • A helper with a method which can throws NumerFormatExeption (FormHelper.java)
  • A widget which uses this helper (CostWidget.java)
  • A presenter which calls this widget to retrieve data (BuildingPresenter.java)

FormHelper.java

public static Integer prepareIntegerForDb(String string) {
    return Integer.parseInt(string);    
}

CostWidget.java

public DetailCostProxy getCostDetail() { 
    ...
    costDetail.setQuantity(FormHelper.prepareDoubleForBd(qtTextBox.getText()));
    ...
    return costDetail;
}


public List<DetailCostProxy> getCostList() {
    ...
    costDetails .add(ligneCout.getCostDetail());
    ...
}

BuildingPresenter.java

public void saveBuilding(final BuildingProxy inter, final CollectRequestContext savecontext) {

    savecontext.save(display.getCostWidget().getCoutList()).fire(new Receiver<BuildingProxy >() {....

}

I am thinking about :

1) adding "throws NumberFormatException" to prepareIntegerForDb() in the helper

2) adding "throws NumberFormatException" to getCostDetail() in the widget

3) adding "throws NumberFormatException" to getCostList() in the widget

4) caching the exception in the presenter (in saveBuilding)

The aim is :

  • to log the exception
  • to provide the user with a message saying that something went wrong

What do you think about this approach considering that this in an example and I will have to apply this pattern into the entire app (more than 20 presenters).

Is my way a good way to handle exceptions in GWT ? or should I log the error directly in the helper or elsewhere ?

prepareIntegerForDB() should throw the exception. This happens automatically when Integer.parse() fails, and you do not have to actually throw the Exception.

getCostDetail() should explicitly catch and throw the exception, and possibly expand upon why it was thrown. Something like " The cost was not in a readable format ". That method is responsible for only that one line.

getCostList() should catch and handle the exceptions. That method is responsible for an entire collection. If you do not handle the bad data here, you will lose the good data. Here is one way to handle the bad data.

public List<DetailCostProxy> getCostList() {
    ...

    try {
        DetailCostProxy cost = lineCount.getCostDetail()
        costDetails.add(cost);
    catch (NumberFormatException e) {
        costDetails.add(null);
    }

    ...
}

Finally, the method that displays your data to the user should interpret the data passed to it before displaying it. If you used my example above, this would be as simple as checking for null values.

What do you think about this approach considering that this in an example and I will have to apply this pattern into the entire app (more than 20 presenters).

Adding throws NumberFormatException declarations won't help you to "provide the user with a message saying that something went wrong". NumberFormatException-s are RuntimeException-s so the throws declaration won't even force to try/catch in the code that uses these methods.

Is my way a good way to handle exceptions in GWT ? or should I log the error directly in the helper or elsewhere ?

4) catching the exception in the presenter (in saveBuilding)

The aim is :

  • to log the exception

  • to provide the user with a message saying that something went wrong

This question is not specific to GWT.

To catch the Exception is a good idea if you know how to deal with it.

If you signal the error to the user, you need to be able to have the user decide how to handle the issue (for example a pop-up message proposing two actions to resume the application execution).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM