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>
17 #include <mono/metadata/object-internals.h>
18 #include <mono/metadata/metadata-internals.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/mono-debug.h>
21 #include <mono/utils/mono-error-internals.h>
24 #ifdef HAVE_EXECINFO_H
29 * mono_exception_from_name:
30 * @image: the Mono image where to look for the class
31 * @name_space: the namespace for the class
34 * Creates an exception of the given namespace/name class in the
37 * Returns: the initialized exception instance.
40 mono_exception_from_name (MonoImage *image, const char *name_space,
43 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
47 * mono_exception_from_name_domain:
48 * @domain: Domain where the return object will be created.
49 * @image: the Mono image where to look for the class
50 * @name_space: the namespace for the class
53 * Creates an exception object of the given namespace/name class on
56 * Returns: the initialized exception instance.
59 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
60 const char* name_space, const char *name)
65 MonoDomain *caller_domain = mono_domain_get ();
67 klass = mono_class_load_from_name (image, name_space, name);
69 o = mono_object_new_checked (domain, klass, &error);
70 g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */
72 if (domain != caller_domain)
73 mono_domain_set_internal (domain);
74 mono_runtime_object_init (o);
75 if (domain != caller_domain)
76 mono_domain_set_internal (caller_domain);
78 return (MonoException *)o;
83 * mono_exception_from_token:
84 * @image: the Mono image where to look for the class
85 * @token: The type token of the class
87 * Creates an exception of the type given by @token.
89 * Returns: the initialized exception instance.
92 mono_exception_from_token (MonoImage *image, guint32 token)
98 klass = mono_class_get_checked (image, token, &error);
99 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
101 o = mono_object_new_checked (mono_domain_get (), klass, &error);
102 g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */
104 mono_runtime_object_init (o);
106 return (MonoException *)o;
109 static MonoException *
110 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
113 MonoDomain *domain = mono_domain_get ();
114 MonoMethod *method = NULL;
124 o = mono_object_new_checked (domain, klass, &error);
125 mono_error_assert_ok (&error);
128 while ((m = mono_class_get_methods (klass, &iter))) {
129 MonoMethodSignature *sig;
131 if (strcmp (".ctor", mono_method_get_name (m)))
133 sig = mono_method_signature (m);
134 if (sig->param_count != count)
137 if (sig->params [0]->type != MONO_TYPE_STRING)
139 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
148 mono_runtime_invoke_checked (method, o, args, &error);
149 mono_error_raise_exception (&error); /* FIXME don't raise here */
151 return (MonoException *) o;
155 * mono_exception_from_name_two_strings:
156 * @image: the Mono image where to look for the class
157 * @name_space: the namespace for the class
159 * @a1: first string argument to pass
160 * @a2: second string argument to pass
162 * Creates an exception from a constructor that takes two string
165 * Returns: the initialized exception instance.
168 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
169 const char *name, MonoString *a1, MonoString *a2)
171 MonoClass *klass = mono_class_load_from_name (image, name_space, name);
173 return create_exception_two_strings (klass, a1, a2);
177 * mono_exception_from_name_msg:
178 * @image: the Mono image where to look for the class
179 * @name_space: the namespace for the class
181 * @msg: the message to embed inside the exception
183 * Creates an exception and initializes its message field.
185 * Returns: the initialized exception instance.
188 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
189 const char *name, const char *msg)
193 ex = mono_exception_from_name (image, name_space, name);
196 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
202 * mono_exception_from_token_two_strings:
204 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
208 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
209 MonoString *a1, MonoString *a2)
212 MonoClass *klass = mono_class_get_checked (image, token, &error);
213 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
215 return create_exception_two_strings (klass, a1, a2);
219 * mono_get_exception_divide_by_zero:
221 * Returns: a new instance of the `System.DivideByZeroException`
224 mono_get_exception_divide_by_zero ()
226 return mono_exception_from_name (mono_get_corlib (), "System",
227 "DivideByZeroException");
231 * mono_get_exception_security:
233 * Returns: a new instance of the `System.Security.SecurityException`
236 mono_get_exception_security ()
238 return mono_exception_from_name (mono_get_corlib (), "System.Security",
239 "SecurityException");
243 * mono_get_exception_thread_abort:
245 * Returns: a new instance of the `System.Threading.ThreadAbortException`
248 mono_get_exception_thread_abort ()
250 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
251 "ThreadAbortException");
255 * mono_get_exception_thread_interrupted:
257 * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
260 mono_get_exception_thread_interrupted ()
262 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
263 "ThreadInterruptedException");
267 * mono_get_exception_arithmetic:
269 * Returns: a new instance of the `System.ArithmeticException`
272 mono_get_exception_arithmetic ()
274 return mono_exception_from_name (mono_get_corlib (), "System",
275 "ArithmeticException");
279 * mono_get_exception_overflow:
281 * Returns: a new instance of the `System.OverflowException`
284 mono_get_exception_overflow ()
286 return mono_exception_from_name (mono_get_corlib (), "System",
287 "OverflowException");
291 * mono_get_exception_null_reference:
293 * Returns: a new instance of the `System.NullReferenceException`
296 mono_get_exception_null_reference ()
298 return mono_exception_from_name (mono_get_corlib (), "System",
299 "NullReferenceException");
303 * mono_get_exception_execution_engine:
304 * @msg: the message to pass to the user
306 * Returns: a new instance of the `System.ExecutionEngineException`
309 mono_get_exception_execution_engine (const char *msg)
311 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
315 * mono_get_exception_serialization:
316 * @msg: the message to pass to the user
318 * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
321 mono_get_exception_serialization (const char *msg)
323 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
327 * mono_get_exception_invalid_cast:
329 * Returns: a new instance of the `System.InvalidCastException`
332 mono_get_exception_invalid_cast ()
334 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
338 * mono_get_exception_invalid_operation:
339 * @msg: the message to pass to the user
341 * Returns: a new instance of the `System.InvalidOperationException`
344 mono_get_exception_invalid_operation (const char *msg)
346 return mono_exception_from_name_msg (mono_get_corlib (), "System",
347 "InvalidOperationException", msg);
351 * mono_get_exception_index_out_of_range:
353 * Returns: a new instance of the `System.IndexOutOfRangeException`
356 mono_get_exception_index_out_of_range ()
358 return mono_exception_from_name (mono_get_corlib (), "System",
359 "IndexOutOfRangeException");
363 * mono_get_exception_array_type_mismatch:
365 * Returns: a new instance of the `System.ArrayTypeMismatchException`
368 mono_get_exception_array_type_mismatch ()
370 return mono_exception_from_name (mono_get_corlib (), "System",
371 "ArrayTypeMismatchException");
375 * mono_get_exception_type_load:
376 * @class_name: the name of the class that could not be loaded
377 * @assembly_name: the assembly where the class was looked up.
379 * Returns: a new instance of the `System.TypeLoadException`
382 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
384 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
386 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
387 "TypeLoadException", class_name, s);
391 * mono_get_exception_not_implemented:
392 * @msg: the message to pass to the user
394 * Returns: a new instance of the `System.NotImplementedException`
397 mono_get_exception_not_implemented (const char *msg)
399 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
403 * mono_get_exception_not_supported:
404 * @msg: the message to pass to the user
406 * Returns: a new instance of the `System.NotSupportedException`
409 mono_get_exception_not_supported (const char *msg)
411 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
415 * mono_get_exception_missing_method:
416 * @class_name: the class where the lookup was performed.
417 * @member_name: the name of the missing method.
419 * Returns: a new instance of the `System.MissingMethodException`
422 mono_get_exception_missing_method (const char *class_name, const char *member_name)
424 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
425 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
427 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
428 "MissingMethodException", s1, s2);
432 * mono_get_exception_missing_field:
433 * @class_name: the class where the lookup was performed
434 * @member_name: the name of the missing method.
436 * Returns: a new instance of the `System.MissingFieldException`
439 mono_get_exception_missing_field (const char *class_name, const char *member_name)
441 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
442 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
444 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
445 "MissingFieldException", s1, s2);
449 * mono_get_exception_argument_null:
450 * @arg: the name of the argument that is null
452 * Returns: a new instance of the `System.ArgumentNullException`
455 mono_get_exception_argument_null (const char *arg)
459 ex = mono_exception_from_name (
460 mono_get_corlib (), "System", "ArgumentNullException");
463 MonoArgumentException *argex = (MonoArgumentException *)ex;
464 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
471 * mono_get_exception_argument:
472 * @arg: the name of the invalid argument.
474 * Returns: a new instance of the `System.ArgumentException`
477 mono_get_exception_argument (const char *arg, const char *msg)
481 ex = mono_exception_from_name_msg (
482 mono_get_corlib (), "System", "ArgumentException", msg);
485 MonoArgumentException *argex = (MonoArgumentException *)ex;
486 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
493 * mono_get_exception_argument_out_of_range:
494 * @arg: the name of the out of range argument.
496 * Returns: a new instance of the `System.ArgumentOutOfRangeException`
499 mono_get_exception_argument_out_of_range (const char *arg)
503 ex = mono_exception_from_name (
504 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
507 MonoArgumentException *argex = (MonoArgumentException *)ex;
508 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
515 * mono_get_exception_thread_state:
516 * @msg: the message to present to the user
518 * Returns: a new instance of the `System.Threading.ThreadStateException`
521 mono_get_exception_thread_state (const char *msg)
523 return mono_exception_from_name_msg (
524 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
528 * mono_get_exception_io:
529 * @msg: the message to present to the user
531 * Returns: a new instance of the `System.IO.IOException`
534 mono_get_exception_io (const char *msg)
536 return mono_exception_from_name_msg (
537 mono_get_corlib (), "System.IO", "IOException", msg);
541 * mono_get_exception_file_not_found:
542 * @fname: the name of the file not found.
544 * Returns: a new instance of the `System.IO.FileNotFoundException`
547 mono_get_exception_file_not_found (MonoString *fname)
549 return mono_exception_from_name_two_strings (
550 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
554 * mono_get_exception_file_not_found2:
555 * @msg: an informative message for the user.
556 * @fname: the name of the file not found.
558 * Returns: a new instance of the `System.IO.FileNotFoundException`
561 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
563 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
565 return mono_exception_from_name_two_strings (
566 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
570 * mono_get_exception_type_initialization:
571 * @type_name: the name of the type that failed to initialize.
572 * @inner: the inner exception.
574 * Returns: a new instance of the `System.TypeInitializationException`
577 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
586 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
588 mono_class_init (klass);
591 while ((method = mono_class_get_methods (klass, &iter))) {
592 if (!strcmp (".ctor", mono_method_get_name (method))) {
593 MonoMethodSignature *sig = mono_method_signature (method);
595 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
602 args [0] = mono_string_new (mono_domain_get (), type_name);
605 exc = mono_object_new_checked (mono_domain_get (), klass, &error);
606 mono_error_assert_ok (&error);
607 mono_runtime_invoke_checked (method, exc, args, &error);
608 mono_error_raise_exception (&error); /* FIXME don't raise here */
610 return (MonoException *) exc;
614 * mono_get_exception_synchronization_lock:
615 * @inner: the inner exception.
617 * Returns: a new instance of the `System.SynchronizationLockException`
620 mono_get_exception_synchronization_lock (const char *msg)
622 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
626 * mono_get_exception_cannot_unload_appdomain:
627 * @inner: the inner exception.
629 * Returns: a new instance of the `System.CannotUnloadAppDomainException`
632 mono_get_exception_cannot_unload_appdomain (const char *msg)
634 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
638 * mono_get_exception_appdomain_unloaded
640 * Returns: a new instance of the `System.AppDomainUnloadedException`
643 mono_get_exception_appdomain_unloaded (void)
645 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
649 * mono_get_exception_bad_image_format:
650 * @msg: an informative message for the user.
652 * Returns: a new instance of the `System.BadImageFormatException`
655 mono_get_exception_bad_image_format (const char *msg)
657 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
661 * mono_get_exception_bad_image_format2:
662 * @msg: an informative message for the user.
663 * @fname: The full name of the file with the invalid image.
665 * Returns: a new instance of the `System.BadImageFormatException`
668 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
670 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
672 return mono_exception_from_name_two_strings (
673 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
677 * mono_get_exception_stack_overflow:
679 * Returns: a new instance of the `System.StackOverflowException`
682 mono_get_exception_stack_overflow (void)
684 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
688 * mono_get_exception_out_of_memory:
690 * Returns: a new instance of the `System.OutOfMemoryException`
693 mono_get_exception_out_of_memory (void)
695 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
699 * mono_get_exception_field_access:
701 * Returns: a new instance of the `System.FieldAccessException`
704 mono_get_exception_field_access (void)
706 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
710 * mono_get_exception_field_access2:
711 * @msg: an informative message for the user.
713 * Returns: a new instance of the `System.FieldAccessException`
716 mono_get_exception_field_access_msg (const char *msg)
718 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
722 * mono_get_exception_method_access:
724 * Returns: a new instance of the `System.MethodAccessException`
727 mono_get_exception_method_access (void)
729 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
733 * mono_get_exception_method_access2:
734 * @msg: an informative message for the user.
736 * Returns: a new instance of the `System.MethodAccessException`
739 mono_get_exception_method_access_msg (const char *msg)
741 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
745 * mono_get_exception_reflection_type_load:
746 * @types: an array of types that were defined in the moduled loaded.
747 * @exceptions: an array of exceptions that were thrown during the type loading.
749 * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
752 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
761 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
763 mono_class_init (klass);
765 /* Find the Type[], Exception[] ctor */
767 while ((method = mono_class_get_methods (klass, &iter))) {
768 if (!strcmp (".ctor", mono_method_get_name (method))) {
769 MonoMethodSignature *sig = mono_method_signature (method);
771 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
779 args [1] = exceptions;
781 exc = mono_object_new_checked (mono_domain_get (), klass, &error);
782 mono_error_assert_ok (&error);
784 mono_runtime_invoke_checked (method, exc, args, &error);
785 mono_error_raise_exception (&error); /* FIXME don't raise here */
787 return (MonoException *) exc;
791 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
797 MonoDomain *domain = mono_domain_get ();
798 gpointer params [16];
800 klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
802 o = mono_object_new_checked (domain, klass, &error);
803 g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */
805 method = mono_class_get_method_from_name (klass, ".ctor", 1);
808 params [0] = wrapped_exception;
810 mono_runtime_invoke_checked (method, o, params, &error);
811 mono_error_raise_exception (&error); /* FIXME don't raise here */
813 return (MonoException *)o;
817 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
819 MonoDomain *domain = mono_domain_get ();
820 GString *text = (GString*)user_data;
823 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
824 g_string_append_printf (text, "%s\n", msg);
827 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
834 mono_exception_get_managed_backtrace (MonoException *exc)
838 text = g_string_new_len (NULL, 20);
840 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
841 g_string_append (text, "managed backtrace not available\n");
843 return g_string_free (text, FALSE);
847 mono_exception_get_native_backtrace (MonoException *exc)
849 #ifdef HAVE_BACKTRACE_SYMBOLS
851 MonoArray *arr = exc->native_trace_ips;
857 return g_strdup ("");
858 domain = mono_domain_get ();
859 len = mono_array_length (arr);
860 text = g_string_new_len (NULL, len * 20);
861 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
864 for (i = 0; i < len; ++i) {
865 gpointer ip = mono_array_get (arr, gpointer, i);
866 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
868 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
869 g_string_append_printf (text, "%s\n", msg);
872 g_string_append_printf (text, "%s\n", messages [i]);
877 return g_string_free (text, FALSE);
879 return g_strdup ("");
884 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
889 mono_raise_exception (mono_get_exception_argument_null ("exception"));
891 trace = mono_exception_get_native_backtrace (exc);
892 res = mono_string_new (mono_domain_get (), trace);
898 * mono_error_raise_exception:
899 * @target_error: the exception to raise
901 * Raises the exception of @target_error.
902 * Does nothing if @target_error has a success error code.
903 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
904 * to construct the first exception object.
905 * The error object @target_error is cleaned up.
908 mono_error_raise_exception (MonoError *target_error)
910 MonoException *ex = mono_error_convert_to_exception (target_error);
912 mono_raise_exception (ex);
916 mono_error_set_pending_exception (MonoError *error)
918 MonoException *ex = mono_error_convert_to_exception (error);
920 mono_set_pending_exception (ex);