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 a range error.
23 class RangeError : Error
25 this( string file, size_t line )
27 super( "Range violation", file, line );
33 * Thrown on an assert error.
35 class AssertError : Error
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 FinalizeError : Error
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 HiddenFuncError : Error
76 super( "Hidden method called for " ~ ci.name );
82 * Thrown on an out of memory error.
84 class OutOfMemoryError : Error
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 SwitchError : Error
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 AssertError will be thrown.
152 * file = The name of the file that signaled this error.
153 * line = The line number on which this error occurred.
155 extern (C) void onAssertError( string file, size_t line )
157 if( assertHandler is null )
158 throw new AssertError( file, line );
159 assertHandler( file, line );
164 * A callback for assert errors in D. The user-supplied assert handler will
165 * be called if one has been supplied, otherwise an AssertError will be thrown.
168 * file = The name of the file that signaled this error.
169 * line = The line number on which this error occurred.
170 * msg = An error message supplied by the user.
172 extern (C) void onAssertErrorMsg( string file, size_t line, string msg )
174 if( assertHandler is null )
175 throw new AssertError( msg, file, line );
176 assertHandler( file, line, msg );
180 ///////////////////////////////////////////////////////////////////////////////
181 // Internal Error Callbacks
182 ///////////////////////////////////////////////////////////////////////////////
186 * A callback for array bounds errors in D. A RangeError will be thrown.
189 * file = The name of the file that signaled this error.
190 * line = The line number on which this error occurred.
195 extern (C) void onRangeError( string file, size_t line )
197 throw new RangeError( file, line );
202 * A callback for finalize errors in D. A FinalizeError will be thrown.
205 * e = The exception thrown during finalization.
210 extern (C) void onFinalizeError( ClassInfo info, Exception ex )
212 throw new FinalizeError( info, ex );
217 * A callback for hidden function errors in D. A HiddenFuncError will be
223 extern (C) void onHiddenFuncError( Object o )
225 throw new HiddenFuncError( o.classinfo );
230 * A callback for out of memory errors in D. An OutOfMemoryError will be
236 extern (C) void onOutOfMemoryError()
238 // NOTE: Since an out of memory condition exists, no allocation must occur
239 // while generating this object.
240 throw cast(OutOfMemoryError) cast(void*) OutOfMemoryError.classinfo.init;
245 * A callback for switch errors in D. A SwitchError will be thrown.
248 * file = The name of the file that signaled this error.
249 * line = The line number on which this error occurred.
254 extern (C) void onSwitchError( string file, size_t line )
256 throw new SwitchError( file, line );
261 * A callback for unicode errors in D. A UnicodeException will be thrown.
264 * msg = Information about the error.
265 * idx = String index where this error was detected.
270 extern (C) void onUnicodeError( string msg, size_t idx )
272 throw new UnicodeException( msg, idx );