1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * SmallSQLException.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | import java.sql.*; |
36 | import java.io.*; |
37 | |
38 | /** |
39 | * @author Volker Berlin |
40 | * |
41 | */ |
42 | class SmallSQLException extends SQLException { |
43 | |
44 | Throwable throwable; |
45 | boolean isInit; |
46 | |
47 | /** |
48 | * |
49 | */ |
50 | public SmallSQLException(Throwable throwable) { |
51 | super("[SmallSQL]" + getMsg(throwable)); |
52 | this.throwable = throwable; |
53 | init(); |
54 | } |
55 | public SmallSQLException(String msg) { |
56 | super("[SmallSQL]" + msg); |
57 | init(); |
58 | } |
59 | |
60 | /* |
61 | public SS_SQLException(String arg0, String arg1) { |
62 | super(arg0, arg1); |
63 | init(); |
64 | } |
65 | */ |
66 | |
67 | private void init(){ |
68 | this.isInit = true; |
69 | PrintStream ps = DriverManager.getLogStream(); |
70 | if(ps != null) this.printStackTrace(ps); |
71 | } |
72 | |
73 | private static String getMsg(Throwable throwable) { |
74 | String msg = throwable.getMessage(); |
75 | if(msg == null || msg.length() < 20){ |
76 | String msg2 = throwable.getClass().getName(); |
77 | msg2 = msg2.substring(msg2.lastIndexOf('.')+1); |
78 | if(msg != null) |
79 | msg2 = msg2 + ':' + msg; |
80 | return msg2; |
81 | } |
82 | return throwable.getMessage(); |
83 | } |
84 | |
85 | /** |
86 | * @param arg0 |
87 | * @param arg1 |
88 | * @param arg2 |
89 | */ |
90 | public SmallSQLException(String arg0, String arg1, int arg2) { |
91 | super(arg0, arg1, arg2); |
92 | // TODO Auto-generated constructor stub |
93 | } |
94 | |
95 | public void printStackTrace(){ |
96 | if(!isInit) return; |
97 | if(throwable == null) |
98 | super.printStackTrace(); |
99 | else{ |
100 | System.err.println(toString()); |
101 | throwable.printStackTrace(); |
102 | } |
103 | } |
104 | |
105 | public void printStackTrace(PrintStream ps){ |
106 | if(!isInit) return; |
107 | if(throwable == null) |
108 | super.printStackTrace(ps); |
109 | else{ |
110 | ps.println(toString()); |
111 | throwable.printStackTrace(ps); |
112 | } |
113 | } |
114 | |
115 | public void printStackTrace(PrintWriter pw){ |
116 | if(!isInit) return; |
117 | if(throwable == null) |
118 | super.printStackTrace(pw); |
119 | else{ |
120 | pw.println(toString()); |
121 | throwable.printStackTrace(pw); |
122 | } |
123 | } |
124 | |
125 | } |