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 in the
27 * Returns: the initialized exception instance.
30 mono_exception_from_name (MonoImage *image, const char *name_space,
33 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
37 * mono_exception_from_name_domain:
38 * @domain: Domain where the return object will be created.
39 * @image: the Mono image where to look for the class
40 * @name_space: the namespace for the class
43 * Creates an exception object of the given namespace/name class on
46 * Returns: the initialized exception instance.
49 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
50 const char* name_space, const char *name)
55 klass = mono_class_from_name (image, name_space, name);
57 o = mono_object_new (domain, klass);
60 mono_runtime_object_init (o);
62 return (MonoException *)o;
67 * mono_exception_from_token:
68 * @image: the Mono image where to look for the class
69 * @token: The type token of the class
71 * Creates an exception of the type given by @token.
73 * Returns: the initialized exception instance.
76 mono_exception_from_token (MonoImage *image, guint32 token)
81 klass = mono_class_get (image, token);
83 o = mono_object_new (mono_domain_get (), klass);
86 mono_runtime_object_init (o);
88 return (MonoException *)o;
92 * mono_exception_from_name_two_strings:
93 * @image: the Mono image where to look for the class
94 * @name_space: the namespace for the class
96 * @a1: first string argument to pass
97 * @a2: second string argument to pass
99 * Creates an exception from a constructor that takes two string
102 * Returns: the initialized exception instance.
105 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
106 const char *name, MonoString *a1, MonoString *a2)
108 MonoDomain *domain = mono_domain_get ();
110 MonoMethod *method = NULL;
120 klass = mono_class_from_name (image, name_space, name);
121 o = mono_object_new (domain, klass);
124 while ((m = mono_class_get_methods (klass, &iter))) {
125 MonoMethodSignature *sig;
127 if (strcmp (".ctor", mono_method_get_name (m)))
129 sig = mono_method_signature (m);
130 if (sig->param_count != count)
133 if (sig->params [0]->type != MONO_TYPE_STRING)
135 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
142 mono_runtime_invoke (method, o, args, NULL);
143 return (MonoException *) o;
147 * mono_exception_from_name_msg:
148 * @image: the Mono image where to look for the class
149 * @name_space: the namespace for the class
151 * @msg: the message to embed inside the exception
153 * Creates an exception and initializes its message field.
155 * Returns: the initialized exception instance.
158 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
159 const char *name, const char *msg)
163 ex = mono_exception_from_name (image, name_space, name);
166 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
172 * mono_get_exception_divide_by_zero:
174 * Returns: a new instance of the System.DivideByZeroException
177 mono_get_exception_divide_by_zero ()
179 return mono_exception_from_name (mono_get_corlib (), "System",
180 "DivideByZeroException");
184 * mono_get_exception_security:
186 * Returns: a new instance of the System.Security.SecurityException
189 mono_get_exception_security ()
191 return mono_exception_from_name (mono_get_corlib (), "System.Security",
192 "SecurityException");
196 * mono_get_exception_thread_abort:
198 * Returns: a new instance of the System.Threading.ThreadAbortException.
201 mono_get_exception_thread_abort ()
203 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
204 "ThreadAbortException");
208 * mono_get_exception_arithmetic:
210 * Returns: a new instance of the System.ArithmeticException.
213 mono_get_exception_arithmetic ()
215 return mono_exception_from_name (mono_get_corlib (), "System",
216 "ArithmeticException");
220 * mono_get_exception_overflow:
222 * Returns: a new instance of the System.OverflowException
225 mono_get_exception_overflow ()
227 return mono_exception_from_name (mono_get_corlib (), "System",
228 "OverflowException");
232 * mono_get_exception_null_reference:
234 * Returns: a new instance of the System.NullReferenceException
237 mono_get_exception_null_reference ()
239 return mono_exception_from_name (mono_get_corlib (), "System",
240 "NullReferenceException");
244 * mono_get_exception_execution_engine:
245 * @msg: the message to pass to the user
247 * Returns: a new instance of the System.ExecutionEngineException
250 mono_get_exception_execution_engine (const char *msg)
252 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
256 * mono_get_exception_serialization:
257 * @msg: the message to pass to the user
259 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
262 mono_get_exception_serialization (const char *msg)
264 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
268 * mono_get_exception_invalid_cast:
270 * Returns: a new instance of the System.InvalidCastException
273 mono_get_exception_invalid_cast ()
275 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
279 * mono_get_exception_invalid_operation:
280 * @msg: the message to pass to the user
282 * Returns: a new instance of the System.InvalidOperationException
285 mono_get_exception_invalid_operation (const char *msg)
287 return mono_exception_from_name_msg (mono_get_corlib (), "System",
288 "InvalidOperationException", msg);
292 * mono_get_exception_index_out_of_range:
294 * Returns: a new instance of the System.IndexOutOfRangeException
297 mono_get_exception_index_out_of_range ()
299 return mono_exception_from_name (mono_get_corlib (), "System",
300 "IndexOutOfRangeException");
304 * mono_get_exception_array_type_mismatch:
306 * Returns: a new instance of the System.ArrayTypeMismatchException
309 mono_get_exception_array_type_mismatch ()
311 return mono_exception_from_name (mono_get_corlib (), "System",
312 "ArrayTypeMismatchException");
316 * mono_get_exception_type_load:
317 * @class_name: the name of the class that could not be loaded
318 * @assembly_name: the assembly where the class was looked up.
320 * Returns: a new instance of the System.TypeLoadException.
323 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
325 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
327 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
328 "TypeLoadException", class_name, s);
332 * mono_get_exception_not_implemented:
333 * @msg: the message to pass to the user
335 * Returns: a new instance of the System.NotImplementedException
338 mono_get_exception_not_implemented (const char *msg)
342 ex = mono_exception_from_name (mono_get_corlib (), "System",
343 "NotImplementedException");
346 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
352 * mono_get_exception_missing_method:
353 * @class_name: the class where the lookup was performed.
354 * @member_name: the name of the missing method.
356 * Returns: a new instance of the System.MissingMethodException
359 mono_get_exception_missing_method (const char *class_name, const char *member_name)
361 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
362 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
364 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
365 "MissingMethodException", s1, s2);
369 * mono_get_exception_missing_field:
370 * @class_name: the class where the lookup was performed
371 * @member_name: the name of the missing method.
373 * Returns: a new instance of the System.MissingFieldException
376 mono_get_exception_missing_field (const char *class_name, const char *member_name)
378 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
379 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
381 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
382 "MissingFieldException", s1, s2);
386 * mono_get_exception_argument_null:
387 * @arg: the name of the argument that is null
389 * Returns: a new instance of the System.ArgumentNullException
392 mono_get_exception_argument_null (const char *arg)
396 ex = mono_exception_from_name (
397 mono_get_corlib (), "System", "ArgumentNullException");
400 ((MonoArgumentException *)ex)->param_name =
401 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
407 * mono_get_exception_argument:
408 * @arg: the name of the invalid argument.
410 * Returns: a new instance of the System.ArgumentException
413 mono_get_exception_argument (const char *arg, const char *msg)
417 ex = mono_exception_from_name_msg (
418 mono_get_corlib (), "System", "ArgumentException", msg);
421 ((MonoArgumentException *)ex)->param_name =
422 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
428 * mono_get_exception_argument_out_of_range:
429 * @arg: the name of the out of range argument.
431 * Returns: a new instance of the System.ArgumentOutOfRangeException
434 mono_get_exception_argument_out_of_range (const char *arg)
438 ex = mono_exception_from_name (
439 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
442 ((MonoArgumentException *)ex)->param_name =
443 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
449 * mono_get_exception_thread_state:
450 * @msg: the message to present to the user
452 * Returns: a new instance of the System.Threading.ThreadStateException
455 mono_get_exception_thread_state (const char *msg)
457 return mono_exception_from_name_msg (
458 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
462 * mono_get_exception_io:
463 * @msg: the message to present to the user
465 * Returns: a new instance of the System.IO.IOException
468 mono_get_exception_io (const char *msg)
470 return mono_exception_from_name_msg (
471 mono_get_corlib (), "System.IO", "IOException", msg);
475 * mono_get_exception_file_not_found:
476 * @fname: the name of the file not found.
478 * Returns: a new instance of the System.IO.FileNotFoundException
481 mono_get_exception_file_not_found (MonoString *fname)
483 return mono_exception_from_name_two_strings (
484 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
488 * mono_get_exception_file_not_found2:
489 * @msg: an informative message for the user.
490 * @fname: the name of the file not found.
492 * Returns: a new instance of the System.IO.FileNotFoundException
495 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
497 MonoString *s = mono_string_new (mono_domain_get (), msg);
499 return mono_exception_from_name_two_strings (
500 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
504 * mono_get_exception_type_initialization:
505 * @type_name: the name of the type that failed to initialize.
506 * @inner: the inner exception.
508 * Returns: a new instance of the System.TypeInitializationException
511 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
519 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
522 mono_class_init (klass);
524 /* TypeInitializationException only has 1 ctor with 2 args */
526 while ((method = mono_class_get_methods (klass, &iter))) {
527 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
534 args [0] = mono_string_new (mono_domain_get (), type_name);
537 exc = mono_object_new (mono_domain_get (), klass);
538 mono_runtime_invoke (method, exc, args, NULL);
540 return (MonoException *) exc;
544 * mono_get_exception_synchronization_lock:
545 * @inner: the inner exception.
547 * Returns: a new instance of the System.TypeInitializationException
550 mono_get_exception_synchronization_lock (const char *msg)
552 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
556 * mono_get_exception_cannot_unload_appdomain:
557 * @inner: the inner exception.
559 * Returns: a new instance of the System.CannotUnloadAppDomainException
562 mono_get_exception_cannot_unload_appdomain (const char *msg)
564 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
568 * mono_get_exception_appdomain_unloaded
570 * Returns: a new instance of the System.AppDomainUnloadedException
573 mono_get_exception_appdomain_unloaded (void)
575 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
579 * mono_get_exception_bad_image_format:
580 * @msg: an informative message for the user.
582 * Returns: a new instance of the System.BadImageFormatException
585 mono_get_exception_bad_image_format (const char *msg)
587 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
591 * mono_get_exception_stack_overflow:
593 * Returns: a new instance of the System.StackOverflowException
596 mono_get_exception_stack_overflow (void)
598 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
602 * mono_get_exception_reflection_type_load:
603 * @types: an array of types that were defined in the moduled loaded.
604 * @exceptions: an array of exceptions that were thrown during the type loading.
606 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
609 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
616 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
618 mono_class_init (klass);
620 method = mono_class_get_method_from_name (klass, ".ctor", 2);
624 args [1] = exceptions;
626 exc = mono_object_new (mono_domain_get (), klass);
627 mono_runtime_invoke (method, exc, args, NULL);
629 return (MonoException *) exc;