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 error is thrown whenever an unrecoverable 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: * throwable that caused the problem (if this information is
020: * available (i.e. it may be null)).
021: * <p>
022: * SWTErrors are thrown when something fails internally which
023: * either leaves SWT in an unknown state (eg. the o/s call to
024: * remove an item from a list returns an error code) or when SWT
025: * is left in a known-to-be-unrecoverable state (eg. it runs out
026: * of callback resources). SWTErrors should not occur in typical
027: * programs, although "high reliability" applications should
028: * still catch them.
029: * </p><p>
030: * This class also provides support methods used by SWT to match
031: * error codes to the appropriate exception class (SWTError,
032: * SWTException, or IllegalArgumentException) and to provide
033: * human readable strings for SWT error codes.
034: * </p>
035: *
036: * @see SWTException
037: * @see SWT#error(int)
038: */
039:
040: public class SWTError extends Error {
041: /**
042: * The SWT error code, one of SWT.ERROR_*.
043: */
044: public int code;
045:
046: /**
047: * The underlying throwable that caused the problem,
048: * or null if this information is not available.
049: */
050: public Throwable throwable;
051:
052: static final long serialVersionUID = 3833467327105808433L;
053:
054: /**
055: * Constructs a new instance of this class with its
056: * stack trace filled in. The error code is set to an
057: * unspecified value.
058: */
059: public SWTError() {
060: this (SWT.ERROR_UNSPECIFIED);
061: }
062:
063: /**
064: * Constructs a new instance of this class with its
065: * stack trace and message filled in. The error code is
066: * set to an unspecified value. Specifying <code>null</code>
067: * as the message is equivalent to specifying an empty string.
068: *
069: * @param message the detail message for the exception
070: */
071: public SWTError(String message) {
072: this (SWT.ERROR_UNSPECIFIED, message);
073: }
074:
075: /**
076: * Constructs a new instance of this class with its
077: * stack trace and error code filled in.
078: *
079: * @param code the SWT error code
080: */
081: public SWTError(int code) {
082: this (code, SWT.findErrorText(code));
083: }
084:
085: /**
086: * Constructs a new instance of this class with its
087: * stack trace, error code and message filled in.
088: * Specifying <code>null</code> as the message is
089: * equivalent to specifying an empty string.
090: *
091: * @param code the SWT error code
092: * @param message the detail message for the exception
093: */
094: public SWTError(int code, String message) {
095: super (message);
096: this .code = code;
097: }
098:
099: /**
100: * Returns the underlying throwable that caused the problem,
101: * or null if this information is not available.
102: * <p>
103: * NOTE: This method overrides Throwable.getCause() that was
104: * added to JDK1.4. It is necessary to override this method
105: * in order for inherited printStackTrace() methods to work.
106: * </p>
107: * @return the underlying throwable
108: *
109: * @since 3.1
110: */
111: public Throwable getCause() {
112: return throwable;
113: }
114:
115: /**
116: * Returns the string describing this SWTError object.
117: * <p>
118: * It is combined with the message string of the Throwable
119: * which caused this SWTError (if this information is available).
120: * </p>
121: * @return the error message string of this SWTError object
122: */
123: public String getMessage() {
124: if (throwable == null)
125: return super .getMessage();
126: return super .getMessage() + " (" + throwable.toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
127: }
128:
129: /**
130: * Outputs a printable representation of this error's
131: * stack trace on the standard error stream.
132: * <p>
133: * Note: printStackTrace(PrintStream) and printStackTrace(PrintWriter)
134: * are not provided in order to maintain compatibility with CLDC.
135: * </p>
136: */
137: public void printStackTrace() {
138: super .printStackTrace();
139: if (Library.JAVA_VERSION < Library.JAVA_VERSION(1, 4, 0)
140: && throwable != null) {
141: System.err
142: .println("*** Stack trace of contained error ***"); //$NON-NLS-1$
143: throwable.printStackTrace();
144: }
145: }
146:
147: }
|