1

I am trying to make a the code that will catch a PhoneNumberInUseException that is thrown by the ServiceImpl however, the code below always go for the else statement.

@Override
            public void onFailure(Throwable caught) {
                Window.alert("Failed create account.");
                if (caught instanceof PhoneNumberInUseException) {
                    Window.alert("Sorry, but the phone number is already used.");
                } else {
                    Window.alert(caught.toString());
                }

            }

The PhoneNumberInUseException extends RuntimeException

Right now I am just showing this through Window.alert, however I will be doing client-side logic for handling the exception, that is why I can't simply use IllegalArgumentException which works just fine passing exception string from server to client.

Am I missing something?

0

2 Answers 2

3

If your PhoneNumberInUseException class looks something like this:

public class PhoneNumberInUseException extends RuntimeException {

    public PhoneNumberInUseException() {
        super();
    }
}

and you're throwing it in the service like this:

throw new PhoneNumberInUseException();

then your code should be firing correctly. That is, of course, assuming your service has declared that it throws PhoneNumberInUseException, like this:

public interface SomeService extends RemoteService {
    void doSomething(Object someObject) throws PhoneNumberInUseException;
}

public interface SomeServiceAsync {
    void doSomething(Object someObject, AsyncCallback callback);
}

public class SomeServiceImpl extends RemoteServiceServlet implements SomService {
    public void doSomething(Object someObject) throws PhoneNumberInUseException {
        if(somethingHappened) {
            throw new PhoneNumberInUseException();
        } else {
            doSomethingCool();
        }
    }
}

If after you've made sure everything should be working correctly, you might want to put a breakpoint on the line

if (caught instanceof PhoneNumberInUseException)

and figure out what the class of caught is. If it's not PhoneNumberInUseException, you probably haven't read the documentation very well.

Sign up to request clarification or add additional context in comments.

Comments

0

I had to extend PhoneNumnberInUseException from IllegalArgumentException instead of just Exception and make it Serializable, I don't know if this is the correct approach though:

public class PhoneNumnberInUseException extends IllegalArgumentException implements Serializable{
    public PhoneNumnberInUseException(){
        super();
    }
    public PhoneNumnberInUseException(String msg) {
        super(msg);
    }
}

2 Comments

Realistically, you could have kept your code exactly as it was and just declared that your RPC method throws that exception: public void doSomething(Object someObject) throws PhoneNumberInUseException;. GWT would only propagate a RuntimeException to the client if it had been explicitly declared via throws.
@ChrisCashwell I see, the reason I used RuntimeException is because I need to throw exception inside a if-else statement, whereas if I extend from Exception I'm getting syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.