2 * The exception module defines all system-level exceptions and provides a
3 * mechanism to alter system-level error handling.
5 * Copyright: Copyright (c) 2005-2008, The D Runtime Project
6 * License: BSD Style, see LICENSE
14 alias void function( string file, size_t line, string msg = null ) assertHandlerType;
16 assertHandlerType assertHandler = null;
21 * Thrown on an array bounds error.
23 class ArrayBoundsException : Exception
25 this( string file, size_t line )
27 super( "Array index out of bounds", file, line );
33 * Thrown on an assert error.
35 class AssertException : Exception
37 this( string file, size_t line )
39 super( "Assertion failure", file, line );
42 this( string msg, string file, size_t line )
44 super( msg, file, line );
50 * Thrown on finalize error.
52 class FinalizeException : Exception
56 this( ClassInfo c, Exception e = null )
58 super( "Finalization error", e );
62 override string toString()
64 return "An exception was thrown while finalizing an instance of class " ~ info.name;
70 * Thrown on an out of memory error.
72 class OutOfMemoryException : Exception
74 this( string file, size_t line )
76 super( "Memory allocation failed", file, line );
79 override string toString()
81 return msg ? super.toString() : "Memory allocation failed";
87 * Thrown on a switch error.
89 class SwitchException : Exception
91 this( string file, size_t line )
93 super( "No appropriate switch clause found", file, line );
99 * Thrown on a unicode conversion error.
101 class UnicodeException : Exception
105 this( string msg, size_t idx )
113 ///////////////////////////////////////////////////////////////////////////////
115 ///////////////////////////////////////////////////////////////////////////////
119 * Overrides the default assert hander with a user-supplied version.
122 * h = The new assert handler. Set to null to use the default handler.
124 void setAssertHandler( assertHandlerType h )
130 ///////////////////////////////////////////////////////////////////////////////
131 // Overridable Callbacks
132 ///////////////////////////////////////////////////////////////////////////////
136 * A callback for assert errors in D. The user-supplied assert handler will
137 * be called if one has been supplied, otherwise an AssertException will be
141 * file = The name of the file that signaled this error.
142 * line = The line number on which this error occurred.
144 extern (C) void onAssertError( string file, size_t line )
146 if( assertHandler is null )
147 throw new AssertException( file, line );
148 assertHandler( file, line );
153 * A callback for assert errors in D. The user-supplied assert handler will
154 * be called if one has been supplied, otherwise an AssertException will be
158 * file = The name of the file that signaled this error.
159 * line = The line number on which this error occurred.
160 * msg = An error message supplied by the user.
162 extern (C) void onAssertErrorMsg( string file, size_t line, string msg )
164 if( assertHandler is null )
165 throw new AssertException( msg, file, line );
166 assertHandler( file, line, msg );
170 ///////////////////////////////////////////////////////////////////////////////
171 // Internal Error Callbacks
172 ///////////////////////////////////////////////////////////////////////////////
176 * A callback for array bounds errors in D. An ArrayBoundsException will be
180 * file = The name of the file that signaled this error.
181 * line = The line number on which this error occurred.
184 * ArrayBoundsException.
186 extern (C) void onArrayBoundsError( string file, size_t line )
188 throw new ArrayBoundsException( file, line );
193 * A callback for finalize errors in D. A FinalizeException will be thrown.
196 * e = The exception thrown during finalization.
201 extern (C) void onFinalizeError( ClassInfo info, Exception ex )
203 throw new FinalizeException( info, ex );
208 * A callback for out of memory errors in D. An OutOfMemoryException will be
212 * OutOfMemoryException.
214 extern (C) void onOutOfMemoryError()
216 // NOTE: Since an out of memory condition exists, no allocation must occur
217 // while generating this object.
218 throw cast(OutOfMemoryException) cast(void*) OutOfMemoryException.classinfo.init;
223 * A callback for switch errors in D. A SwitchException will be thrown.
226 * file = The name of the file that signaled this error.
227 * line = The line number on which this error occurred.
232 extern (C) void onSwitchError( string file, size_t line )
234 throw new SwitchException( file, line );
239 * A callback for unicode errors in D. A UnicodeException will be thrown.
242 * msg = Information about the error.
243 * idx = String index where this error was detected.
248 extern (C) void onUnicodeError( string msg, size_t idx )
250 throw new UnicodeException( msg, idx );