2 * exception.c: Exception handling
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Dick Porter (dick@ximian.com)
8 * Miguel de Icaza (miguel@ximian.com)
10 * (C) 2001, 2002 Ximian, Inc.
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/appdomain.h>
19 * mono_exception_from_name:
20 * @image: the Mono image where to look for the class
21 * @name_space: the namespace for the class
24 * Creates an exception of the given namespace/name class.
26 * Returns: the initialized exception instance.
29 mono_exception_from_name (MonoImage *image, const char *name_space,
32 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
36 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
37 const char* name_space, const char *name)
42 klass = mono_class_from_name (image, name_space, name);
44 o = mono_object_new (domain, klass);
47 mono_runtime_object_init (o);
49 return (MonoException *)o;
54 * mono_exception_from_token:
55 * @image: the Mono image where to look for the class
56 * @token: The type token of the class
58 * Creates an exception of the type given by @token.
60 * Returns: the initialized exception instance.
63 mono_exception_from_token (MonoImage *image, guint32 token)
68 klass = mono_class_get (image, token);
70 o = mono_object_new (mono_domain_get (), klass);
73 mono_runtime_object_init (o);
75 return (MonoException *)o;
79 * mono_exception_from_name_two_strings:
80 * @image: the Mono image where to look for the class
81 * @name_space: the namespace for the class
83 * @a1: first string argument to pass
84 * @a2: second string argument to pass
86 * Creates an exception from a constructor that takes two string
89 * Returns: the initialized exception instance.
92 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
93 const char *name, MonoString *a1, MonoString *a2)
95 MonoDomain *domain = mono_domain_get ();
97 MonoMethod *method = NULL;
107 klass = mono_class_from_name (image, name_space, name);
108 o = mono_object_new (domain, klass);
111 while ((m = mono_class_get_methods (klass, &iter))) {
112 MonoMethodSignature *sig;
114 if (strcmp (".ctor", mono_method_get_name (m)))
116 sig = mono_method_signature (m);
117 if (sig->param_count != count)
120 if (sig->params [0]->type != MONO_TYPE_STRING)
122 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
129 mono_runtime_invoke (method, o, args, NULL);
130 return (MonoException *) o;
134 * mono_exception_from_name_msg:
135 * @image: the Mono image where to look for the class
136 * @name_space: the namespace for the class
138 * @msg: the message to embed inside the exception
140 * Creates an exception and initializes its message field.
142 * Returns: the initialized exception instance.
145 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
146 const char *name, const guchar *msg)
150 ex = mono_exception_from_name (image, name_space, name);
153 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
159 mono_get_exception_divide_by_zero ()
161 return mono_exception_from_name (mono_get_corlib (), "System",
162 "DivideByZeroException");
166 mono_get_exception_security ()
168 return mono_exception_from_name (mono_get_corlib (), "System.Security",
169 "SecurityException");
173 mono_get_exception_thread_abort ()
175 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
176 "ThreadAbortException");
180 mono_get_exception_arithmetic ()
182 return mono_exception_from_name (mono_get_corlib (), "System",
183 "ArithmeticException");
187 mono_get_exception_overflow ()
189 return mono_exception_from_name (mono_get_corlib (), "System",
190 "OverflowException");
194 mono_get_exception_null_reference ()
196 return mono_exception_from_name (mono_get_corlib (), "System",
197 "NullReferenceException");
201 mono_get_exception_execution_engine (const guchar *msg)
203 return mono_exception_from_name_msg (mono_get_corlib (), "System",
204 "ExecutionEngineException", msg);
208 mono_get_exception_serialization (const guchar *msg)
210 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization",
211 "SerializationException", msg);
215 mono_get_exception_invalid_cast ()
217 return mono_exception_from_name (mono_get_corlib (), "System",
218 "InvalidCastException");
222 mono_get_exception_invalid_operation (const guchar *msg)
224 return mono_exception_from_name_msg (mono_get_corlib (), "System",
225 "InvalidOperationException", msg);
229 mono_get_exception_index_out_of_range ()
231 return mono_exception_from_name (mono_get_corlib (), "System",
232 "IndexOutOfRangeException");
236 mono_get_exception_array_type_mismatch ()
238 return mono_exception_from_name (mono_get_corlib (), "System",
239 "ArrayTypeMismatchException");
243 mono_get_exception_type_load (MonoString *type_name)
245 MonoTypeLoadException *exc;
247 exc = (MonoTypeLoadException *) mono_exception_from_name (mono_get_corlib (),
249 "TypeLoadException");
251 exc->type_name = type_name;
252 return (MonoException *) exc;
256 mono_get_exception_not_implemented (const guchar *msg)
260 ex = mono_exception_from_name (mono_get_corlib (), "System",
261 "NotImplementedException");
264 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
270 mono_get_exception_missing_method ()
272 return mono_exception_from_name (mono_get_corlib (), "System",
273 "MissingMethodException");
277 mono_get_exception_argument_null (const guchar *arg)
281 ex = mono_exception_from_name (
282 mono_get_corlib (), "System", "ArgumentNullException");
285 ((MonoArgumentException *)ex)->param_name =
286 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
292 mono_get_exception_argument (const guchar *arg, const guchar *msg)
296 ex = mono_exception_from_name_msg (
297 mono_get_corlib (), "System", "ArgumentException", msg);
300 ((MonoArgumentException *)ex)->param_name =
301 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
307 mono_get_exception_argument_out_of_range (const guchar *arg)
311 ex = mono_exception_from_name (
312 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
315 ((MonoArgumentException *)ex)->param_name =
316 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
322 mono_get_exception_thread_state (const guchar *msg)
324 return mono_exception_from_name_msg (
325 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
329 mono_get_exception_io (const guchar *msg)
331 return mono_exception_from_name_msg (
332 mono_get_corlib (), "System.IO", "IOException", msg);
336 mono_get_exception_file_not_found (MonoString *fname)
338 return mono_exception_from_name_two_strings (
339 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
343 mono_get_exception_file_not_found2 (const guchar *msg, MonoString *fname)
345 MonoString *s = mono_string_new (mono_domain_get (), msg);
347 return mono_exception_from_name_two_strings (
348 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
352 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
360 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
363 mono_class_init (klass);
365 /* TypeInitializationException only has 1 ctor with 2 args */
367 while ((method = mono_class_get_methods (klass, &iter))) {
368 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
375 args [0] = mono_string_new (mono_domain_get (), type_name);
378 exc = mono_object_new (mono_domain_get (), klass);
379 mono_runtime_invoke (method, exc, args, NULL);
381 return (MonoException *) exc;
385 mono_get_exception_synchronization_lock (const guchar *msg)
387 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
391 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
393 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
397 mono_get_exception_appdomain_unloaded (void)
399 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
403 mono_get_exception_bad_image_format (const guchar *msg)
405 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
409 mono_get_exception_stack_overflow (void)
411 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");