KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > jdbc > exception > NestedSQLException


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.common.jdbc.exception;
17
18 import java.sql.SQLException JavaDoc;
19
20 /**
21  * Class to allow passing an Exception with the original SQLException
22  */

23 public class NestedSQLException extends SQLException JavaDoc {
24
25   private static final String JavaDoc CAUSED_BY = "\nCaused by: ";
26
27   private Throwable JavaDoc cause = null;
28
29   /**
30    * Constructor from java.sql.SQLException
31    * @see java.sql.SQLException
32    * @param msg - the message for the exception
33    */

34   public NestedSQLException(String JavaDoc msg) {
35     super(msg);
36   }
37
38   /**
39    * Constructor from java.sql.SQLException
40    * @see java.sql.SQLException
41    * @param reason - the reason for the exception
42    * @param SQLState - the SQLState
43    */

44   public NestedSQLException(String JavaDoc reason, String JavaDoc SQLState) {
45     super(reason, SQLState);
46   }
47
48   /**
49    * Constructor from java.sql.SQLException
50    * @see java.sql.SQLException
51    * @param reason - the reason for the exception
52    * @param SQLState - the SQLState
53    * @param vendorCode - a vendor supplied code to go w/ the message
54    */

55   public NestedSQLException(String JavaDoc reason, String JavaDoc SQLState, int vendorCode) {
56     super(reason, SQLState, vendorCode);
57   }
58
59   /**
60    * Constructor from java.sql.SQLException with added nested exception
61    * @param msg - the message for the exception
62    * @param cause - the cause of the exception
63    */

64   public NestedSQLException(String JavaDoc msg, Throwable JavaDoc cause) {
65     super(msg);
66     this.cause = cause;
67   }
68
69   /**
70    * Constructor from java.sql.SQLException with added nested exception
71    * @see java.sql.SQLException
72    * @param reason - the reason for the exception
73    * @param SQLState - the SQLState
74    * @param cause - the cause of the exception
75    */

76   public NestedSQLException(String JavaDoc reason, String JavaDoc SQLState, Throwable JavaDoc cause) {
77     super(reason, SQLState);
78     this.cause = cause;
79   }
80
81   /**
82    * Constructor from java.sql.SQLException with added nested exception
83    * @param reason - the reason for the exception
84    * @param SQLState - the SQLState
85    * @param vendorCode - a vendor supplied code to go w/ the message
86    * @param cause - the cause of the exception
87    */

88   public NestedSQLException(String JavaDoc reason, String JavaDoc SQLState, int vendorCode, Throwable JavaDoc cause) {
89     super(reason, SQLState, vendorCode);
90     this.cause = cause;
91   }
92
93   /**
94    * Constructor from java.sql.SQLException with added nested exception
95    * @param cause - the cause of the exception
96    */

97   public NestedSQLException(Throwable JavaDoc cause) {
98     super();
99     this.cause = cause;
100   }
101
102   /**
103    * Gets the causing exception, if any.
104    */

105   public Throwable JavaDoc getCause() {
106     return cause;
107   }
108
109   public String JavaDoc toString() {
110     if (cause == null) {
111       return super.toString();
112     } else {
113       return super.toString() + CAUSED_BY + cause.toString();
114     }
115   }
116
117   public void printStackTrace() {
118     super.printStackTrace();
119     if (cause != null) {
120       System.err.println(CAUSED_BY);
121       cause.printStackTrace();
122     }
123   }
124
125   public void printStackTrace(java.io.PrintStream JavaDoc ps) {
126     super.printStackTrace(ps);
127     if (cause != null) {
128       ps.println(CAUSED_BY);
129       cause.printStackTrace(ps);
130     }
131   }
132
133   public void printStackTrace(java.io.PrintWriter JavaDoc pw) {
134     super.printStackTrace(pw);
135     if (cause != null) {
136       pw.println(CAUSED_BY);
137       cause.printStackTrace(pw);
138     }
139   }
140
141
142 }
143
Popular Tags