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)
15 #include <mono/metadata/exception.h>
16 #include <mono/metadata/exception-internals.h>
18 #include <mono/metadata/object-internals.h>
19 #include <mono/metadata/metadata-internals.h>
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/mono-debug.h>
22 #include <mono/utils/mono-error-internals.h>
25 #ifdef HAVE_EXECINFO_H
30 * mono_exception_from_name:
31 * @image: the Mono image where to look for the class
32 * @name_space: the namespace for the class
35 * Creates an exception of the given namespace/name class in the
38 * Returns: the initialized exception instance.
41 mono_exception_from_name (MonoImage *image, const char *name_space,
44 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
48 * mono_exception_from_name_domain:
49 * @domain: Domain where the return object will be created.
50 * @image: the Mono image where to look for the class
51 * @name_space: the namespace for the class
54 * Creates an exception object of the given namespace/name class on
57 * Returns: the initialized exception instance.
60 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
61 const char* name_space, const char *name)
66 MonoDomain *caller_domain = mono_domain_get ();
68 klass = mono_class_load_from_name (image, name_space, name);
70 o = mono_object_new_checked (domain, klass, &error);
71 mono_error_assert_ok (&error);
73 if (domain != caller_domain)
74 mono_domain_set_internal (domain);
75 mono_runtime_object_init_checked (o, &error);
76 mono_error_assert_ok (&error);
78 if (domain != caller_domain)
79 mono_domain_set_internal (caller_domain);
81 return (MonoException *)o;
86 * mono_exception_from_token:
87 * @image: the Mono image where to look for the class
88 * @token: The type token of the class
90 * Creates an exception of the type given by @token.
92 * Returns: the initialized exception instance.
95 mono_exception_from_token (MonoImage *image, guint32 token)
101 klass = mono_class_get_checked (image, token, &error);
102 mono_error_assert_ok (&error);
104 o = mono_object_new_checked (mono_domain_get (), klass, &error);
105 mono_error_assert_ok (&error);
107 mono_runtime_object_init_checked (o, &error);
108 mono_error_assert_ok (&error);
110 return (MonoException *)o;
113 static MonoException *
114 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
116 MonoDomain *domain = mono_domain_get ();
117 MonoMethod *method = NULL;
127 o = mono_object_new_checked (domain, klass, error);
128 mono_error_assert_ok (error);
131 while ((m = mono_class_get_methods (klass, &iter))) {
132 MonoMethodSignature *sig;
134 if (strcmp (".ctor", mono_method_get_name (m)))
136 sig = mono_method_signature (m);
137 if (sig->param_count != count)
140 if (sig->params [0]->type != MONO_TYPE_STRING)
142 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
151 mono_runtime_invoke_checked (method, o, args, error);
152 return_val_if_nok (error, NULL);
154 return (MonoException *) o;
158 * mono_exception_from_name_two_strings:
159 * @image: the Mono image where to look for the class
160 * @name_space: the namespace for the class
162 * @a1: first string argument to pass
163 * @a2: second string argument to pass
165 * Creates an exception from a constructor that takes two string
168 * Returns: the initialized exception instance.
171 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
172 const char *name, MonoString *a1, MonoString *a2)
178 klass = mono_class_load_from_name (image, name_space, name);
180 ret = create_exception_two_strings (klass, a1, a2, &error);
181 mono_error_raise_exception (&error); /* FIXME don't raise here */
187 * mono_exception_from_name_msg:
188 * @image: the Mono image where to look for the class
189 * @name_space: the namespace for the class
191 * @msg: the message to embed inside the exception
193 * Creates an exception and initializes its message field.
195 * Returns: the initialized exception instance.
198 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
199 const char *name, const char *msg)
203 ex = mono_exception_from_name (image, name_space, name);
206 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
212 * mono_exception_from_token_two_strings:
214 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
218 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
219 MonoString *a1, MonoString *a2)
225 klass = mono_class_get_checked (image, token, &error);
226 mono_error_assert_ok (&error); /* FIXME handle the error. */
228 ret = create_exception_two_strings (klass, a1, a2, &error);
229 mono_error_raise_exception (&error); /* FIXME don't raise here */
235 * mono_get_exception_divide_by_zero:
237 * Returns: a new instance of the `System.DivideByZeroException`
240 mono_get_exception_divide_by_zero ()
242 return mono_exception_from_name (mono_get_corlib (), "System",
243 "DivideByZeroException");
247 * mono_get_exception_security:
249 * Returns: a new instance of the `System.Security.SecurityException`
252 mono_get_exception_security ()
254 return mono_exception_from_name (mono_get_corlib (), "System.Security",
255 "SecurityException");
259 * mono_get_exception_thread_abort:
261 * Returns: a new instance of the `System.Threading.ThreadAbortException`
264 mono_get_exception_thread_abort ()
266 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
267 "ThreadAbortException");
271 * mono_get_exception_thread_interrupted:
273 * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
276 mono_get_exception_thread_interrupted ()
278 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
279 "ThreadInterruptedException");
283 * mono_get_exception_arithmetic:
285 * Returns: a new instance of the `System.ArithmeticException`
288 mono_get_exception_arithmetic ()
290 return mono_exception_from_name (mono_get_corlib (), "System",
291 "ArithmeticException");
295 * mono_get_exception_overflow:
297 * Returns: a new instance of the `System.OverflowException`
300 mono_get_exception_overflow ()
302 return mono_exception_from_name (mono_get_corlib (), "System",
303 "OverflowException");
307 * mono_get_exception_null_reference:
309 * Returns: a new instance of the `System.NullReferenceException`
312 mono_get_exception_null_reference ()
314 return mono_exception_from_name (mono_get_corlib (), "System",
315 "NullReferenceException");
319 * mono_get_exception_execution_engine:
320 * @msg: the message to pass to the user
322 * Returns: a new instance of the `System.ExecutionEngineException`
325 mono_get_exception_execution_engine (const char *msg)
327 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
331 * mono_get_exception_serialization:
332 * @msg: the message to pass to the user
334 * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
337 mono_get_exception_serialization (const char *msg)
339 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
343 * mono_get_exception_invalid_cast:
345 * Returns: a new instance of the `System.InvalidCastException`
348 mono_get_exception_invalid_cast ()
350 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
354 * mono_get_exception_invalid_operation:
355 * @msg: the message to pass to the user
357 * Returns: a new instance of the `System.InvalidOperationException`
360 mono_get_exception_invalid_operation (const char *msg)
362 return mono_exception_from_name_msg (mono_get_corlib (), "System",
363 "InvalidOperationException", msg);
367 * mono_get_exception_index_out_of_range:
369 * Returns: a new instance of the `System.IndexOutOfRangeException`
372 mono_get_exception_index_out_of_range ()
374 return mono_exception_from_name (mono_get_corlib (), "System",
375 "IndexOutOfRangeException");
379 * mono_get_exception_array_type_mismatch:
381 * Returns: a new instance of the `System.ArrayTypeMismatchException`
384 mono_get_exception_array_type_mismatch ()
386 return mono_exception_from_name (mono_get_corlib (), "System",
387 "ArrayTypeMismatchException");
391 * mono_get_exception_type_load:
392 * @class_name: the name of the class that could not be loaded
393 * @assembly_name: the assembly where the class was looked up.
395 * Returns: a new instance of the `System.TypeLoadException`
398 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
400 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
402 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
403 "TypeLoadException", class_name, s);
407 * mono_get_exception_not_implemented:
408 * @msg: the message to pass to the user
410 * Returns: a new instance of the `System.NotImplementedException`
413 mono_get_exception_not_implemented (const char *msg)
415 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
419 * mono_get_exception_not_supported:
420 * @msg: the message to pass to the user
422 * Returns: a new instance of the `System.NotSupportedException`
425 mono_get_exception_not_supported (const char *msg)
427 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
431 * mono_get_exception_missing_method:
432 * @class_name: the class where the lookup was performed.
433 * @member_name: the name of the missing method.
435 * Returns: a new instance of the `System.MissingMethodException`
438 mono_get_exception_missing_method (const char *class_name, const char *member_name)
440 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
441 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
443 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
444 "MissingMethodException", s1, s2);
448 * mono_get_exception_missing_field:
449 * @class_name: the class where the lookup was performed
450 * @member_name: the name of the missing method.
452 * Returns: a new instance of the `System.MissingFieldException`
455 mono_get_exception_missing_field (const char *class_name, const char *member_name)
457 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
458 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
460 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
461 "MissingFieldException", s1, s2);
465 * mono_get_exception_argument_null:
466 * @arg: the name of the argument that is null
468 * Returns: a new instance of the `System.ArgumentNullException`
471 mono_get_exception_argument_null (const char *arg)
475 ex = mono_exception_from_name (
476 mono_get_corlib (), "System", "ArgumentNullException");
479 MonoArgumentException *argex = (MonoArgumentException *)ex;
480 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
487 * mono_get_exception_argument:
488 * @arg: the name of the invalid argument.
490 * Returns: a new instance of the `System.ArgumentException`
493 mono_get_exception_argument (const char *arg, const char *msg)
497 ex = mono_exception_from_name_msg (
498 mono_get_corlib (), "System", "ArgumentException", msg);
501 MonoArgumentException *argex = (MonoArgumentException *)ex;
502 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
509 * mono_get_exception_argument_out_of_range:
510 * @arg: the name of the out of range argument.
512 * Returns: a new instance of the `System.ArgumentOutOfRangeException`
515 mono_get_exception_argument_out_of_range (const char *arg)
519 ex = mono_exception_from_name (
520 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
523 MonoArgumentException *argex = (MonoArgumentException *)ex;
524 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
531 * mono_get_exception_thread_state:
532 * @msg: the message to present to the user
534 * Returns: a new instance of the `System.Threading.ThreadStateException`
537 mono_get_exception_thread_state (const char *msg)
539 return mono_exception_from_name_msg (
540 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
544 * mono_get_exception_io:
545 * @msg: the message to present to the user
547 * Returns: a new instance of the `System.IO.IOException`
550 mono_get_exception_io (const char *msg)
552 return mono_exception_from_name_msg (
553 mono_get_corlib (), "System.IO", "IOException", msg);
557 * mono_get_exception_file_not_found:
558 * @fname: the name of the file not found.
560 * Returns: a new instance of the `System.IO.FileNotFoundException`
563 mono_get_exception_file_not_found (MonoString *fname)
565 return mono_exception_from_name_two_strings (
566 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
570 * mono_get_exception_file_not_found2:
571 * @msg: an informative message for the user.
572 * @fname: the name of the file not found.
574 * Returns: a new instance of the `System.IO.FileNotFoundException`
577 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
579 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
581 return mono_exception_from_name_two_strings (
582 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
586 * mono_get_exception_type_initialization:
587 * @type_name: the name of the type that failed to initialize.
588 * @inner: the inner exception.
590 * Returns: a new instance of the `System.TypeInitializationException`
593 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
596 MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
597 if (!is_ok (&error)) {
598 mono_error_cleanup (&error);
606 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
614 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
616 mono_class_init (klass);
619 while ((method = mono_class_get_methods (klass, &iter))) {
620 if (!strcmp (".ctor", mono_method_get_name (method))) {
621 MonoMethodSignature *sig = mono_method_signature (method);
623 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
630 args [0] = mono_string_new (mono_domain_get (), type_name);
633 exc = mono_object_new_checked (mono_domain_get (), klass, error);
634 mono_error_assert_ok (error);
636 mono_runtime_invoke_checked (method, exc, args, error);
637 return_val_if_nok (error, NULL);
639 return (MonoException *) exc;
643 * mono_get_exception_synchronization_lock:
644 * @inner: the inner exception.
646 * Returns: a new instance of the `System.SynchronizationLockException`
649 mono_get_exception_synchronization_lock (const char *msg)
651 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
655 * mono_get_exception_cannot_unload_appdomain:
656 * @inner: the inner exception.
658 * Returns: a new instance of the `System.CannotUnloadAppDomainException`
661 mono_get_exception_cannot_unload_appdomain (const char *msg)
663 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
667 * mono_get_exception_appdomain_unloaded
669 * Returns: a new instance of the `System.AppDomainUnloadedException`
672 mono_get_exception_appdomain_unloaded (void)
674 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
678 * mono_get_exception_bad_image_format:
679 * @msg: an informative message for the user.
681 * Returns: a new instance of the `System.BadImageFormatException`
684 mono_get_exception_bad_image_format (const char *msg)
686 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
690 * mono_get_exception_bad_image_format2:
691 * @msg: an informative message for the user.
692 * @fname: The full name of the file with the invalid image.
694 * Returns: a new instance of the `System.BadImageFormatException`
697 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
699 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
701 return mono_exception_from_name_two_strings (
702 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
706 * mono_get_exception_stack_overflow:
708 * Returns: a new instance of the `System.StackOverflowException`
711 mono_get_exception_stack_overflow (void)
713 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
717 * mono_get_exception_out_of_memory:
719 * Returns: a new instance of the `System.OutOfMemoryException`
722 mono_get_exception_out_of_memory (void)
724 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
728 * mono_get_exception_field_access:
730 * Returns: a new instance of the `System.FieldAccessException`
733 mono_get_exception_field_access (void)
735 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
739 * mono_get_exception_field_access2:
740 * @msg: an informative message for the user.
742 * Returns: a new instance of the `System.FieldAccessException`
745 mono_get_exception_field_access_msg (const char *msg)
747 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
751 * mono_get_exception_method_access:
753 * Returns: a new instance of the `System.MethodAccessException`
756 mono_get_exception_method_access (void)
758 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
762 * mono_get_exception_method_access2:
763 * @msg: an informative message for the user.
765 * Returns: a new instance of the `System.MethodAccessException`
768 mono_get_exception_method_access_msg (const char *msg)
770 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
774 * mono_get_exception_reflection_type_load:
775 * @types: an array of types that were defined in the moduled loaded.
776 * @exceptions: an array of exceptions that were thrown during the type loading.
778 * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
781 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
784 MonoException *ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
785 if (is_ok (&error)) {
786 mono_error_cleanup (&error);
794 mono_get_exception_reflection_type_load_checked (MonoArray *types, MonoArray *exceptions, MonoError *error)
802 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
804 mono_class_init (klass);
806 /* Find the Type[], Exception[] ctor */
808 while ((method = mono_class_get_methods (klass, &iter))) {
809 if (!strcmp (".ctor", mono_method_get_name (method))) {
810 MonoMethodSignature *sig = mono_method_signature (method);
812 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
820 args [1] = exceptions;
822 exc = mono_object_new_checked (mono_domain_get (), klass, error);
823 mono_error_assert_ok (error);
825 mono_runtime_invoke_checked (method, exc, args, error);
826 return_val_if_nok (error, NULL);
828 return (MonoException *) exc;
832 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
835 MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
836 if (!is_ok (&error)) {
837 mono_error_cleanup (&error);
845 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
850 MonoDomain *domain = mono_domain_get ();
851 gpointer params [16];
853 klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
855 o = mono_object_new_checked (domain, klass, error);
856 mono_error_assert_ok (error);
857 g_assert (o != NULL);
859 method = mono_class_get_method_from_name (klass, ".ctor", 1);
862 params [0] = wrapped_exception;
864 mono_runtime_invoke_checked (method, o, params, error);
865 return_val_if_nok (error, NULL);
867 return (MonoException *)o;
871 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
873 MonoDomain *domain = mono_domain_get ();
874 GString *text = (GString*)user_data;
877 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
878 g_string_append_printf (text, "%s\n", msg);
881 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
888 mono_exception_get_managed_backtrace (MonoException *exc)
892 text = g_string_new_len (NULL, 20);
894 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
895 g_string_append (text, "managed backtrace not available\n");
897 return g_string_free (text, FALSE);
901 mono_exception_get_native_backtrace (MonoException *exc)
903 #ifdef HAVE_BACKTRACE_SYMBOLS
905 MonoArray *arr = exc->native_trace_ips;
911 return g_strdup ("");
912 domain = mono_domain_get ();
913 len = mono_array_length (arr);
914 text = g_string_new_len (NULL, len * 20);
915 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
918 for (i = 0; i < len; ++i) {
919 gpointer ip = mono_array_get (arr, gpointer, i);
920 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
922 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
923 g_string_append_printf (text, "%s\n", msg);
926 g_string_append_printf (text, "%s\n", messages [i]);
931 return g_string_free (text, FALSE);
933 return g_strdup ("");
938 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
943 mono_raise_exception (mono_get_exception_argument_null ("exception"));
945 trace = mono_exception_get_native_backtrace (exc);
946 res = mono_string_new (mono_domain_get (), trace);
952 * mono_error_raise_exception:
953 * @target_error: the exception to raise
955 * Raises the exception of @target_error.
956 * Does nothing if @target_error has a success error code.
957 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
958 * to construct the first exception object.
959 * The error object @target_error is cleaned up.
962 mono_error_raise_exception (MonoError *target_error)
964 MonoException *ex = mono_error_convert_to_exception (target_error);
966 mono_raise_exception (ex);
970 mono_error_set_pending_exception (MonoError *error)
972 MonoException *ex = mono_error_convert_to_exception (error);
974 mono_set_pending_exception (ex);