Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

exception handling - GWT.setUncaughtExceptionHandler()

Has anyone successfully used the above statement to catch the exception before it goes to the browser as an alert?.

I registered a custom Exception Handler in the first line of my application entry point. But it does not catch the exception as expected.

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

EDIT

Here are my two classes:

I expect my system.out will print the details of the exception and exception will be swallowed and should not be sent to browser.

Or Am I wrong?

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());
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe what's happening here is that the current JS event cycle is using the DefaultUncaughtExceptionHandler because that was the handler set at the start of the cycle. You'll need to defer further initialization to the next event cycle, like this:

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
}

Update: And here's the issue that describes why this works, and why it isn't planned to be fixed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...