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>
19 #include <mono/utils/mono-error-internals.h>
22 #ifdef HAVE_EXECINFO_H
27 * mono_exception_from_name:
28 * @image: the Mono image where to look for the class
29 * @name_space: the namespace for the class
32 * Creates an exception of the given namespace/name class in the
35 * Returns: the initialized exception instance.
38 mono_exception_from_name (MonoImage *image, const char *name_space,
41 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
45 * mono_exception_from_name_domain:
46 * @domain: Domain where the return object will be created.
47 * @image: the Mono image where to look for the class
48 * @name_space: the namespace for the class
51 * Creates an exception object of the given namespace/name class on
54 * Returns: the initialized exception instance.
57 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
58 const char* name_space, const char *name)
62 MonoDomain *caller_domain = mono_domain_get ();
64 klass = mono_class_from_name (image, name_space, name);
66 o = mono_object_new (domain, klass);
69 if (domain != caller_domain)
70 mono_domain_set_internal (domain);
71 mono_runtime_object_init (o);
72 if (domain != caller_domain)
73 mono_domain_set_internal (caller_domain);
75 return (MonoException *)o;
80 * mono_exception_from_token:
81 * @image: the Mono image where to look for the class
82 * @token: The type token of the class
84 * Creates an exception of the type given by @token.
86 * Returns: the initialized exception instance.
89 mono_exception_from_token (MonoImage *image, guint32 token)
95 klass = mono_class_get_checked (image, token, &error);
96 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
98 o = mono_object_new (mono_domain_get (), klass);
101 mono_runtime_object_init (o);
103 return (MonoException *)o;
106 static MonoException *
107 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
109 MonoDomain *domain = mono_domain_get ();
110 MonoMethod *method = NULL;
120 o = mono_object_new (domain, klass);
123 while ((m = mono_class_get_methods (klass, &iter))) {
124 MonoMethodSignature *sig;
126 if (strcmp (".ctor", mono_method_get_name (m)))
128 sig = mono_method_signature (m);
129 if (sig->param_count != count)
132 if (sig->params [0]->type != MONO_TYPE_STRING)
134 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_two_strings:
148 * @image: the Mono image where to look for the class
149 * @name_space: the namespace for the class
151 * @a1: first string argument to pass
152 * @a2: second string argument to pass
154 * Creates an exception from a constructor that takes two string
157 * Returns: the initialized exception instance.
160 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
161 const char *name, MonoString *a1, MonoString *a2)
163 MonoClass *klass = mono_class_from_name (image, name_space, name);
165 return create_exception_two_strings (klass, a1, a2);
169 * mono_exception_from_name_msg:
170 * @image: the Mono image where to look for the class
171 * @name_space: the namespace for the class
173 * @msg: the message to embed inside the exception
175 * Creates an exception and initializes its message field.
177 * Returns: the initialized exception instance.
180 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
181 const char *name, const char *msg)
185 ex = mono_exception_from_name (image, name_space, name);
188 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
194 * mono_exception_from_token_two_strings:
196 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
200 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
201 MonoString *a1, MonoString *a2)
204 MonoClass *klass = mono_class_get_checked (image, token, &error);
205 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
207 return create_exception_two_strings (klass, a1, a2);
211 * mono_get_exception_divide_by_zero:
213 * Returns: a new instance of the System.DivideByZeroException
216 mono_get_exception_divide_by_zero ()
218 return mono_exception_from_name (mono_get_corlib (), "System",
219 "DivideByZeroException");
223 * mono_get_exception_security:
225 * Returns: a new instance of the System.Security.SecurityException
228 mono_get_exception_security ()
230 return mono_exception_from_name (mono_get_corlib (), "System.Security",
231 "SecurityException");
235 * mono_get_exception_thread_abort:
237 * Returns: a new instance of the System.Threading.ThreadAbortException.
240 mono_get_exception_thread_abort ()
242 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
243 "ThreadAbortException");
247 * mono_get_exception_thread_interrupted:
249 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
252 mono_get_exception_thread_interrupted ()
254 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
255 "ThreadInterruptedException");
259 * mono_get_exception_arithmetic:
261 * Returns: a new instance of the System.ArithmeticException.
264 mono_get_exception_arithmetic ()
266 return mono_exception_from_name (mono_get_corlib (), "System",
267 "ArithmeticException");
271 * mono_get_exception_overflow:
273 * Returns: a new instance of the System.OverflowException
276 mono_get_exception_overflow ()
278 return mono_exception_from_name (mono_get_corlib (), "System",
279 "OverflowException");
283 * mono_get_exception_null_reference:
285 * Returns: a new instance of the System.NullReferenceException
288 mono_get_exception_null_reference ()
290 return mono_exception_from_name (mono_get_corlib (), "System",
291 "NullReferenceException");
295 * mono_get_exception_execution_engine:
296 * @msg: the message to pass to the user
298 * Returns: a new instance of the System.ExecutionEngineException
301 mono_get_exception_execution_engine (const char *msg)
303 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
307 * mono_get_exception_serialization:
308 * @msg: the message to pass to the user
310 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
313 mono_get_exception_serialization (const char *msg)
315 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
319 * mono_get_exception_invalid_cast:
321 * Returns: a new instance of the System.InvalidCastException
324 mono_get_exception_invalid_cast ()
326 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
330 * mono_get_exception_invalid_operation:
331 * @msg: the message to pass to the user
333 * Returns: a new instance of the System.InvalidOperationException
336 mono_get_exception_invalid_operation (const char *msg)
338 return mono_exception_from_name_msg (mono_get_corlib (), "System",
339 "InvalidOperationException", msg);
343 * mono_get_exception_index_out_of_range:
345 * Returns: a new instance of the System.IndexOutOfRangeException
348 mono_get_exception_index_out_of_range ()
350 return mono_exception_from_name (mono_get_corlib (), "System",
351 "IndexOutOfRangeException");
355 * mono_get_exception_array_type_mismatch:
357 * Returns: a new instance of the System.ArrayTypeMismatchException
360 mono_get_exception_array_type_mismatch ()
362 return mono_exception_from_name (mono_get_corlib (), "System",
363 "ArrayTypeMismatchException");
367 * mono_get_exception_type_load:
368 * @class_name: the name of the class that could not be loaded
369 * @assembly_name: the assembly where the class was looked up.
371 * Returns: a new instance of the System.TypeLoadException.
374 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
376 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
378 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
379 "TypeLoadException", class_name, s);
383 * mono_get_exception_not_implemented:
384 * @msg: the message to pass to the user
386 * Returns: a new instance of the System.NotImplementedException
389 mono_get_exception_not_implemented (const char *msg)
391 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
395 * mono_get_exception_not_supported:
396 * @msg: the message to pass to the user
398 * Returns: a new instance of the System.NotSupportedException
401 mono_get_exception_not_supported (const char *msg)
403 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
407 * mono_get_exception_missing_method:
408 * @class_name: the class where the lookup was performed.
409 * @member_name: the name of the missing method.
411 * Returns: a new instance of the System.MissingMethodException
414 mono_get_exception_missing_method (const char *class_name, const char *member_name)
416 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
417 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
419 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
420 "MissingMethodException", s1, s2);
424 * mono_get_exception_missing_field:
425 * @class_name: the class where the lookup was performed
426 * @member_name: the name of the missing method.
428 * Returns: a new instance of the System.MissingFieldException
431 mono_get_exception_missing_field (const char *class_name, const char *member_name)
433 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
434 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
436 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
437 "MissingFieldException", s1, s2);
441 * mono_get_exception_argument_null:
442 * @arg: the name of the argument that is null
444 * Returns: a new instance of the System.ArgumentNullException
447 mono_get_exception_argument_null (const char *arg)
451 ex = mono_exception_from_name (
452 mono_get_corlib (), "System", "ArgumentNullException");
455 MonoArgumentException *argex = (MonoArgumentException *)ex;
456 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
463 * mono_get_exception_argument:
464 * @arg: the name of the invalid argument.
466 * Returns: a new instance of the System.ArgumentException
469 mono_get_exception_argument (const char *arg, const char *msg)
473 ex = mono_exception_from_name_msg (
474 mono_get_corlib (), "System", "ArgumentException", msg);
477 MonoArgumentException *argex = (MonoArgumentException *)ex;
478 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
485 * mono_get_exception_argument_out_of_range:
486 * @arg: the name of the out of range argument.
488 * Returns: a new instance of the System.ArgumentOutOfRangeException
491 mono_get_exception_argument_out_of_range (const char *arg)
495 ex = mono_exception_from_name (
496 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
499 MonoArgumentException *argex = (MonoArgumentException *)ex;
500 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
507 * mono_get_exception_thread_state:
508 * @msg: the message to present to the user
510 * Returns: a new instance of the System.Threading.ThreadStateException
513 mono_get_exception_thread_state (const char *msg)
515 return mono_exception_from_name_msg (
516 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
520 * mono_get_exception_io:
521 * @msg: the message to present to the user
523 * Returns: a new instance of the System.IO.IOException
526 mono_get_exception_io (const char *msg)
528 return mono_exception_from_name_msg (
529 mono_get_corlib (), "System.IO", "IOException", msg);
533 * mono_get_exception_file_not_found:
534 * @fname: the name of the file not found.
536 * Returns: a new instance of the System.IO.FileNotFoundException
539 mono_get_exception_file_not_found (MonoString *fname)
541 return mono_exception_from_name_two_strings (
542 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
546 * mono_get_exception_file_not_found2:
547 * @msg: an informative message for the user.
548 * @fname: the name of the file not found.
550 * Returns: a new instance of the System.IO.FileNotFoundException
553 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
555 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
557 return mono_exception_from_name_two_strings (
558 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
562 * mono_get_exception_type_initialization:
563 * @type_name: the name of the type that failed to initialize.
564 * @inner: the inner exception.
566 * Returns: a new instance of the System.TypeInitializationException
569 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
577 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
580 mono_class_init (klass);
583 while ((method = mono_class_get_methods (klass, &iter))) {
584 if (!strcmp (".ctor", mono_method_get_name (method))) {
585 MonoMethodSignature *sig = mono_method_signature (method);
587 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
594 args [0] = mono_string_new (mono_domain_get (), type_name);
597 exc = mono_object_new (mono_domain_get (), klass);
598 mono_runtime_invoke (method, exc, args, NULL);
600 return (MonoException *) exc;
604 * mono_get_exception_synchronization_lock:
605 * @inner: the inner exception.
607 * Returns: a new instance of the System.SynchronizationLockException
610 mono_get_exception_synchronization_lock (const char *msg)
612 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
616 * mono_get_exception_cannot_unload_appdomain:
617 * @inner: the inner exception.
619 * Returns: a new instance of the System.CannotUnloadAppDomainException
622 mono_get_exception_cannot_unload_appdomain (const char *msg)
624 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
628 * mono_get_exception_appdomain_unloaded
630 * Returns: a new instance of the System.AppDomainUnloadedException
633 mono_get_exception_appdomain_unloaded (void)
635 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
639 * mono_get_exception_bad_image_format:
640 * @msg: an informative message for the user.
642 * Returns: a new instance of the System.BadImageFormatException
645 mono_get_exception_bad_image_format (const char *msg)
647 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
651 * mono_get_exception_bad_image_format2:
652 * @msg: an informative message for the user.
653 * @fname: The full name of the file with the invalid image.
655 * Returns: a new instance of the System.BadImageFormatException
658 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
660 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
662 return mono_exception_from_name_two_strings (
663 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
667 * mono_get_exception_stack_overflow:
669 * Returns: a new instance of the System.StackOverflowException
672 mono_get_exception_stack_overflow (void)
674 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
678 * mono_get_exception_out_of_memory:
680 * Returns: a new instance of the System.OutOfMemoryException
683 mono_get_exception_out_of_memory (void)
685 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
689 * mono_get_exception_field_access:
691 * Returns: a new instance of the System.FieldAccessException
694 mono_get_exception_field_access (void)
696 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
700 * mono_get_exception_field_access2:
701 * @msg: an informative message for the user.
703 * Returns: a new instance of the System.FieldAccessException
706 mono_get_exception_field_access_msg (const char *msg)
708 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
712 * mono_get_exception_method_access:
714 * Returns: a new instance of the System.MethodAccessException
717 mono_get_exception_method_access (void)
719 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
723 * mono_get_exception_method_access2:
724 * @msg: an informative message for the user.
726 * Returns: a new instance of the System.MethodAccessException
729 mono_get_exception_method_access_msg (const char *msg)
731 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
735 * mono_get_exception_reflection_type_load:
736 * @types: an array of types that were defined in the moduled loaded.
737 * @exceptions: an array of exceptions that were thrown during the type loading.
739 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
742 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
750 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
752 mono_class_init (klass);
754 /* Find the Type[], Exception[] ctor */
756 while ((method = mono_class_get_methods (klass, &iter))) {
757 if (!strcmp (".ctor", mono_method_get_name (method))) {
758 MonoMethodSignature *sig = mono_method_signature (method);
760 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
768 args [1] = exceptions;
770 exc = mono_object_new (mono_domain_get (), klass);
771 mono_runtime_invoke (method, exc, args, NULL);
773 return (MonoException *) exc;
777 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
782 MonoDomain *domain = mono_domain_get ();
783 gpointer params [16];
785 klass = mono_class_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
788 o = mono_object_new (domain, klass);
789 g_assert (o != NULL);
791 method = mono_class_get_method_from_name (klass, ".ctor", 1);
794 params [0] = wrapped_exception;
795 mono_runtime_invoke (method, o, params, NULL);
797 return (MonoException *)o;
801 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
803 MonoDomain *domain = mono_domain_get ();
804 GString *text = (GString*)user_data;
807 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
808 g_string_append_printf (text, "%s\n", msg);
811 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
818 mono_exception_get_managed_backtrace (MonoException *exc)
822 text = g_string_new_len (NULL, 20);
824 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
825 g_string_append (text, "managed backtrace not available\n");
827 return g_string_free (text, FALSE);
831 mono_exception_get_native_backtrace (MonoException *exc)
833 #ifdef HAVE_BACKTRACE_SYMBOLS
835 MonoArray *arr = exc->native_trace_ips;
841 return g_strdup ("");
842 domain = mono_domain_get ();
843 len = mono_array_length (arr);
844 text = g_string_new_len (NULL, len * 20);
845 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
848 for (i = 0; i < len; ++i) {
849 gpointer ip = mono_array_get (arr, gpointer, i);
850 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
852 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
853 g_string_append_printf (text, "%s\n", msg);
856 g_string_append_printf (text, "%s\n", messages [i]);
861 return g_string_free (text, FALSE);
863 return g_strdup ("");
868 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
873 mono_raise_exception (mono_get_exception_argument_null ("exception"));
875 trace = mono_exception_get_native_backtrace (exc);
876 res = mono_string_new (mono_domain_get (), trace);
882 * mono_error_raise_exception:
883 * @target_error: the exception to raise
885 * Raises the exception of @target_error.
886 * Does nothing if @target_error has a success error code.
887 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
888 * to construct the first exception object.
889 * The error object @target_error is cleaned up.
892 mono_error_raise_exception (MonoError *target_error)
894 MonoException *ex = mono_error_convert_to_exception (target_error);
896 mono_raise_exception (ex);
900 mono_error_set_pending_exception (MonoError *error)
902 MonoException *ex = mono_error_convert_to_exception (error);
904 mono_set_pending_exception (ex);