View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
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    *    https://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;
19  
20  /**
21   * Unchecked exception to allow passing an Exception with the original SQLException.
22   */
23  public class RuntimeSQLException extends RuntimeException {
24  
25    /** The Constant serialVersionUID. */
26    private static final long serialVersionUID = 1L;
27  
28    /**
29     * Default constructor.
30     */
31    public RuntimeSQLException() {
32    }
33  
34    /**
35     * Constructor to pass along a message.
36     *
37     * @param msg
38     *          - the message
39     */
40    public RuntimeSQLException(String msg) {
41      super(msg);
42    }
43  
44    /**
45     * Constructor to pass along another exception.
46     *
47     * @param sqlException
48     *          - the exception
49     */
50    public RuntimeSQLException(SQLException sqlException) {
51      super(sqlException);
52    }
53  
54    /**
55     * Constructor to pass along a message and an exception.
56     *
57     * @param msg
58     *          - the message
59     * @param sqlException
60     *          - the exception
61     */
62    public RuntimeSQLException(String msg, SQLException sqlException) {
63      super(msg, sqlException);
64    }
65  
66    /**
67     * Getter for the SQL State.
68     *
69     * @return - the state
70     */
71    public String getSQLState() {
72      Throwable cause = getCause();
73      if (cause instanceof SQLException) {
74        return ((SQLException) cause).getSQLState();
75      }
76      return null;
77  
78    }
79  
80    /**
81     * Getter for the error code.
82     *
83     * @return - the error code
84     */
85    public int getErrorCode() {
86      Throwable cause = getCause();
87      if (cause instanceof SQLException) {
88        return ((SQLException) cause).getErrorCode();
89      }
90      return -1;
91    }
92  
93    /**
94     * Get the next exception in the chain.
95     *
96     * @return - the next exception
97     */
98    public SQLException getNextException() {
99      Throwable cause = getCause();
100     if (cause instanceof SQLException) {
101       return ((SQLException) cause).getNextException();
102     }
103     return null;
104   }
105 
106   /**
107    * Set the next exception in the chain.
108    *
109    * @param ex
110    *          - the next exception
111    */
112   public synchronized void setNextException(SQLException ex) {
113     Throwable cause = getCause();
114     if (cause instanceof SQLException) {
115       ((SQLException) cause).setNextException(ex);
116     }
117   }
118 
119 }