tags:

views:

247

answers:

4

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());
    ...
    ....
}
A: 

You should try the following:

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

and see if that helps.

Jeroen
If the OP's `MyExceptionHandler` implements `GWT.UncaughtExceptionHandler`, then I don't see how this should matter.
Igor Klimer
Dear Jeroen,I did try the anonymous version as suggested earlier. But the problem remains the same.
moorsu
A: 
//-- Global error handling...
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() 
{ 
    public void onUncaughtException(Throwable e) {
        Window.aler("I Caught IT !!!!!!");
    } 
});
int error = Window.Location.getParameter("test").length();

Does not show the alert, am i doing something wrong here ??

Salvin Francis
ahem : I have added this to the onload method... :)
Salvin Francis
A: 

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.

Isaac Truett
A: 
moorsu
@moorsu: Sorry, I didn't see this until now. I originally posted an answer because I was new to SO and didn't have permission for comments. Then I deleted my answer and reposted it as a comment. After that, I stopped getting notification of new activity on this question.I undeleted and updated my previous "answer" with a real solution. Hope that helps.
Isaac Truett
Thanks Isaac!. Your solution is working as I expected.
moorsu