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 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
14 #include <mono/metadata/exception.h>
15 #include <mono/metadata/object-internals.h>
16 #include <mono/metadata/appdomain.h>
20 * mono_exception_from_name:
21 * @image: the Mono image where to look for the class
22 * @name_space: the namespace for the class
25 * Creates an exception of the given namespace/name class in the
28 * Returns: the initialized exception instance.
31 mono_exception_from_name (MonoImage *image, const char *name_space,
34 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
38 * mono_exception_from_name_domain:
39 * @domain: Domain where the return object will be created.
40 * @image: the Mono image where to look for the class
41 * @name_space: the namespace for the class
44 * Creates an exception object of the given namespace/name class on
47 * Returns: the initialized exception instance.
50 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
51 const char* name_space, const char *name)
55 MonoDomain *caller_domain = mono_domain_get ();
57 klass = mono_class_from_name (image, name_space, name);
59 o = mono_object_new (domain, klass);
62 if (domain != caller_domain)
63 mono_domain_set_internal (domain);
64 mono_runtime_object_init (o);
65 if (domain != caller_domain)
66 mono_domain_set_internal (caller_domain);
68 return (MonoException *)o;
73 * mono_exception_from_token:
74 * @image: the Mono image where to look for the class
75 * @token: The type token of the class
77 * Creates an exception of the type given by @token.
79 * Returns: the initialized exception instance.
82 mono_exception_from_token (MonoImage *image, guint32 token)
87 klass = mono_class_get (image, token);
89 o = mono_object_new (mono_domain_get (), klass);
92 mono_runtime_object_init (o);
94 return (MonoException *)o;
97 static MonoException *
98 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
100 MonoDomain *domain = mono_domain_get ();
101 MonoMethod *method = NULL;
111 o = mono_object_new (domain, klass);
114 while ((m = mono_class_get_methods (klass, &iter))) {
115 MonoMethodSignature *sig;
117 if (strcmp (".ctor", mono_method_get_name (m)))
119 sig = mono_method_signature (m);
120 if (sig->param_count != count)
123 if (sig->params [0]->type != MONO_TYPE_STRING)
125 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
133 mono_runtime_invoke (method, o, args, NULL);
134 return (MonoException *) o;
138 * mono_exception_from_name_two_strings:
139 * @image: the Mono image where to look for the class
140 * @name_space: the namespace for the class
142 * @a1: first string argument to pass
143 * @a2: second string argument to pass
145 * Creates an exception from a constructor that takes two string
148 * Returns: the initialized exception instance.
151 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
152 const char *name, MonoString *a1, MonoString *a2)
154 MonoClass *klass = mono_class_from_name (image, name_space, name);
156 return create_exception_two_strings (klass, a1, a2);
160 * mono_exception_from_name_msg:
161 * @image: the Mono image where to look for the class
162 * @name_space: the namespace for the class
164 * @msg: the message to embed inside the exception
166 * Creates an exception and initializes its message field.
168 * Returns: the initialized exception instance.
171 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
172 const char *name, const char *msg)
176 ex = mono_exception_from_name (image, name_space, name);
179 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
185 * mono_exception_from_token_two_strings:
187 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
191 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
192 MonoString *a1, MonoString *a2)
194 MonoClass *klass = mono_class_get (image, token);
196 return create_exception_two_strings (klass, a1, a2);
200 * mono_get_exception_divide_by_zero:
202 * Returns: a new instance of the System.DivideByZeroException
205 mono_get_exception_divide_by_zero ()
207 return mono_exception_from_name (mono_get_corlib (), "System",
208 "DivideByZeroException");
212 * mono_get_exception_security:
214 * Returns: a new instance of the System.Security.SecurityException
217 mono_get_exception_security ()
219 return mono_exception_from_name (mono_get_corlib (), "System.Security",
220 "SecurityException");
224 * mono_get_exception_thread_abort:
226 * Returns: a new instance of the System.Threading.ThreadAbortException.
229 mono_get_exception_thread_abort ()
231 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
232 "ThreadAbortException");
236 * mono_get_exception_thread_interrupted:
238 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
241 mono_get_exception_thread_interrupted ()
243 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
244 "ThreadInterruptedException");
248 * mono_get_exception_arithmetic:
250 * Returns: a new instance of the System.ArithmeticException.
253 mono_get_exception_arithmetic ()
255 return mono_exception_from_name (mono_get_corlib (), "System",
256 "ArithmeticException");
260 * mono_get_exception_overflow:
262 * Returns: a new instance of the System.OverflowException
265 mono_get_exception_overflow ()
267 return mono_exception_from_name (mono_get_corlib (), "System",
268 "OverflowException");
272 * mono_get_exception_null_reference:
274 * Returns: a new instance of the System.NullReferenceException
277 mono_get_exception_null_reference ()
279 return mono_exception_from_name (mono_get_corlib (), "System",
280 "NullReferenceException");
284 * mono_get_exception_execution_engine:
285 * @msg: the message to pass to the user
287 * Returns: a new instance of the System.ExecutionEngineException
290 mono_get_exception_execution_engine (const char *msg)
292 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
296 * mono_get_exception_serialization:
297 * @msg: the message to pass to the user
299 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
302 mono_get_exception_serialization (const char *msg)
304 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
308 * mono_get_exception_invalid_cast:
310 * Returns: a new instance of the System.InvalidCastException
313 mono_get_exception_invalid_cast ()
315 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
319 * mono_get_exception_invalid_operation:
320 * @msg: the message to pass to the user
322 * Returns: a new instance of the System.InvalidOperationException
325 mono_get_exception_invalid_operation (const char *msg)
327 return mono_exception_from_name_msg (mono_get_corlib (), "System",
328 "InvalidOperationException", msg);
332 * mono_get_exception_index_out_of_range:
334 * Returns: a new instance of the System.IndexOutOfRangeException
337 mono_get_exception_index_out_of_range ()
339 return mono_exception_from_name (mono_get_corlib (), "System",
340 "IndexOutOfRangeException");
344 * mono_get_exception_array_type_mismatch:
346 * Returns: a new instance of the System.ArrayTypeMismatchException
349 mono_get_exception_array_type_mismatch ()
351 return mono_exception_from_name (mono_get_corlib (), "System",
352 "ArrayTypeMismatchException");
356 * mono_get_exception_type_load:
357 * @class_name: the name of the class that could not be loaded
358 * @assembly_name: the assembly where the class was looked up.
360 * Returns: a new instance of the System.TypeLoadException.
363 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
365 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
367 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
368 "TypeLoadException", class_name, s);
372 * mono_get_exception_not_implemented:
373 * @msg: the message to pass to the user
375 * Returns: a new instance of the System.NotImplementedException
378 mono_get_exception_not_implemented (const char *msg)
380 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
384 * mono_get_exception_not_supported:
385 * @msg: the message to pass to the user
387 * Returns: a new instance of the System.NotSupportedException
390 mono_get_exception_not_supported (const char *msg)
392 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
396 * mono_get_exception_missing_method:
397 * @class_name: the class where the lookup was performed.
398 * @member_name: the name of the missing method.
400 * Returns: a new instance of the System.MissingMethodException
403 mono_get_exception_missing_method (const char *class_name, const char *member_name)
405 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
406 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
408 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
409 "MissingMethodException", s1, s2);
413 * mono_get_exception_missing_field:
414 * @class_name: the class where the lookup was performed
415 * @member_name: the name of the missing method.
417 * Returns: a new instance of the System.MissingFieldException
420 mono_get_exception_missing_field (const char *class_name, const char *member_name)
422 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
423 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
425 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
426 "MissingFieldException", s1, s2);
430 * mono_get_exception_argument_null:
431 * @arg: the name of the argument that is null
433 * Returns: a new instance of the System.ArgumentNullException
436 mono_get_exception_argument_null (const char *arg)
440 ex = mono_exception_from_name (
441 mono_get_corlib (), "System", "ArgumentNullException");
444 MonoArgumentException *argex = (MonoArgumentException *)ex;
445 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
452 * mono_get_exception_argument:
453 * @arg: the name of the invalid argument.
455 * Returns: a new instance of the System.ArgumentException
458 mono_get_exception_argument (const char *arg, const char *msg)
462 ex = mono_exception_from_name_msg (
463 mono_get_corlib (), "System", "ArgumentException", msg);
466 MonoArgumentException *argex = (MonoArgumentException *)ex;
467 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
474 * mono_get_exception_argument_out_of_range:
475 * @arg: the name of the out of range argument.
477 * Returns: a new instance of the System.ArgumentOutOfRangeException
480 mono_get_exception_argument_out_of_range (const char *arg)
484 ex = mono_exception_from_name (
485 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
488 MonoArgumentException *argex = (MonoArgumentException *)ex;
489 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
496 * mono_get_exception_thread_state:
497 * @msg: the message to present to the user
499 * Returns: a new instance of the System.Threading.ThreadStateException
502 mono_get_exception_thread_state (const char *msg)
504 return mono_exception_from_name_msg (
505 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
509 * mono_get_exception_io:
510 * @msg: the message to present to the user
512 * Returns: a new instance of the System.IO.IOException
515 mono_get_exception_io (const char *msg)
517 return mono_exception_from_name_msg (
518 mono_get_corlib (), "System.IO", "IOException", msg);
522 * mono_get_exception_file_not_found:
523 * @fname: the name of the file not found.
525 * Returns: a new instance of the System.IO.FileNotFoundException
528 mono_get_exception_file_not_found (MonoString *fname)
530 return mono_exception_from_name_two_strings (
531 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
535 * mono_get_exception_file_not_found2:
536 * @msg: an informative message for the user.
537 * @fname: the name of the file not found.
539 * Returns: a new instance of the System.IO.FileNotFoundException
542 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
544 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
546 return mono_exception_from_name_two_strings (
547 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
551 * mono_get_exception_type_initialization:
552 * @type_name: the name of the type that failed to initialize.
553 * @inner: the inner exception.
555 * Returns: a new instance of the System.TypeInitializationException
558 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
566 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
569 mono_class_init (klass);
571 /* TypeInitializationException only has 1 ctor with 2 args */
573 while ((method = mono_class_get_methods (klass, &iter))) {
574 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
581 args [0] = mono_string_new (mono_domain_get (), type_name);
584 exc = mono_object_new (mono_domain_get (), klass);
585 mono_runtime_invoke (method, exc, args, NULL);
587 return (MonoException *) exc;
591 * mono_get_exception_synchronization_lock:
592 * @inner: the inner exception.
594 * Returns: a new instance of the System.TypeInitializationException
597 mono_get_exception_synchronization_lock (const char *msg)
599 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
603 * mono_get_exception_cannot_unload_appdomain:
604 * @inner: the inner exception.
606 * Returns: a new instance of the System.CannotUnloadAppDomainException
609 mono_get_exception_cannot_unload_appdomain (const char *msg)
611 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
615 * mono_get_exception_appdomain_unloaded
617 * Returns: a new instance of the System.AppDomainUnloadedException
620 mono_get_exception_appdomain_unloaded (void)
622 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
626 * mono_get_exception_bad_image_format:
627 * @msg: an informative message for the user.
629 * Returns: a new instance of the System.BadImageFormatException
632 mono_get_exception_bad_image_format (const char *msg)
634 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
638 * mono_get_exception_bad_image_format2:
639 * @msg: an informative message for the user.
640 * @fname: The full name of the file with the invalid image.
642 * Returns: a new instance of the System.BadImageFormatException
645 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
647 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
649 return mono_exception_from_name_two_strings (
650 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
654 * mono_get_exception_stack_overflow:
656 * Returns: a new instance of the System.StackOverflowException
659 mono_get_exception_stack_overflow (void)
661 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
665 * mono_get_exception_out_of_memory:
667 * Returns: a new instance of the System.OutOfMemoryException
670 mono_get_exception_out_of_memory (void)
672 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
676 * mono_get_exception_field_access:
678 * Returns: a new instance of the System.FieldAccessException
681 mono_get_exception_field_access (void)
683 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
687 * mono_get_exception_method_access:
689 * Returns: a new instance of the System.MethodAccessException
692 mono_get_exception_method_access (void)
694 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
698 * mono_get_exception_reflection_type_load:
699 * @types: an array of types that were defined in the moduled loaded.
700 * @exceptions: an array of exceptions that were thrown during the type loading.
702 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
705 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
712 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
714 mono_class_init (klass);
716 method = mono_class_get_method_from_name (klass, ".ctor", 2);
720 args [1] = exceptions;
722 exc = mono_object_new (mono_domain_get (), klass);
723 mono_runtime_invoke (method, exc, args, NULL);
725 return (MonoException *) exc;