001: /*
002: * Copyright 2004-2006 Fouad HAMDI.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.csvbeans;
017:
018: import java.io.PrintStream;
019: import java.io.PrintWriter;
020:
021: /**
022: * Generic exception class for the entire library.
023: *
024: * @author Fouad Hamdi
025: * @since 0.5
026: */
027: public class CSVBeansException extends Exception {
028: /**
029: * Error code.
030: */
031: private String code;
032:
033: /**
034: * Underlying error.
035: */
036: private Throwable underlyingException;
037:
038: /**
039: * Constructor.
040: * @param message the error message
041: * @param t the underlying exception
042: */
043: public CSVBeansException(String message, Throwable t) {
044: this (null, message, t);
045: }
046:
047: /**
048: * Constructor.
049: * @param code identifies the message type
050: * @param message the error message
051: */
052: public CSVBeansException(String code, String message) {
053: this (code, message, null);
054: }
055:
056: /**
057: * Constructor.
058: * @param code the message type
059: * @param message error message
060: * @param e underlying error
061: */
062: public CSVBeansException(String code, String message, Throwable t) {
063: super (message);
064: this .code = code;
065: this .underlyingException = t;
066: }
067:
068: /**
069: * @return Returns the code.
070: */
071: public String getErrorCode() {
072: return code;
073: }
074:
075: /**
076: * @return Returns the underlying error.
077: */
078: public Throwable getUnderlyingException() {
079: return underlyingException;
080: }
081:
082: /**
083: * Allow to know if there is an underlying exception.
084: */
085: public boolean hasUnderlyingException() {
086: return underlyingException != null;
087: }
088:
089: /**
090: * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
091: */
092: public void printStackTrace(PrintWriter s) {
093: super .printStackTrace(s);
094: if (hasUnderlyingException()) {
095: underlyingException.printStackTrace(s);
096: }
097: }
098:
099: /* (non-Javadoc)
100: * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
101: */
102: public void printStackTrace(PrintStream s) {
103: super.printStackTrace(s);
104: if (hasUnderlyingException()) {
105: underlyingException.printStackTrace(s);
106: }
107: }
108: }
|