Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
    }
}
like image 299
moorsu Avatar asked Jun 12 '10 11:06

moorsu


2 Answers

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.

like image 147
Isaac Truett Avatar answered Sep 22 '22 20:09

Isaac Truett


Setting up a default handler can be a tricky proposition some times. I can tell you exactly what is going on. If you get an exception in the onModuleLoad(), the handler will not be called. It is only after the load method is completed that it will ACTUALLY get put into place.

like image 37
Jeff Richley Avatar answered Sep 22 '22 20:09

Jeff Richley