001package arcommon.exception; 002 003import java.lang.Thread.UncaughtExceptionHandler; 004 005public class ExceptionUtil { 006 public static void installStandardGuiExceptionHandler() { 007 UncaughtExceptionHandler uncaughtExceptionHandler = new MyUncaughtExceptionHandler(true); 008 installExceptionHandler(uncaughtExceptionHandler); 009 } 010 011 public static void installStandardTuiExceptionHandler() { 012 UncaughtExceptionHandler uncaughtExceptionHandler = new MyUncaughtExceptionHandler(false); 013 installExceptionHandler(uncaughtExceptionHandler); 014 } 015 016 public static void installExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) { 017 UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); 018 if (defaultUncaughtExceptionHandler != null) { 019 throw new RuntimeException("Thread.getDefaultUncaughtExceptionHandler() != null " + defaultUncaughtExceptionHandler); 020 } 021 Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler); 022 } 023 024 public static void printLogAndShowException(Throwable throwable) { 025 printLogAndShowException(throwable, null); 026 } 027 028 public static void printLogAndShowException(Throwable throwable, String title) { 029 printLogAndShowException(new ExceptionData(throwable), title); 030 } 031 032 public static void printLogAndShowException(Thread thread, Throwable throwable, String title) { 033 printLogAndShowException(new ExceptionData(thread, throwable), title); 034 } 035 036 public static void printLogAndShowException(ExceptionData exceptionData, String title) { 037 printAndLogException(exceptionData); 038 039 ExceptionViewDialog.viewException(exceptionData, title); 040 } 041 042 public static void printException(Throwable throwable) { 043 System.err.println(exceptionToString(throwable)); 044 } 045 046 public static void printAndLogException(Throwable throwable) { 047 printAndLogException(new ExceptionData(throwable)); 048 } 049 050 public static void printAndLogException(ExceptionData exceptionData) { 051 System.err.println(exceptionData.exceptionStackToString(true)); 052 053 if (MyLog.logFileName != null) { 054 MyLog.logException(exceptionData); 055 } 056 } 057 058 public static void printStack() { 059 printStack(null); 060 } 061 062 public static void printStack(String text) { 063 if (text != null) { 064 System.err.println(text); 065 } 066 Thread.dumpStack(); 067 } 068 069 public static String stackToString() { 070 return stackToString(3); 071 } 072 073 public static String stackToString(int stackOffset) { 074 StackTraceElement[] stackToStackTraceElements = stackToStackTraceElements(); 075 return exceptionStackToString(stackToStackTraceElements, stackOffset, stackToStackTraceElements.length); 076 } 077 078 public static StackTraceElement[] stackToStackTraceElements() { 079 return new Exception().getStackTrace(); // HACK!! 080 } 081 082 public static String exceptionToString(Throwable e) { 083 String result = e.toString() + System.getProperty("line.separator"); 084 result += exceptionStackToString(e); 085 if (e.getCause() != null) { 086 result += System.getProperty("line.separator") + exceptionToString(e.getCause()); 087 } 088 return result; 089 } 090 091 public static String exceptionStackToString(Throwable e) { 092 return exceptionStackToString(e.getStackTrace()); 093 } 094 095 private static String exceptionStackToString(StackTraceElement[] stackTraceElements) { 096 return exceptionStackToString(stackTraceElements, 0, stackTraceElements.length); 097 } 098 099 public static String exceptionStackToString(StackTraceElement[] stackTraceElements, int fromIndex, int toIndex) { 100 StringBuilder result = new StringBuilder(); 101 if (stackTraceElements != null) { 102 for (int i = fromIndex; i < toIndex; i++) { 103 if (result.length() > 0) { 104 result.append(System.getProperty("line.separator")); 105 } 106 result.append("\tat "); 107 result.append(stackTraceElements[i]); 108 } 109 } 110 return result.toString(); 111 } 112}