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 hidden function error.
35 class HiddenFuncException : Exception
39 super("hidden method called for " ~ ci.name);
45 * Thrown on an assert error.
47 class AssertException : Exception
49 this( string file, size_t line )
51 super( "Assertion failure", file, line );
54 this( string msg, string file, size_t line )
56 super( msg, file, line );
62 * Thrown on finalize error.
64 class FinalizeException : Exception
68 this( ClassInfo c, Exception e = null )
70 super( "Finalization error", e );
74 override string toString()
76 return "An exception was thrown while finalizing an instance of class " ~ info.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 out of memory errors in D. An OutOfMemoryException will be
224 * OutOfMemoryException.
226 extern (C) void onOutOfMemoryError()
228 // NOTE: Since an out of memory condition exists, no allocation must occur
229 // while generating this object.
230 throw cast(OutOfMemoryException) cast(void*) OutOfMemoryException.classinfo.init;
235 * A callback for switch errors in D. A SwitchException will be thrown.
238 * file = The name of the file that signaled this error.
239 * line = The line number on which this error occurred.
244 extern (C) void onSwitchError( string file, size_t line )
246 throw new SwitchException( file, line );
251 * A callback for unicode errors in D. A UnicodeException will be thrown.
254 * msg = Information about the error.
255 * idx = String index where this error was detected.
260 extern (C) void onUnicodeError( string msg, size_t idx )
262 throw new UnicodeException( msg, idx );
265 /********************************************
266 * Called by the compiler generated code.
269 extern (C) void _d_hidden_func()
276 //printf("_d_hidden_func()\n");
277 auto a = new HiddenFuncException(o.classinfo);
278 //printf("assertion %p created\n", a);