001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt;
011:
012: import org.eclipse.swt.internal.*;
013:
014: /**
015: * This runtime exception is thrown whenever a recoverable error
016: * occurs internally in SWT. The message text and error code
017: * provide a further description of the problem. The exception
018: * has a <code>throwable</code> field which holds the underlying
019: * exception that caused the problem (if this information is
020: * available (i.e. it may be null)).
021: * <p>
022: * SWTExceptions are thrown when something fails internally,
023: * but SWT is left in a known stable state (eg. a widget call
024: * was made from a non-u/i thread, or there is failure while
025: * reading an Image because the source file was corrupt).
026: * </p>
027: *
028: * @see SWTError
029: */
030:
031: public class SWTException extends RuntimeException {
032: /**
033: * The SWT error code, one of SWT.ERROR_*.
034: */
035: public int code;
036:
037: /**
038: * The underlying throwable that caused the problem,
039: * or null if this information is not available.
040: */
041: public Throwable throwable;
042:
043: static final long serialVersionUID = 3257282552304842547L;
044:
045: /**
046: * Constructs a new instance of this class with its
047: * stack trace filled in. The error code is set to an
048: * unspecified value.
049: */
050: public SWTException() {
051: this (SWT.ERROR_UNSPECIFIED);
052: }
053:
054: /**
055: * Constructs a new instance of this class with its
056: * stack trace and message filled in. The error code is
057: * set to an unspecified value. Specifying <code>null</code>
058: * as the message is equivalent to specifying an empty string.
059: *
060: * @param message the detail message for the exception
061: */
062: public SWTException(String message) {
063: this (SWT.ERROR_UNSPECIFIED, message);
064: }
065:
066: /**
067: * Constructs a new instance of this class with its
068: * stack trace and error code filled in.
069: *
070: * @param code the SWT error code
071: */
072: public SWTException(int code) {
073: this (code, SWT.findErrorText(code));
074: }
075:
076: /**
077: * Constructs a new instance of this class with its
078: * stack trace, error code and message filled in.
079: * Specifying <code>null</code> as the message is
080: * equivalent to specifying an empty string.
081: *
082: * @param code the SWT error code
083: * @param message the detail message for the exception
084: */
085: public SWTException(int code, String message) {
086: super (message);
087: this .code = code;
088: }
089:
090: /**
091: * Returns the underlying throwable that caused the problem,
092: * or null if this information is not available.
093: * <p>
094: * NOTE: This method overrides Throwable.getCause() that was
095: * added to JDK1.4. It is necessary to override this method
096: * in order for inherited printStackTrace() methods to work.
097: * </p>
098: * @return the underlying throwable
099: *
100: * @since 3.1
101: */
102: public Throwable getCause() {
103: return throwable;
104: }
105:
106: /**
107: * Returns the string describing this SWTException object.
108: * <p>
109: * It is combined with the message string of the Throwable
110: * which caused this SWTException (if this information is available).
111: * </p>
112: * @return the error message string of this SWTException object
113: */
114: public String getMessage() {
115: if (throwable == null)
116: return super .getMessage();
117: return super .getMessage() + " (" + throwable.toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
118: }
119:
120: /**
121: * Outputs a printable representation of this exception's
122: * stack trace on the standard error stream.
123: * <p>
124: * Note: printStackTrace(PrintStream) and printStackTrace(PrintWriter)
125: * are not provided in order to maintain compatibility with CLDC.
126: * </p>
127: */
128: public void printStackTrace() {
129: super .printStackTrace();
130: if (Library.JAVA_VERSION < Library.JAVA_VERSION(1, 4, 0)
131: && throwable != null) {
132: System.err
133: .println("*** Stack trace of contained exception ***"); //$NON-NLS-1$
134: throwable.printStackTrace();
135: }
136: }
137:
138: }
|