DWR入门 (三)处理异常
1.首先要建立一个继承RuntimeException的UserException类。
public class UserException extends RuntimeException
2.在UserService类中修改deleteUser方法:
public void deleteUser(int id){if(!users.containsKey(id)){throw new UserException("删除用户不存在");}users.remove(id);}
function deleteUser(){var uid=$("#did").val();UserService.deleteUser(uid);};<p>删除用户:<input type="text" id="did"/><input type="button" id="deleteUser" value="delete"/></p>
UserService.deleteUser(uid, { callback:function(data){ init();}, errorHandler:function(msg,e){ alert(e.message); //删除用户不存在 console.log(e); //如下图所示 } } );
//定义一个错误处理函数function errorHandler(msg,e){alert(msg);}function deleteUser(){var uid=$("#did").val();UserService.deleteUser(uid,function(data){init();});}; //设置全局异常处理dwr.engine.setErrorHandler(errorHandler);