📄 exceptionhandle.java
字号:
package util;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ExceptionHandle {
private static Log logger = LogFactory.getLog(ExceptionHandle.class);
/**
* Exception handle
* @param e
* @param exceptionSourceClass 异常是从哪一个类里抛出来的
*/
public static void doException(Throwable e, Class exceptionSourceClass) {
if (Display.getCurrent() != null){
doException (Display.getCurrent().getActiveShell(), e, exceptionSourceClass);
} else {
logger.error(getExceptionStatckTrace(e));
}
}
public static void doException(Throwable e){
doException(e, null);
}
/**
* Exception handle
* @param strMessage
*/
public static void doException(String strMessage) {
if (logger.isWarnEnabled()){
logger.warn(strMessage);
}
MessageDialog.openWarning(Display.getCurrent().getActiveShell(),"错误",strMessage);
}
/**
* Exception handle
* @param e
*/
public static void doUnknownException(Exception e) {
if (logger.isWarnEnabled()){
String exeptionInfo = getExceptionStatckTrace(e);
logger.warn(exeptionInfo);
}
MessageDialog.openError(Display.getCurrent().getActiveShell(),"错误","系统错误");
}
/**
* Exception handle
*
* @param shell
* @param e
*/
public static void doException(Shell shell, Throwable e) {
doException(shell, e, (Class)null);
}
public static void doException(Shell shell, Throwable e, Class exceptionSourceClass){
doException(shell, "错误", e, exceptionSourceClass);
}
/**
* Exception handle
* @param shell
* @param title
* @param e
*/
public static void doException(Shell shell, String title, Throwable e, Class exceptionSource) {
if (e == null) {
return;
}
Dialog.openErrorDialog(shell, title, "系统错误", e, exceptionSource);
}
public static boolean isExceptionStatckHasCause(Throwable e, Class causeType){
Throwable cause = e;
if (e == null || causeType == null)
return false;
while (cause != null){
if (cause.getClass() == causeType)
return true;
cause = cause.getCause();
}
return false;
}
public static Throwable getStackCause(Throwable e, Class requireType){
Throwable cause = e;
if (e == null || requireType == null)
return null;
while (cause != null){
if (cause.getClass() == requireType)
return cause;
cause = cause.getCause();
}
return null;
}
public static String getExceptionStatckTrace(Throwable e) {
if (e == null)
return "";
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -