1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.SWTError; 14 15 import java.lang.all; 16 17 import org.eclipse.swt.SWT; 18 19 20 /** 21 * This error is thrown whenever an unrecoverable error 22 * occurs internally in SWT. The message text and error code 23 * provide a further description of the problem. The exception 24 * has a <code>throwable</code> field which holds the underlying 25 * throwable that caused the problem (if this information is 26 * available (i.e. it may be null)). 27 * <p> 28 * SWTErrors are thrown when something fails internally which 29 * either leaves SWT in an unknown state (eg. the o/s call to 30 * remove an item from a list returns an error code) or when SWT 31 * is left in a known-to-be-unrecoverable state (eg. it runs out 32 * of callback resources). SWTErrors should not occur in typical 33 * programs, although "high reliability" applications should 34 * still catch them. 35 * </p><p> 36 * This class also provides support methods used by SWT to match 37 * error codes to the appropriate exception class (SWTError, 38 * SWTException, or IllegalArgumentException) and to provide 39 * human readable strings for SWT error codes. 40 * </p> 41 * 42 * @see SWTException 43 * @see SWT#error(int) 44 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 45 */ 46 47 public class SWTError : PlatformException { 48 /** 49 * The SWT error code, one of SWT.ERROR_*. 50 */ 51 public int code; 52 53 /** 54 * The underlying throwable that caused the problem, 55 * or null if this information is not available. 56 */ 57 public Throwable throwable( Throwable e ){ 58 this.next = e; 59 return this.next; 60 } 61 public Throwable throwable(){ 62 return this.next; 63 } 64 65 //static const long serialVersionUID = 3833467327105808433L; 66 67 /** 68 * Constructs a new instance of this class with its 69 * stack trace filled in. The error code is set to an 70 * unspecified value. 71 */ 72 public this () { 73 this (SWT.ERROR_UNSPECIFIED); 74 } 75 76 /** 77 * Constructs a new instance of this class with its 78 * stack trace and message filled in. The error code is 79 * set to an unspecified value. Specifying <code>null</code> 80 * as the message is equivalent to specifying an empty string. 81 * 82 * @param message the detail message for the exception 83 */ 84 public this (String message) { 85 this (SWT.ERROR_UNSPECIFIED, message); 86 } 87 88 /** 89 * Constructs a new instance of this class with its 90 * stack trace and error code filled in. 91 * 92 * @param code the SWT error code 93 */ 94 public this (int code) { 95 this (code, SWT.findErrorText (code)); 96 } 97 98 /** 99 * Constructs a new instance of this class with its 100 * stack trace, error code and message filled in. 101 * Specifying <code>null</code> as the message is 102 * equivalent to specifying an empty string. 103 * 104 * @param code the SWT error code 105 * @param message the detail message for the exception 106 */ 107 public this (int code, String message) { 108 super (message); 109 this.code = code; 110 } 111 112 /** 113 * Returns the underlying throwable that caused the problem, 114 * or null if this information is not available. 115 * <p> 116 * NOTE: This method overrides Throwable.getCause() that was 117 * added to JDK1.4. It is necessary to override this method 118 * in order for inherited printStackTrace() methods to work. 119 * </p> 120 * @return the underlying throwable 121 * 122 * @since 3.1 123 */ 124 public Throwable getCause() { 125 return throwable; 126 } 127 128 /** 129 * Returns the string describing this SWTError object. 130 * <p> 131 * It is combined with the message string of the Throwable 132 * which caused this SWTError (if this information is available). 133 * </p> 134 * @return the error message string of this SWTError object 135 */ 136 public String getMessage () { 137 if (throwable is null) 138 return super.toString(); 139 return super.toString () ~ " (" ~ throwable.toString () ~ ")"; //$NON-NLS-1$ //$NON-NLS-2$ 140 } 141 142 /** 143 * Outputs a printable representation of this error's 144 * stack trace on the standard error stream. 145 * <p> 146 * Note: printStackTrace(PrintStream) and printStackTrace(PrintWriter) 147 * are not provided in order to maintain compatibility with CLDC. 148 * </p> 149 */ 150 public void printStackTrace () { 151 getDwtLogger().error( __FILE__, __LINE__, "stacktrace follows (if feature compiled in)" ); 152 foreach( msg; info ){ 153 getDwtLogger().error( __FILE__, __LINE__, "{}", msg ); 154 } 155 if ( throwable !is null) { 156 getDwtLogger().error ( __FILE__, __LINE__, "*** Stack trace of contained error ***"); //$NON-NLS-1$ 157 foreach( msg; throwable.info ){ 158 getDwtLogger().error( __FILE__, __LINE__, "{}", msg ); 159 } 160 } 161 } 162 163 }