GWT.setUncaughtExceptionHandler()
在异常作为警报进入浏览器之前,是否有人成功使用上述语句捕获异常 我在应用程序入口点的第一行注册了一个自定义异常处理程序。但它并没有像预期的那样捕获异常GWT.setUncaughtExceptionHandler(),gwt,exception-handling,Gwt,Exception Handling,在异常作为警报进入浏览器之前,是否有人成功使用上述语句捕获异常 我在应用程序入口点的第一行注册了一个自定义异常处理程序。但它并没有像预期的那样捕获异常 public void onModuleLoad(){ GWT.setUncaughtExceptionHandler(new MyExceptionHandler()); ... .... } 编辑 以下是我的两门课: 我希望我的system.out会打印异常的详细信息 异常将被吞并,不应发送到浏览器 还是我错了 pa
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");
}
}
您应该尝试以下方法:
public void onModuleLoad(){
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
onUncaughtException(Throwable t) {
// Do stuff here
}
});
}
看看这是否有帮助。我认为当前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
}
更新:这说明了为什么会这样做,以及为什么不计划对其进行修复。设置默认处理程序有时是一个棘手的问题。我可以确切地告诉你发生了什么事。如果在onModuleLoad()中出现异常,则不会调用该处理程序。只有在加载方法完成后,它才能真正投入使用。愚蠢的解决方案,但效果很好! 在任何操作之前,请在app.gwt.xml中添加您的入口点
<entry-point class='myPackage.client.MyEntryPoint' />
您的CustomUncaughtExceptionHandler类似于:
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;
}
}
MyExceptionHandler
做什么?您是否尝试过使用调试器查看异常是否会进入您的处理程序?来自javadocs:“在托管模式下,默认处理程序将堆栈跟踪打印到日志窗口。在web模式下,默认处理程序为null,因此允许异常转义,这提供了使用JavaScript调试器的机会。”您是否已检查是否确实设置了此默认处理程序?并可能尝试通过GWT.setUncaughtExceptionHandler(null)取消设置它
然后尝试设置您的(我知道这听起来不太聪明,但可能是GWT中的一个bug,默认处理程序没有正确地“unset”)。亲爱的Jason,MyExceptionHandler是GWT.UncaughtExceptionHandler的一个实现。程序流根本没有到达onUncaughtException(Throwable cause)方法。亲爱的Igor,使用null和自定义UncaughtExceptionHandler设置无效。这也是同样的问题。setUncaughtExceptionHandler()确实有效。与其描述没有发生的事情,不如描述正在发生的事情?如果你能提供一个代码片段来重现你遇到的问题,那会有很大帮助。如果OP的MyExceptionHandler
实现了GWT.UncaughtExceptionHandler
,那么我不认为这有什么关系。亲爱的Jeroen,我确实尝试了前面建议的匿名版本。但问题还是一样。当我运行我的应用程序时,我会看到“你不会看到这个”的文字为什么?
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");
}
}
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;
}
}