简体   繁体   English

在Java(GWT)中处理异常

[英]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 : 这是GWT应用程序中的堆栈:

  • A helper with a method which can throws NumerFormatExeption (FormHelper.java) 具有可以抛出NumerFormatExeption(FormHelper.java)的方法的助手
  • A widget which uses this helper (CostWidget.java) 使用此帮助器的小部件(CostWidget.java)
  • A presenter which calls this widget to retrieve data (BuildingPresenter.java) 演示者,调用此小部件以检索数据(BuildingPresenter.java)

FormHelper.java FormHelper.java

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

CostWidget.java CostWidget.java

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


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

BuildingPresenter.java 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 1)在助手中添加“ throws NumberFormatException”到prepareIntegerForDb()

2) adding "throws NumberFormatException" to getCostDetail() in the widget 2)在小部件中的getCostDetail()中添加“ throws NumberFormatException”

3) adding "throws NumberFormatException" to getCostList() in the widget 3)在小部件中的getCostList()中添加“ throws NumberFormatException”

4) caching the exception in the presenter (in saveBuilding) 4)在演示者中缓存异常(在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). 考虑到一个示例,您对这种方法有何看法,我将不得不将此模式应用于整个应用程序(超过20位演示者)。

Is my way a good way to handle exceptions in GWT ? 我的方法是处理GWT中异常的好方法吗? or should I log the error directly in the helper or elsewhere ? 还是应该直接在助手或其他地方记录错误?

prepareIntegerForDB() should throw the exception. prepareIntegerForDB()应该抛出异常。 This happens automatically when Integer.parse() fails, and you do not have to actually throw the Exception. 当Integer.parse()失败并且您不必实际抛出异常时,会自动发生这种情况。

getCostDetail() should explicitly catch and throw the exception, and possibly expand upon why it was thrown. getCostDetail()应该显式捕获引发异常,并可能扩展引发异常的原因。 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. getCostList()应该捕获并处理异常。 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). 考虑到一个示例,您对这种方法有何看法,我将不得不将此模式应用于整个应用程序(超过20位演示者)。

Adding throws NumberFormatException declarations won't help you to "provide the user with a message saying that something went wrong". 添加throws NumberFormatException声明不会帮助您“向用户提供一条消息,指出发生了问题”。 NumberFormatException-s are RuntimeException-s so the throws declaration won't even force to try/catch in the code that uses these methods. NumberFormatException-sRuntimeException-s因此throws明甚至不会强制try/catch使用这些方法的代码。

Is my way a good way to handle exceptions in GWT ? 我的方法是处理GWT中异常的好方法吗? or should I log the error directly in the helper or elsewhere ? 还是应该直接在助手或其他地方记录错误?

4) catching the exception in the presenter (in saveBuilding) 4)在演示者中捕获异常(在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. 这个问题并非专门针对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). 如果将错误告知用户,则需要使用户能够决定如何处理该问题(例如,弹出消息,建议采取两种措施来恢复应用程序的执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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