GWT.setUncaughtExceptionHandler()

问题描述 投票:15回答:4

是否有人成功使用上述语句来捕获异常,然后才将其作为警报进入浏览器?

我在应用程序入口点的第一行注册了一个自定义异常处理程序。但它没有按预期捕获异常。

public void onModuleLoad(){
    GWT.setUncaughtExceptionHandler(new MyExceptionHandler());
    ...
    ....
}

编辑

这是我的两个班级:

我希望我的system.out将打印异常的细节,异常将被吞下,不应该发送到浏览器。

还是我错了?

package mypackage;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

public class MyEntryPoint implements EntryPoint {

    public void onModuleLoad() {
    GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
    startApplication();
    }

    private void startApplication() {
    Integer.parseInt("I_AM_NOT_NUMBER");
    }
}

package mypackage;

import com.google.gwt.core.client.GWT;

public class ClientExceptionHandler implements GWT.UncaughtExceptionHandler {

    @Override
    public void onUncaughtException(Throwable cause) {
    System.out.println(cause.getMessage());
    }
}
gwt exception-handling
4个回答
21
投票

我相信这里发生的事情是当前的JS事件循环正在使用DefaultUncaughtExceptionHandler,因为那是在循环开始时设置的处理程序。您需要将进一步的初始化推迟到下一个事件周期,如下所示:

public void onModuleLoad() {
    GWT.setUncaughtExceptionHandler(new ClientExceptionHandler());
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        @Override
        public void execute() {
           startApplication();
           Window.alert("You won't see this");
        }
    });
}

private void startApplication() {
    Integer.parseInt("I_AM_NOT_A_NUMBER");
    // or any exception that results from server call
}

更新:和here's the issue描述了为什么这样做,以及为什么它没有计划修复。


1
投票

有时,设置默认处理程序可能是一个棘手的问题。我可以准确地告诉你发生了什么。如果在onModuleLoad()中出现异常,则不会调用该处理程序。只有在加载方法完成后,它才会实际安装到位。


0
投票

您应该尝试以下方法:

public void onModuleLoad(){
    GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        onUncaughtException(Throwable t) {
            // Do stuff here
        }
    });
}

看看是否有帮助。


-1
投票

傻解决方案,但它工作正常!之前在app.gwt.xml中添加您的EntryPoint

<entry-point class='myPackage.client.MyEntryPoint' />

然后;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
public class MyEntryPoint implements EntryPoint {

    private EventBus eventBus;
    @Inject
    public MyEntryPoint(final EventBus eventBus){
        this.eventBus = eventBus;
    }

    @Override
    public void onModuleLoad() {

        CustomUncaughtExceptionHandler customUncaughtExceptionHandler = new CustomUncaughtExceptionHandler();
        GWT.setUncaughtExceptionHandler(customUncaughtExceptionHandler);
        try {
            onModuleLoad2();
        } catch (RuntimeException ex) {
            eventBus.fireEvent(new BusyEvent(false));
            customUncaughtExceptionHandler.onUncaughtException(ex);
        }

    }

    private void onModuleLoad2() {
        throw new RuntimeException("test");
    }
}

你的Custom UncaughtExceptionHandler会是这样的:

import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.user.client.Window;
public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
    @Override
    public void onUncaughtException(Throwable e) {
        Throwable exceptionToDisplay = getExceptionToDisplay( e );
         Window.alert( exceptionToDisplay.getCause() .getMessage()+"    "+ exceptionToDisplay.getStackTrace());

    }
    private static Throwable getExceptionToDisplay( Throwable throwable ) {
         Throwable result = throwable;
         if (throwable instanceof UmbrellaException && ((UmbrellaException) throwable).getCauses().size() >= 1) {
             result = ((UmbrellaException) throwable).getCauses().iterator().next();
         }
         return result;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.