Skip to content

Instantly share code, notes, and snippets.

@spg
Last active August 29, 2015 14:15
Show Gist options
  • Save spg/1f22a1d4c3dafc23d67d to your computer and use it in GitHub Desktop.
Save spg/1f22a1d4c3dafc23d67d to your computer and use it in GitHub Desktop.
Exception handling example
// MyActionHandler throws an exception. Note that we're throwing a custom exception
public class MyActionHandler extends AbstractAction<MyAction, MyActionResult> {
@Override
public MyActionResult execute(MyAction action, ExecutionContext context) throws ActionException {
if (somethingGoesWrong()) {
throw new MyCustomException("This is my custom message");
}
// everything ok
}
}
// MyCustomException has to be serializable, have a no-arg public constructor, and reside in shared (client + server) path
package com.mycompany.shared;
public class MyCustomException extends ActionException implements Serializable {
public MyCustomException() {
}
public MyCustomException(String msg) {
super(msg);
}
}
// on the client side, switch between error classes and handle them accordingly
dispatchAsync.execute(new MyAction(), new AsyncCallbackImpl<MyActionResult>() {
@Override
public void onReturn(MyActionResult result) {
// everything ok
}
@Override
public void onFailure(Throwable caught) {
try {
thrown caught;
} catch (MyCustomException e) {
handleMyCustomException(e); // possibly display the error message
} catch (AnotherExceptionType e) {
// do something else for this particular exception type
} catch (Throwable e) {
// last resort, something unexpected happened
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment