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/metadata-internals.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/mono-debug.h>
21 #ifdef HAVE_EXECINFO_H
26 * mono_exception_from_name:
27 * @image: the Mono image where to look for the class
28 * @name_space: the namespace for the class
31 * Creates an exception of the given namespace/name class in the
34 * Returns: the initialized exception instance.
37 mono_exception_from_name (MonoImage *image, const char *name_space,
40 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
44 * mono_exception_from_name_domain:
45 * @domain: Domain where the return object will be created.
46 * @image: the Mono image where to look for the class
47 * @name_space: the namespace for the class
50 * Creates an exception object of the given namespace/name class on
53 * Returns: the initialized exception instance.
56 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
57 const char* name_space, const char *name)
61 MonoDomain *caller_domain = mono_domain_get ();
63 klass = mono_class_from_name (image, name_space, name);
65 o = mono_object_new (domain, klass);
68 if (domain != caller_domain)
69 mono_domain_set_internal (domain);
70 mono_runtime_object_init (o);
71 if (domain != caller_domain)
72 mono_domain_set_internal (caller_domain);
74 return (MonoException *)o;
79 * mono_exception_from_token:
80 * @image: the Mono image where to look for the class
81 * @token: The type token of the class
83 * Creates an exception of the type given by @token.
85 * Returns: the initialized exception instance.
88 mono_exception_from_token (MonoImage *image, guint32 token)
93 klass = mono_class_get (image, token);
95 o = mono_object_new (mono_domain_get (), klass);
98 mono_runtime_object_init (o);
100 return (MonoException *)o;
103 static MonoException *
104 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
106 MonoDomain *domain = mono_domain_get ();
107 MonoMethod *method = NULL;
117 o = mono_object_new (domain, klass);
120 while ((m = mono_class_get_methods (klass, &iter))) {
121 MonoMethodSignature *sig;
123 if (strcmp (".ctor", mono_method_get_name (m)))
125 sig = mono_method_signature (m);
126 if (sig->param_count != count)
129 if (sig->params [0]->type != MONO_TYPE_STRING)
131 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
139 mono_runtime_invoke (method, o, args, NULL);
140 return (MonoException *) o;
144 * mono_exception_from_name_two_strings:
145 * @image: the Mono image where to look for the class
146 * @name_space: the namespace for the class
148 * @a1: first string argument to pass
149 * @a2: second string argument to pass
151 * Creates an exception from a constructor that takes two string
154 * Returns: the initialized exception instance.
157 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
158 const char *name, MonoString *a1, MonoString *a2)
160 MonoClass *klass = mono_class_from_name (image, name_space, name);
162 return create_exception_two_strings (klass, a1, a2);
166 * mono_exception_from_name_msg:
167 * @image: the Mono image where to look for the class
168 * @name_space: the namespace for the class
170 * @msg: the message to embed inside the exception
172 * Creates an exception and initializes its message field.
174 * Returns: the initialized exception instance.
177 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
178 const char *name, const char *msg)
182 ex = mono_exception_from_name (image, name_space, name);
185 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
191 * mono_exception_from_token_two_strings:
193 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
197 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
198 MonoString *a1, MonoString *a2)
200 MonoClass *klass = mono_class_get (image, token);
202 return create_exception_two_strings (klass, a1, a2);
206 * mono_get_exception_divide_by_zero:
208 * Returns: a new instance of the System.DivideByZeroException
211 mono_get_exception_divide_by_zero ()
213 return mono_exception_from_name (mono_get_corlib (), "System",
214 "DivideByZeroException");
218 * mono_get_exception_security:
220 * Returns: a new instance of the System.Security.SecurityException
223 mono_get_exception_security ()
225 return mono_exception_from_name (mono_get_corlib (), "System.Security",
226 "SecurityException");
230 * mono_get_exception_thread_abort:
232 * Returns: a new instance of the System.Threading.ThreadAbortException.
235 mono_get_exception_thread_abort ()
237 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
238 "ThreadAbortException");
242 * mono_get_exception_thread_interrupted:
244 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
247 mono_get_exception_thread_interrupted ()
249 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
250 "ThreadInterruptedException");
254 * mono_get_exception_arithmetic:
256 * Returns: a new instance of the System.ArithmeticException.
259 mono_get_exception_arithmetic ()
261 return mono_exception_from_name (mono_get_corlib (), "System",
262 "ArithmeticException");
266 * mono_get_exception_overflow:
268 * Returns: a new instance of the System.OverflowException
271 mono_get_exception_overflow ()
273 return mono_exception_from_name (mono_get_corlib (), "System",
274 "OverflowException");
278 * mono_get_exception_null_reference:
280 * Returns: a new instance of the System.NullReferenceException
283 mono_get_exception_null_reference ()
285 return mono_exception_from_name (mono_get_corlib (), "System",
286 "NullReferenceException");
290 * mono_get_exception_execution_engine:
291 * @msg: the message to pass to the user
293 * Returns: a new instance of the System.ExecutionEngineException
296 mono_get_exception_execution_engine (const char *msg)
298 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
302 * mono_get_exception_serialization:
303 * @msg: the message to pass to the user
305 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
308 mono_get_exception_serialization (const char *msg)
310 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
314 * mono_get_exception_invalid_cast:
316 * Returns: a new instance of the System.InvalidCastException
319 mono_get_exception_invalid_cast ()
321 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
325 * mono_get_exception_invalid_operation:
326 * @msg: the message to pass to the user
328 * Returns: a new instance of the System.InvalidOperationException
331 mono_get_exception_invalid_operation (const char *msg)
333 return mono_exception_from_name_msg (mono_get_corlib (), "System",
334 "InvalidOperationException", msg);
338 * mono_get_exception_index_out_of_range:
340 * Returns: a new instance of the System.IndexOutOfRangeException
343 mono_get_exception_index_out_of_range ()
345 return mono_exception_from_name (mono_get_corlib (), "System",
346 "IndexOutOfRangeException");
350 * mono_get_exception_array_type_mismatch:
352 * Returns: a new instance of the System.ArrayTypeMismatchException
355 mono_get_exception_array_type_mismatch ()
357 return mono_exception_from_name (mono_get_corlib (), "System",
358 "ArrayTypeMismatchException");
362 * mono_get_exception_type_load:
363 * @class_name: the name of the class that could not be loaded
364 * @assembly_name: the assembly where the class was looked up.
366 * Returns: a new instance of the System.TypeLoadException.
369 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
371 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
373 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
374 "TypeLoadException", class_name, s);
378 * mono_get_exception_not_implemented:
379 * @msg: the message to pass to the user
381 * Returns: a new instance of the System.NotImplementedException
384 mono_get_exception_not_implemented (const char *msg)
386 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
390 * mono_get_exception_not_supported:
391 * @msg: the message to pass to the user
393 * Returns: a new instance of the System.NotSupportedException
396 mono_get_exception_not_supported (const char *msg)
398 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
402 * mono_get_exception_missing_method:
403 * @class_name: the class where the lookup was performed.
404 * @member_name: the name of the missing method.
406 * Returns: a new instance of the System.MissingMethodException
409 mono_get_exception_missing_method (const char *class_name, const char *member_name)
411 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
412 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
414 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
415 "MissingMethodException", s1, s2);
419 * mono_get_exception_missing_field:
420 * @class_name: the class where the lookup was performed
421 * @member_name: the name of the missing method.
423 * Returns: a new instance of the System.MissingFieldException
426 mono_get_exception_missing_field (const char *class_name, const char *member_name)
428 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
429 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
431 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
432 "MissingFieldException", s1, s2);
436 * mono_get_exception_argument_null:
437 * @arg: the name of the argument that is null
439 * Returns: a new instance of the System.ArgumentNullException
442 mono_get_exception_argument_null (const char *arg)
446 ex = mono_exception_from_name (
447 mono_get_corlib (), "System", "ArgumentNullException");
450 MonoArgumentException *argex = (MonoArgumentException *)ex;
451 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
458 * mono_get_exception_argument:
459 * @arg: the name of the invalid argument.
461 * Returns: a new instance of the System.ArgumentException
464 mono_get_exception_argument (const char *arg, const char *msg)
468 ex = mono_exception_from_name_msg (
469 mono_get_corlib (), "System", "ArgumentException", msg);
472 MonoArgumentException *argex = (MonoArgumentException *)ex;
473 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
480 * mono_get_exception_argument_out_of_range:
481 * @arg: the name of the out of range argument.
483 * Returns: a new instance of the System.ArgumentOutOfRangeException
486 mono_get_exception_argument_out_of_range (const char *arg)
490 ex = mono_exception_from_name (
491 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
494 MonoArgumentException *argex = (MonoArgumentException *)ex;
495 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
502 * mono_get_exception_thread_state:
503 * @msg: the message to present to the user
505 * Returns: a new instance of the System.Threading.ThreadStateException
508 mono_get_exception_thread_state (const char *msg)
510 return mono_exception_from_name_msg (
511 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
515 * mono_get_exception_io:
516 * @msg: the message to present to the user
518 * Returns: a new instance of the System.IO.IOException
521 mono_get_exception_io (const char *msg)
523 return mono_exception_from_name_msg (
524 mono_get_corlib (), "System.IO", "IOException", msg);
528 * mono_get_exception_file_not_found:
529 * @fname: the name of the file not found.
531 * Returns: a new instance of the System.IO.FileNotFoundException
534 mono_get_exception_file_not_found (MonoString *fname)
536 return mono_exception_from_name_two_strings (
537 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
541 * mono_get_exception_file_not_found2:
542 * @msg: an informative message for the user.
543 * @fname: the name of the file not found.
545 * Returns: a new instance of the System.IO.FileNotFoundException
548 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
550 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
552 return mono_exception_from_name_two_strings (
553 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
557 * mono_get_exception_type_initialization:
558 * @type_name: the name of the type that failed to initialize.
559 * @inner: the inner exception.
561 * Returns: a new instance of the System.TypeInitializationException
564 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
572 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
575 mono_class_init (klass);
577 /* TypeInitializationException only has 1 ctor with 2 args */
579 while ((method = mono_class_get_methods (klass, &iter))) {
580 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
587 args [0] = mono_string_new (mono_domain_get (), type_name);
590 exc = mono_object_new (mono_domain_get (), klass);
591 mono_runtime_invoke (method, exc, args, NULL);
593 return (MonoException *) exc;
597 * mono_get_exception_synchronization_lock:
598 * @inner: the inner exception.
600 * Returns: a new instance of the System.SynchronizationLockException
603 mono_get_exception_synchronization_lock (const char *msg)
605 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
609 * mono_get_exception_cannot_unload_appdomain:
610 * @inner: the inner exception.
612 * Returns: a new instance of the System.CannotUnloadAppDomainException
615 mono_get_exception_cannot_unload_appdomain (const char *msg)
617 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
621 * mono_get_exception_appdomain_unloaded
623 * Returns: a new instance of the System.AppDomainUnloadedException
626 mono_get_exception_appdomain_unloaded (void)
628 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
632 * mono_get_exception_bad_image_format:
633 * @msg: an informative message for the user.
635 * Returns: a new instance of the System.BadImageFormatException
638 mono_get_exception_bad_image_format (const char *msg)
640 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
644 * mono_get_exception_bad_image_format2:
645 * @msg: an informative message for the user.
646 * @fname: The full name of the file with the invalid image.
648 * Returns: a new instance of the System.BadImageFormatException
651 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
653 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
655 return mono_exception_from_name_two_strings (
656 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
660 * mono_get_exception_stack_overflow:
662 * Returns: a new instance of the System.StackOverflowException
665 mono_get_exception_stack_overflow (void)
667 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
671 * mono_get_exception_out_of_memory:
673 * Returns: a new instance of the System.OutOfMemoryException
676 mono_get_exception_out_of_memory (void)
678 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
682 * mono_get_exception_field_access:
684 * Returns: a new instance of the System.FieldAccessException
687 mono_get_exception_field_access (void)
689 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
693 * mono_get_exception_field_access2:
694 * @msg: an informative message for the user.
696 * Returns: a new instance of the System.FieldAccessException
699 mono_get_exception_field_access_msg (const char *msg)
701 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
705 * mono_get_exception_method_access:
707 * Returns: a new instance of the System.MethodAccessException
710 mono_get_exception_method_access (void)
712 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
716 * mono_get_exception_method_access2:
717 * @msg: an informative message for the user.
719 * Returns: a new instance of the System.MethodAccessException
722 mono_get_exception_method_access_msg (const char *msg)
724 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
728 * mono_get_exception_reflection_type_load:
729 * @types: an array of types that were defined in the moduled loaded.
730 * @exceptions: an array of exceptions that were thrown during the type loading.
732 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
735 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
742 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
744 mono_class_init (klass);
746 method = mono_class_get_method_from_name (klass, ".ctor", 2);
750 args [1] = exceptions;
752 exc = mono_object_new (mono_domain_get (), klass);
753 mono_runtime_invoke (method, exc, args, NULL);
755 return (MonoException *) exc;
759 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
761 MonoRuntimeWrappedException *ex = (MonoRuntimeWrappedException*)
762 mono_exception_from_name (mono_get_corlib (), "System.Runtime.CompilerServices",
763 "RuntimeWrappedException");
765 MONO_OBJECT_SETREF (ex, wrapped_exception, wrapped_exception);
766 return (MonoException*)ex;
770 mono_exception_get_native_backtrace (MonoException *exc)
772 #ifdef HAVE_BACKTRACE_SYMBOLS
774 MonoArray *arr = exc->native_trace_ips;
780 return g_strdup ("");
781 domain = mono_domain_get ();
782 len = mono_array_length (arr);
783 text = g_string_new_len (NULL, len * 20);
784 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
787 for (i = 0; i < len; ++i) {
788 gpointer ip = mono_array_get (arr, gpointer, i);
789 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
791 char *msg = mono_debug_print_stack_frame (ji->method, (char*)ip - (char*)ji->code_start, domain);
792 g_string_append_printf (text, "%s\n", msg);
795 g_string_append_printf (text, "%s\n", messages [i]);
800 return g_string_free (text, FALSE);
802 return g_strdup ("");
807 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
812 mono_raise_exception (mono_get_exception_argument_null ("exception"));
814 trace = mono_exception_get_native_backtrace (exc);
815 res = mono_string_new (mono_domain_get (), trace);