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 char *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 char *msg)
203 return mono_exception_from_name_msg (mono_get_corlib (), "System",
204 "ExecutionEngineException", msg);
208 mono_get_exception_serialization (const char *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 char *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 *class_name, char *assembly_name)
245 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
247 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
248 "TypeLoadException", class_name, s);
252 mono_get_exception_not_implemented (const char *msg)
256 ex = mono_exception_from_name (mono_get_corlib (), "System",
257 "NotImplementedException");
260 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
266 mono_get_exception_missing_method (const char *class_name, const char *member_name)
268 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
269 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
271 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
272 "MissingMethodException", s1, s2);
276 mono_get_exception_missing_field (const char *class_name, const char *member_name)
278 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
279 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
281 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
282 "MissingFieldException", s1, s2);
286 mono_get_exception_argument_null (const char *arg)
290 ex = mono_exception_from_name (
291 mono_get_corlib (), "System", "ArgumentNullException");
294 ((MonoArgumentException *)ex)->param_name =
295 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
301 mono_get_exception_argument (const char *arg, const char *msg)
305 ex = mono_exception_from_name_msg (
306 mono_get_corlib (), "System", "ArgumentException", msg);
309 ((MonoArgumentException *)ex)->param_name =
310 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
316 mono_get_exception_argument_out_of_range (const char *arg)
320 ex = mono_exception_from_name (
321 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
324 ((MonoArgumentException *)ex)->param_name =
325 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
331 mono_get_exception_thread_state (const char *msg)
333 return mono_exception_from_name_msg (
334 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
338 mono_get_exception_io (const char *msg)
340 return mono_exception_from_name_msg (
341 mono_get_corlib (), "System.IO", "IOException", msg);
345 mono_get_exception_file_not_found (MonoString *fname)
347 return mono_exception_from_name_two_strings (
348 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
352 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
354 MonoString *s = mono_string_new (mono_domain_get (), msg);
356 return mono_exception_from_name_two_strings (
357 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
361 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
369 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
372 mono_class_init (klass);
374 /* TypeInitializationException only has 1 ctor with 2 args */
376 while ((method = mono_class_get_methods (klass, &iter))) {
377 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
384 args [0] = mono_string_new (mono_domain_get (), type_name);
387 exc = mono_object_new (mono_domain_get (), klass);
388 mono_runtime_invoke (method, exc, args, NULL);
390 return (MonoException *) exc;
394 mono_get_exception_synchronization_lock (const char *msg)
396 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
400 mono_get_exception_cannot_unload_appdomain (const char *msg)
402 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
406 mono_get_exception_appdomain_unloaded (void)
408 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
412 mono_get_exception_bad_image_format (const char *msg)
414 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
418 mono_get_exception_stack_overflow (void)
420 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
424 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
431 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
433 mono_class_init (klass);
435 method = mono_class_get_method_from_name (klass, ".ctor", 2);
439 args [1] = exceptions;
441 exc = mono_object_new (mono_domain_get (), klass);
442 mono_runtime_invoke (method, exc, args, NULL);
444 return (MonoException *) exc;