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)
94 klass = mono_class_get_checked (image, token, &error);
95 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
97 o = mono_object_new (mono_domain_get (), klass);
100 mono_runtime_object_init (o);
102 return (MonoException *)o;
105 static MonoException *
106 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
108 MonoDomain *domain = mono_domain_get ();
109 MonoMethod *method = NULL;
119 o = mono_object_new (domain, klass);
122 while ((m = mono_class_get_methods (klass, &iter))) {
123 MonoMethodSignature *sig;
125 if (strcmp (".ctor", mono_method_get_name (m)))
127 sig = mono_method_signature (m);
128 if (sig->param_count != count)
131 if (sig->params [0]->type != MONO_TYPE_STRING)
133 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
141 mono_runtime_invoke (method, o, args, NULL);
142 return (MonoException *) o;
146 * mono_exception_from_name_two_strings:
147 * @image: the Mono image where to look for the class
148 * @name_space: the namespace for the class
150 * @a1: first string argument to pass
151 * @a2: second string argument to pass
153 * Creates an exception from a constructor that takes two string
156 * Returns: the initialized exception instance.
159 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
160 const char *name, MonoString *a1, MonoString *a2)
162 MonoClass *klass = mono_class_from_name (image, name_space, name);
164 return create_exception_two_strings (klass, a1, a2);
168 * mono_exception_from_name_msg:
169 * @image: the Mono image where to look for the class
170 * @name_space: the namespace for the class
172 * @msg: the message to embed inside the exception
174 * Creates an exception and initializes its message field.
176 * Returns: the initialized exception instance.
179 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
180 const char *name, const char *msg)
184 ex = mono_exception_from_name (image, name_space, name);
187 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
193 * mono_exception_from_token_two_strings:
195 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
199 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
200 MonoString *a1, MonoString *a2)
203 MonoClass *klass = mono_class_get_checked (image, token, &error);
204 g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
206 return create_exception_two_strings (klass, a1, a2);
210 * mono_get_exception_divide_by_zero:
212 * Returns: a new instance of the System.DivideByZeroException
215 mono_get_exception_divide_by_zero ()
217 return mono_exception_from_name (mono_get_corlib (), "System",
218 "DivideByZeroException");
222 * mono_get_exception_security:
224 * Returns: a new instance of the System.Security.SecurityException
227 mono_get_exception_security ()
229 return mono_exception_from_name (mono_get_corlib (), "System.Security",
230 "SecurityException");
234 * mono_get_exception_thread_abort:
236 * Returns: a new instance of the System.Threading.ThreadAbortException.
239 mono_get_exception_thread_abort ()
241 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
242 "ThreadAbortException");
246 * mono_get_exception_thread_interrupted:
248 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
251 mono_get_exception_thread_interrupted ()
253 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
254 "ThreadInterruptedException");
258 * mono_get_exception_arithmetic:
260 * Returns: a new instance of the System.ArithmeticException.
263 mono_get_exception_arithmetic ()
265 return mono_exception_from_name (mono_get_corlib (), "System",
266 "ArithmeticException");
270 * mono_get_exception_overflow:
272 * Returns: a new instance of the System.OverflowException
275 mono_get_exception_overflow ()
277 return mono_exception_from_name (mono_get_corlib (), "System",
278 "OverflowException");
282 * mono_get_exception_null_reference:
284 * Returns: a new instance of the System.NullReferenceException
287 mono_get_exception_null_reference ()
289 return mono_exception_from_name (mono_get_corlib (), "System",
290 "NullReferenceException");
294 * mono_get_exception_execution_engine:
295 * @msg: the message to pass to the user
297 * Returns: a new instance of the System.ExecutionEngineException
300 mono_get_exception_execution_engine (const char *msg)
302 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
306 * mono_get_exception_serialization:
307 * @msg: the message to pass to the user
309 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
312 mono_get_exception_serialization (const char *msg)
314 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
318 * mono_get_exception_invalid_cast:
320 * Returns: a new instance of the System.InvalidCastException
323 mono_get_exception_invalid_cast ()
325 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
329 * mono_get_exception_invalid_operation:
330 * @msg: the message to pass to the user
332 * Returns: a new instance of the System.InvalidOperationException
335 mono_get_exception_invalid_operation (const char *msg)
337 return mono_exception_from_name_msg (mono_get_corlib (), "System",
338 "InvalidOperationException", msg);
342 * mono_get_exception_index_out_of_range:
344 * Returns: a new instance of the System.IndexOutOfRangeException
347 mono_get_exception_index_out_of_range ()
349 return mono_exception_from_name (mono_get_corlib (), "System",
350 "IndexOutOfRangeException");
354 * mono_get_exception_array_type_mismatch:
356 * Returns: a new instance of the System.ArrayTypeMismatchException
359 mono_get_exception_array_type_mismatch ()
361 return mono_exception_from_name (mono_get_corlib (), "System",
362 "ArrayTypeMismatchException");
366 * mono_get_exception_type_load:
367 * @class_name: the name of the class that could not be loaded
368 * @assembly_name: the assembly where the class was looked up.
370 * Returns: a new instance of the System.TypeLoadException.
373 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
375 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
377 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
378 "TypeLoadException", class_name, s);
382 * mono_get_exception_not_implemented:
383 * @msg: the message to pass to the user
385 * Returns: a new instance of the System.NotImplementedException
388 mono_get_exception_not_implemented (const char *msg)
390 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
394 * mono_get_exception_not_supported:
395 * @msg: the message to pass to the user
397 * Returns: a new instance of the System.NotSupportedException
400 mono_get_exception_not_supported (const char *msg)
402 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
406 * mono_get_exception_missing_method:
407 * @class_name: the class where the lookup was performed.
408 * @member_name: the name of the missing method.
410 * Returns: a new instance of the System.MissingMethodException
413 mono_get_exception_missing_method (const char *class_name, const char *member_name)
415 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
416 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
418 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
419 "MissingMethodException", s1, s2);
423 * mono_get_exception_missing_field:
424 * @class_name: the class where the lookup was performed
425 * @member_name: the name of the missing method.
427 * Returns: a new instance of the System.MissingFieldException
430 mono_get_exception_missing_field (const char *class_name, const char *member_name)
432 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
433 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
435 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
436 "MissingFieldException", s1, s2);
440 * mono_get_exception_argument_null:
441 * @arg: the name of the argument that is null
443 * Returns: a new instance of the System.ArgumentNullException
446 mono_get_exception_argument_null (const char *arg)
450 ex = mono_exception_from_name (
451 mono_get_corlib (), "System", "ArgumentNullException");
454 MonoArgumentException *argex = (MonoArgumentException *)ex;
455 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
462 * mono_get_exception_argument:
463 * @arg: the name of the invalid argument.
465 * Returns: a new instance of the System.ArgumentException
468 mono_get_exception_argument (const char *arg, const char *msg)
472 ex = mono_exception_from_name_msg (
473 mono_get_corlib (), "System", "ArgumentException", msg);
476 MonoArgumentException *argex = (MonoArgumentException *)ex;
477 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
484 * mono_get_exception_argument_out_of_range:
485 * @arg: the name of the out of range argument.
487 * Returns: a new instance of the System.ArgumentOutOfRangeException
490 mono_get_exception_argument_out_of_range (const char *arg)
494 ex = mono_exception_from_name (
495 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
498 MonoArgumentException *argex = (MonoArgumentException *)ex;
499 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
506 * mono_get_exception_thread_state:
507 * @msg: the message to present to the user
509 * Returns: a new instance of the System.Threading.ThreadStateException
512 mono_get_exception_thread_state (const char *msg)
514 return mono_exception_from_name_msg (
515 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
519 * mono_get_exception_io:
520 * @msg: the message to present to the user
522 * Returns: a new instance of the System.IO.IOException
525 mono_get_exception_io (const char *msg)
527 return mono_exception_from_name_msg (
528 mono_get_corlib (), "System.IO", "IOException", msg);
532 * mono_get_exception_file_not_found:
533 * @fname: the name of the file not found.
535 * Returns: a new instance of the System.IO.FileNotFoundException
538 mono_get_exception_file_not_found (MonoString *fname)
540 return mono_exception_from_name_two_strings (
541 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
545 * mono_get_exception_file_not_found2:
546 * @msg: an informative message for the user.
547 * @fname: the name of the file not found.
549 * Returns: a new instance of the System.IO.FileNotFoundException
552 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
554 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
556 return mono_exception_from_name_two_strings (
557 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
561 * mono_get_exception_type_initialization:
562 * @type_name: the name of the type that failed to initialize.
563 * @inner: the inner exception.
565 * Returns: a new instance of the System.TypeInitializationException
568 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
576 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
579 mono_class_init (klass);
582 while ((method = mono_class_get_methods (klass, &iter))) {
583 if (!strcmp (".ctor", mono_method_get_name (method))) {
584 MonoMethodSignature *sig = mono_method_signature (method);
586 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
593 args [0] = mono_string_new (mono_domain_get (), type_name);
596 exc = mono_object_new (mono_domain_get (), klass);
597 mono_runtime_invoke (method, exc, args, NULL);
599 return (MonoException *) exc;
603 * mono_get_exception_synchronization_lock:
604 * @inner: the inner exception.
606 * Returns: a new instance of the System.SynchronizationLockException
609 mono_get_exception_synchronization_lock (const char *msg)
611 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
615 * mono_get_exception_cannot_unload_appdomain:
616 * @inner: the inner exception.
618 * Returns: a new instance of the System.CannotUnloadAppDomainException
621 mono_get_exception_cannot_unload_appdomain (const char *msg)
623 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
627 * mono_get_exception_appdomain_unloaded
629 * Returns: a new instance of the System.AppDomainUnloadedException
632 mono_get_exception_appdomain_unloaded (void)
634 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
638 * mono_get_exception_bad_image_format:
639 * @msg: an informative message for the user.
641 * Returns: a new instance of the System.BadImageFormatException
644 mono_get_exception_bad_image_format (const char *msg)
646 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
650 * mono_get_exception_bad_image_format2:
651 * @msg: an informative message for the user.
652 * @fname: The full name of the file with the invalid image.
654 * Returns: a new instance of the System.BadImageFormatException
657 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
659 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
661 return mono_exception_from_name_two_strings (
662 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
666 * mono_get_exception_stack_overflow:
668 * Returns: a new instance of the System.StackOverflowException
671 mono_get_exception_stack_overflow (void)
673 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
677 * mono_get_exception_out_of_memory:
679 * Returns: a new instance of the System.OutOfMemoryException
682 mono_get_exception_out_of_memory (void)
684 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
688 * mono_get_exception_field_access:
690 * Returns: a new instance of the System.FieldAccessException
693 mono_get_exception_field_access (void)
695 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
699 * mono_get_exception_field_access2:
700 * @msg: an informative message for the user.
702 * Returns: a new instance of the System.FieldAccessException
705 mono_get_exception_field_access_msg (const char *msg)
707 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
711 * mono_get_exception_method_access:
713 * Returns: a new instance of the System.MethodAccessException
716 mono_get_exception_method_access (void)
718 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
722 * mono_get_exception_method_access2:
723 * @msg: an informative message for the user.
725 * Returns: a new instance of the System.MethodAccessException
728 mono_get_exception_method_access_msg (const char *msg)
730 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
734 * mono_get_exception_reflection_type_load:
735 * @types: an array of types that were defined in the moduled loaded.
736 * @exceptions: an array of exceptions that were thrown during the type loading.
738 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
741 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
749 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
751 mono_class_init (klass);
753 /* Find the Type[], Exception[] ctor */
755 while ((method = mono_class_get_methods (klass, &iter))) {
756 if (!strcmp (".ctor", mono_method_get_name (method))) {
757 MonoMethodSignature *sig = mono_method_signature (method);
759 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
767 args [1] = exceptions;
769 exc = mono_object_new (mono_domain_get (), klass);
770 mono_runtime_invoke (method, exc, args, NULL);
772 return (MonoException *) exc;
776 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
781 MonoDomain *domain = mono_domain_get ();
782 gpointer params [16];
784 klass = mono_class_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
787 o = mono_object_new (domain, klass);
788 g_assert (o != NULL);
790 method = mono_class_get_method_from_name (klass, ".ctor", 1);
793 params [0] = wrapped_exception;
794 mono_runtime_invoke (method, o, params, NULL);
796 return (MonoException *)o;
800 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
802 MonoDomain *domain = mono_domain_get ();
803 GString *text = (GString*)user_data;
806 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
807 g_string_append_printf (text, "%s\n", msg);
810 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
817 mono_exception_get_managed_backtrace (MonoException *exc)
821 text = g_string_new_len (NULL, 20);
823 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
824 g_string_append (text, "managed backtrace not available\n");
826 return g_string_free (text, FALSE);
830 mono_exception_get_native_backtrace (MonoException *exc)
832 #ifdef HAVE_BACKTRACE_SYMBOLS
834 MonoArray *arr = exc->native_trace_ips;
840 return g_strdup ("");
841 domain = mono_domain_get ();
842 len = mono_array_length (arr);
843 text = g_string_new_len (NULL, len * 20);
844 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
847 for (i = 0; i < len; ++i) {
848 gpointer ip = mono_array_get (arr, gpointer, i);
849 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
851 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
852 g_string_append_printf (text, "%s\n", msg);
855 g_string_append_printf (text, "%s\n", messages [i]);
860 return g_string_free (text, FALSE);
862 return g_strdup ("");
867 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
872 mono_raise_exception (mono_get_exception_argument_null ("exception"));
874 trace = mono_exception_get_native_backtrace (exc);
875 res = mono_string_new (mono_domain_get (), trace);