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)
64 MonoDomain *caller_domain = mono_domain_get ();
66 klass = mono_class_from_name (image, name_space, name);
68 o = mono_object_new (domain, klass);
71 if (domain != caller_domain)
72 mono_domain_set_internal (domain);
73 mono_runtime_object_init (o);
74 if (domain != caller_domain)
75 mono_domain_set_internal (caller_domain);
77 return (MonoException *)o;
82 * mono_exception_from_token:
83 * @image: the Mono image where to look for the class
84 * @token: The type token of the class
86 * Creates an exception of the type given by @token.
88 * Returns: the initialized exception instance.
91 mono_exception_from_token (MonoImage *image, guint32 token)
97 klass = mono_class_get_checked (image, token, &error);
98 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
100 o = mono_object_new (mono_domain_get (), klass);
101 g_assert (o != NULL);
103 mono_runtime_object_init (o);
105 return (MonoException *)o;
108 static MonoException *
109 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
111 MonoDomain *domain = mono_domain_get ();
112 MonoMethod *method = NULL;
122 o = mono_object_new (domain, klass);
125 while ((m = mono_class_get_methods (klass, &iter))) {
126 MonoMethodSignature *sig;
128 if (strcmp (".ctor", mono_method_get_name (m)))
130 sig = mono_method_signature (m);
131 if (sig->param_count != count)
134 if (sig->params [0]->type != MONO_TYPE_STRING)
136 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
144 mono_runtime_invoke (method, o, args, NULL);
145 return (MonoException *) o;
149 * mono_exception_from_name_two_strings:
150 * @image: the Mono image where to look for the class
151 * @name_space: the namespace for the class
153 * @a1: first string argument to pass
154 * @a2: second string argument to pass
156 * Creates an exception from a constructor that takes two string
159 * Returns: the initialized exception instance.
162 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
163 const char *name, MonoString *a1, MonoString *a2)
165 MonoClass *klass = mono_class_from_name (image, name_space, name);
167 return create_exception_two_strings (klass, a1, a2);
171 * mono_exception_from_name_msg:
172 * @image: the Mono image where to look for the class
173 * @name_space: the namespace for the class
175 * @msg: the message to embed inside the exception
177 * Creates an exception and initializes its message field.
179 * Returns: the initialized exception instance.
182 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
183 const char *name, const char *msg)
187 ex = mono_exception_from_name (image, name_space, name);
190 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
196 * mono_exception_from_token_two_strings:
198 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
202 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
203 MonoString *a1, MonoString *a2)
206 MonoClass *klass = mono_class_get_checked (image, token, &error);
207 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
209 return create_exception_two_strings (klass, a1, a2);
213 * mono_get_exception_divide_by_zero:
215 * Returns: a new instance of the System.DivideByZeroException
218 mono_get_exception_divide_by_zero ()
220 return mono_exception_from_name (mono_get_corlib (), "System",
221 "DivideByZeroException");
225 * mono_get_exception_security:
227 * Returns: a new instance of the System.Security.SecurityException
230 mono_get_exception_security ()
232 return mono_exception_from_name (mono_get_corlib (), "System.Security",
233 "SecurityException");
237 * mono_get_exception_thread_abort:
239 * Returns: a new instance of the System.Threading.ThreadAbortException.
242 mono_get_exception_thread_abort ()
244 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
245 "ThreadAbortException");
249 * mono_get_exception_thread_interrupted:
251 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
254 mono_get_exception_thread_interrupted ()
256 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
257 "ThreadInterruptedException");
261 * mono_get_exception_arithmetic:
263 * Returns: a new instance of the System.ArithmeticException.
266 mono_get_exception_arithmetic ()
268 return mono_exception_from_name (mono_get_corlib (), "System",
269 "ArithmeticException");
273 * mono_get_exception_overflow:
275 * Returns: a new instance of the System.OverflowException
278 mono_get_exception_overflow ()
280 return mono_exception_from_name (mono_get_corlib (), "System",
281 "OverflowException");
285 * mono_get_exception_null_reference:
287 * Returns: a new instance of the System.NullReferenceException
290 mono_get_exception_null_reference ()
292 return mono_exception_from_name (mono_get_corlib (), "System",
293 "NullReferenceException");
297 * mono_get_exception_execution_engine:
298 * @msg: the message to pass to the user
300 * Returns: a new instance of the System.ExecutionEngineException
303 mono_get_exception_execution_engine (const char *msg)
305 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
309 * mono_get_exception_serialization:
310 * @msg: the message to pass to the user
312 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
315 mono_get_exception_serialization (const char *msg)
317 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
321 * mono_get_exception_invalid_cast:
323 * Returns: a new instance of the System.InvalidCastException
326 mono_get_exception_invalid_cast ()
328 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
332 * mono_get_exception_invalid_operation:
333 * @msg: the message to pass to the user
335 * Returns: a new instance of the System.InvalidOperationException
338 mono_get_exception_invalid_operation (const char *msg)
340 return mono_exception_from_name_msg (mono_get_corlib (), "System",
341 "InvalidOperationException", msg);
345 * mono_get_exception_index_out_of_range:
347 * Returns: a new instance of the System.IndexOutOfRangeException
350 mono_get_exception_index_out_of_range ()
352 return mono_exception_from_name (mono_get_corlib (), "System",
353 "IndexOutOfRangeException");
357 * mono_get_exception_array_type_mismatch:
359 * Returns: a new instance of the System.ArrayTypeMismatchException
362 mono_get_exception_array_type_mismatch ()
364 return mono_exception_from_name (mono_get_corlib (), "System",
365 "ArrayTypeMismatchException");
369 * mono_get_exception_type_load:
370 * @class_name: the name of the class that could not be loaded
371 * @assembly_name: the assembly where the class was looked up.
373 * Returns: a new instance of the System.TypeLoadException.
376 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
378 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
380 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
381 "TypeLoadException", class_name, s);
385 * mono_get_exception_not_implemented:
386 * @msg: the message to pass to the user
388 * Returns: a new instance of the System.NotImplementedException
391 mono_get_exception_not_implemented (const char *msg)
393 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
397 * mono_get_exception_not_supported:
398 * @msg: the message to pass to the user
400 * Returns: a new instance of the System.NotSupportedException
403 mono_get_exception_not_supported (const char *msg)
405 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
409 * mono_get_exception_missing_method:
410 * @class_name: the class where the lookup was performed.
411 * @member_name: the name of the missing method.
413 * Returns: a new instance of the System.MissingMethodException
416 mono_get_exception_missing_method (const char *class_name, const char *member_name)
418 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
419 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
421 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
422 "MissingMethodException", s1, s2);
426 * mono_get_exception_missing_field:
427 * @class_name: the class where the lookup was performed
428 * @member_name: the name of the missing method.
430 * Returns: a new instance of the System.MissingFieldException
433 mono_get_exception_missing_field (const char *class_name, const char *member_name)
435 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
436 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
438 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
439 "MissingFieldException", s1, s2);
443 * mono_get_exception_argument_null:
444 * @arg: the name of the argument that is null
446 * Returns: a new instance of the System.ArgumentNullException
449 mono_get_exception_argument_null (const char *arg)
453 ex = mono_exception_from_name (
454 mono_get_corlib (), "System", "ArgumentNullException");
457 MonoArgumentException *argex = (MonoArgumentException *)ex;
458 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
465 * mono_get_exception_argument:
466 * @arg: the name of the invalid argument.
468 * Returns: a new instance of the System.ArgumentException
471 mono_get_exception_argument (const char *arg, const char *msg)
475 ex = mono_exception_from_name_msg (
476 mono_get_corlib (), "System", "ArgumentException", msg);
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_out_of_range:
488 * @arg: the name of the out of range argument.
490 * Returns: a new instance of the System.ArgumentOutOfRangeException
493 mono_get_exception_argument_out_of_range (const char *arg)
497 ex = mono_exception_from_name (
498 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
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_thread_state:
510 * @msg: the message to present to the user
512 * Returns: a new instance of the System.Threading.ThreadStateException
515 mono_get_exception_thread_state (const char *msg)
517 return mono_exception_from_name_msg (
518 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
522 * mono_get_exception_io:
523 * @msg: the message to present to the user
525 * Returns: a new instance of the System.IO.IOException
528 mono_get_exception_io (const char *msg)
530 return mono_exception_from_name_msg (
531 mono_get_corlib (), "System.IO", "IOException", msg);
535 * mono_get_exception_file_not_found:
536 * @fname: the name of the file not found.
538 * Returns: a new instance of the System.IO.FileNotFoundException
541 mono_get_exception_file_not_found (MonoString *fname)
543 return mono_exception_from_name_two_strings (
544 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
548 * mono_get_exception_file_not_found2:
549 * @msg: an informative message for the user.
550 * @fname: the name of the file not found.
552 * Returns: a new instance of the System.IO.FileNotFoundException
555 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
557 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
559 return mono_exception_from_name_two_strings (
560 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
564 * mono_get_exception_type_initialization:
565 * @type_name: the name of the type that failed to initialize.
566 * @inner: the inner exception.
568 * Returns: a new instance of the System.TypeInitializationException
571 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
579 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
582 mono_class_init (klass);
585 while ((method = mono_class_get_methods (klass, &iter))) {
586 if (!strcmp (".ctor", mono_method_get_name (method))) {
587 MonoMethodSignature *sig = mono_method_signature (method);
589 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
596 args [0] = mono_string_new (mono_domain_get (), type_name);
599 exc = mono_object_new (mono_domain_get (), klass);
600 mono_runtime_invoke (method, exc, args, NULL);
602 return (MonoException *) exc;
606 * mono_get_exception_synchronization_lock:
607 * @inner: the inner exception.
609 * Returns: a new instance of the System.SynchronizationLockException
612 mono_get_exception_synchronization_lock (const char *msg)
614 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
618 * mono_get_exception_cannot_unload_appdomain:
619 * @inner: the inner exception.
621 * Returns: a new instance of the System.CannotUnloadAppDomainException
624 mono_get_exception_cannot_unload_appdomain (const char *msg)
626 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
630 * mono_get_exception_appdomain_unloaded
632 * Returns: a new instance of the System.AppDomainUnloadedException
635 mono_get_exception_appdomain_unloaded (void)
637 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
641 * mono_get_exception_bad_image_format:
642 * @msg: an informative message for the user.
644 * Returns: a new instance of the System.BadImageFormatException
647 mono_get_exception_bad_image_format (const char *msg)
649 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
653 * mono_get_exception_bad_image_format2:
654 * @msg: an informative message for the user.
655 * @fname: The full name of the file with the invalid image.
657 * Returns: a new instance of the System.BadImageFormatException
660 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
662 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
664 return mono_exception_from_name_two_strings (
665 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
669 * mono_get_exception_stack_overflow:
671 * Returns: a new instance of the System.StackOverflowException
674 mono_get_exception_stack_overflow (void)
676 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
680 * mono_get_exception_out_of_memory:
682 * Returns: a new instance of the System.OutOfMemoryException
685 mono_get_exception_out_of_memory (void)
687 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
691 * mono_get_exception_field_access:
693 * Returns: a new instance of the System.FieldAccessException
696 mono_get_exception_field_access (void)
698 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
702 * mono_get_exception_field_access2:
703 * @msg: an informative message for the user.
705 * Returns: a new instance of the System.FieldAccessException
708 mono_get_exception_field_access_msg (const char *msg)
710 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
714 * mono_get_exception_method_access:
716 * Returns: a new instance of the System.MethodAccessException
719 mono_get_exception_method_access (void)
721 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
725 * mono_get_exception_method_access2:
726 * @msg: an informative message for the user.
728 * Returns: a new instance of the System.MethodAccessException
731 mono_get_exception_method_access_msg (const char *msg)
733 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
737 * mono_get_exception_reflection_type_load:
738 * @types: an array of types that were defined in the moduled loaded.
739 * @exceptions: an array of exceptions that were thrown during the type loading.
741 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
744 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
752 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
754 mono_class_init (klass);
756 /* Find the Type[], Exception[] ctor */
758 while ((method = mono_class_get_methods (klass, &iter))) {
759 if (!strcmp (".ctor", mono_method_get_name (method))) {
760 MonoMethodSignature *sig = mono_method_signature (method);
762 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
770 args [1] = exceptions;
772 exc = mono_object_new (mono_domain_get (), klass);
773 mono_runtime_invoke (method, exc, args, NULL);
775 return (MonoException *) exc;
779 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
784 MonoDomain *domain = mono_domain_get ();
785 gpointer params [16];
787 klass = mono_class_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
790 o = mono_object_new (domain, klass);
791 g_assert (o != NULL);
793 method = mono_class_get_method_from_name (klass, ".ctor", 1);
796 params [0] = wrapped_exception;
797 mono_runtime_invoke (method, o, params, NULL);
799 return (MonoException *)o;
803 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
805 MonoDomain *domain = mono_domain_get ();
806 GString *text = (GString*)user_data;
809 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
810 g_string_append_printf (text, "%s\n", msg);
813 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
820 mono_exception_get_managed_backtrace (MonoException *exc)
824 text = g_string_new_len (NULL, 20);
826 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
827 g_string_append (text, "managed backtrace not available\n");
829 return g_string_free (text, FALSE);
833 mono_exception_get_native_backtrace (MonoException *exc)
835 #ifdef HAVE_BACKTRACE_SYMBOLS
837 MonoArray *arr = exc->native_trace_ips;
843 return g_strdup ("");
844 domain = mono_domain_get ();
845 len = mono_array_length (arr);
846 text = g_string_new_len (NULL, len * 20);
847 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
850 for (i = 0; i < len; ++i) {
851 gpointer ip = mono_array_get (arr, gpointer, i);
852 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
854 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
855 g_string_append_printf (text, "%s\n", msg);
858 g_string_append_printf (text, "%s\n", messages [i]);
863 return g_string_free (text, FALSE);
865 return g_strdup ("");
870 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
875 mono_raise_exception (mono_get_exception_argument_null ("exception"));
877 trace = mono_exception_get_native_backtrace (exc);
878 res = mono_string_new (mono_domain_get (), trace);
884 * mono_error_raise_exception:
885 * @target_error: the exception to raise
887 * Raises the exception of @target_error.
888 * Does nothing if @target_error has a success error code.
889 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
890 * to construct the first exception object.
891 * The error object @target_error is cleaned up.
894 mono_error_raise_exception (MonoError *target_error)
896 MonoException *ex = mono_error_convert_to_exception (target_error);
898 mono_raise_exception (ex);
902 mono_error_set_pending_exception (MonoError *error)
904 MonoException *ex = mono_error_convert_to_exception (error);
906 mono_set_pending_exception (ex);