0

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 ?

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I agree to this solution, except I would probably let the exception bubble up one more layer before catching and handling it. In other words, I would catch and handle it inside the saveBuilding() method. I prefer to catch exceptions close to where the request originally started. In a GUI app, that is in the event handler. You might be able to implement some reusable notify() method that can notify the user of exceptions from anywhere.
@Jakob Jenkov If you throw the exception to saveBuilding(), what happens to the remainder of your data?
Alternatively you could have a separate extractAndValidate() method which attempts to extract and validate the data. You call this method before you call any save methods. If any exceptions occur, you can catch them around this method call. That way it is easier to separate validation exceptions from remote service exceptions.
You don't save any data unless all data is valid. That is what happens. First validate, handle exceptions, and then save, and handle exceptions.
@Jakob Jenkov I didn't say throw. I said throw TO. Also, he isn't saving any data. He's displaying the data. Finally, edit your comments rather than double posting.
|
1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.