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 hidden function error.
72 class HiddenFuncException : Exception
76 super( "Hidden method called for " ~ ci.name );
82 * Thrown on an out of memory error.
84 class OutOfMemoryException : Exception
86 this( string file, size_t line )
88 super( "Memory allocation failed", file, line );
91 override string toString()
93 return msg ? super.toString() : "Memory allocation failed";
99 * Thrown on a switch error.
101 class SwitchException : Exception
103 this( string file, size_t line )
105 super( "No appropriate switch clause found", file, line );
111 * Thrown on a unicode conversion error.
113 class UnicodeException : Exception
117 this( string msg, size_t idx )
125 ///////////////////////////////////////////////////////////////////////////////
127 ///////////////////////////////////////////////////////////////////////////////
131 * Overrides the default assert hander with a user-supplied version.
134 * h = The new assert handler. Set to null to use the default handler.
136 void setAssertHandler( assertHandlerType h )
142 ///////////////////////////////////////////////////////////////////////////////
143 // Overridable Callbacks
144 ///////////////////////////////////////////////////////////////////////////////
148 * A callback for assert errors in D. The user-supplied assert handler will
149 * be called if one has been supplied, otherwise an AssertException will be
153 * file = The name of the file that signaled this error.
154 * line = The line number on which this error occurred.
156 extern (C) void onAssertError( string file, size_t line )
158 if( assertHandler is null )
159 throw new AssertException( file, line );
160 assertHandler( file, line );
165 * A callback for assert errors in D. The user-supplied assert handler will
166 * be called if one has been supplied, otherwise an AssertException will be
170 * file = The name of the file that signaled this error.
171 * line = The line number on which this error occurred.
172 * msg = An error message supplied by the user.
174 extern (C) void onAssertErrorMsg( string file, size_t line, string msg )
176 if( assertHandler is null )
177 throw new AssertException( msg, file, line );
178 assertHandler( file, line, msg );
182 ///////////////////////////////////////////////////////////////////////////////
183 // Internal Error Callbacks
184 ///////////////////////////////////////////////////////////////////////////////
188 * A callback for array bounds errors in D. An ArrayBoundsException will be
192 * file = The name of the file that signaled this error.
193 * line = The line number on which this error occurred.
196 * ArrayBoundsException.
198 extern (C) void onArrayBoundsError( string file, size_t line )
200 throw new ArrayBoundsException( file, line );
205 * A callback for finalize errors in D. A FinalizeException will be thrown.
208 * e = The exception thrown during finalization.
213 extern (C) void onFinalizeError( ClassInfo info, Exception ex )
215 throw new FinalizeException( info, ex );
220 * A callback for hidden function errors in D. A HiddenFuncException will be
224 * HiddenFuncException.
226 extern (C) void onHiddenFuncError( Object o )
228 throw new HiddenFuncException( o.classinfo );
233 * A callback for out of memory errors in D. An OutOfMemoryException will be
237 * OutOfMemoryException.
239 extern (C) void onOutOfMemoryError()
241 // NOTE: Since an out of memory condition exists, no allocation must occur
242 // while generating this object.
243 throw cast(OutOfMemoryException) cast(void*) OutOfMemoryException.classinfo.init;
248 * A callback for switch errors in D. A SwitchException will be thrown.
251 * file = The name of the file that signaled this error.
252 * line = The line number on which this error occurred.
257 extern (C) void onSwitchError( string file, size_t line )
259 throw new SwitchException( file, line );
264 * A callback for unicode errors in D. A UnicodeException will be thrown.
267 * msg = Information about the error.
268 * idx = String index where this error was detected.
273 extern (C) void onUnicodeError( string msg, size_t idx )
275 throw new UnicodeException( msg, idx );