1 30 package com.genimen.djeneric.repository.exceptions; 31 32 import java.io.PrintStream ; 33 import java.io.PrintWriter ; 34 import java.io.StringWriter ; 35 36 public class DjenericException extends Exception 37 { 38 private static final long serialVersionUID = 4123101775394911796L; 39 40 private final static String CHAINED_MSG = "Chained to: "; 41 42 private String _stackTrace = null; 43 private transient boolean _ignoreMyOwnStack = false; 44 private Throwable _chained = null; 45 46 public DjenericException() 47 { 48 } 49 50 public DjenericException(Throwable chainThis) 51 { 52 super(chainThis.getMessage()); 53 storeStack(chainThis); 54 } 55 56 public DjenericException(String message) 57 { 58 super(message); 59 } 60 61 public Throwable getRootException() 62 { 63 if (_chained instanceof DjenericException && _chained != this) 64 { 65 DjenericException dje = (DjenericException) _chained; 66 return dje.getRootException(); 67 } 68 if (_chained == null) return this; 69 return _chained; 70 } 71 72 private void storeStack(Throwable theEx) 73 { 74 _chained = theEx; 75 _stackTrace = stack2String(theEx); 76 _ignoreMyOwnStack = true; 77 } 78 79 public void printStackTrace() 80 { 81 if (_stackTrace != null) 82 { 83 System.err.println(_stackTrace); 84 if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG); 85 } 86 if (!_ignoreMyOwnStack) super.printStackTrace(); 87 } 88 89 public void printStackTrace(PrintStream printStream) 90 { 91 if (_stackTrace != null) 92 { 93 printStream.println(_stackTrace); 94 if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG); 95 } 96 if (!_ignoreMyOwnStack) super.printStackTrace(printStream); 97 } 98 99 public void printStackTrace(PrintWriter writer) 100 { 101 if (_stackTrace != null) 102 { 103 writer.println(_stackTrace); 104 if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG); 105 } 106 if (!_ignoreMyOwnStack) super.printStackTrace(writer); 107 } 108 109 protected String getChainedClassName() 110 { 111 if (_chained != null) 112 { 113 if (_chained instanceof DjenericException) return getClass().getName() + "\n" 114 + ((DjenericException) _chained).getChainedClassName(); 115 else return getClass().getName() + "\n" + _chained.getClass().getName(); 116 } 117 return getClass().getName(); 118 } 119 120 public String toString() 121 { 122 String exceptionMessage = getChainedClassName() + ": "; 123 124 if (getMessage() != null) exceptionMessage += getMessage() + "\n"; 125 return exceptionMessage.trim(); 126 } 127 128 private String stack2String(Throwable x) 129 { 130 if (x == null) return ""; 131 132 StringWriter stringWriter = new StringWriter (); 133 java.io.PrintWriter stackTrace = new java.io.PrintWriter (stringWriter); 134 x.printStackTrace(stackTrace); 135 String result = stringWriter.toString().trim(); 136 if (result.length() == 0) result = null; 137 138 return result; 139 } 140 141 public static String getNiceMessage(Exception x) 142 { 143 String msg = x.getMessage(); 144 if (x instanceof NumberFormatException ) 145 { 146 msg = "Invalid number entered"; 147 } 148 return msg; 149 } 150 151 } | Popular Tags |