自定义异常:
----------------------------------------------
//使用方法
throw new AppException("token过期了");
----------------------------------------------
//AppException.java
package app;
public class AppException extends RuntimeException {
private static final long serialVersionUID = 1L;
public AppException() {
super();
}
public AppException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public AppException(String message, Throwable cause) {
super(message, cause);
}
public AppException(String message) {
super(message);
}
public AppException(Throwable cause) {
super(cause);
}
}
----------------------------------------------
//ExceptionInterceptor.java
package app;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class ExceptionInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
try {
inv.invoke();
} catch (Exception e) {
if (e instanceof AppException) {
inv.getController().renderJson(Ret.fail(e.getMessage()));
} else {
throw e;
}
}
}
}
----------------------------------------------
//JFinalConfig
@Override
public void configInterceptor(Interceptors me) {
me.add(new ExceptionInterceptor());
}
----------------------------------------------