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)
93 klass = mono_class_get (image, token);
95 o = mono_object_new (mono_domain_get (), klass);
98 mono_runtime_object_init (o);
100 return (MonoException *)o;
103 static MonoException *
104 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
106 MonoDomain *domain = mono_domain_get ();
107 MonoMethod *method = NULL;
117 o = mono_object_new (domain, klass);
120 while ((m = mono_class_get_methods (klass, &iter))) {
121 MonoMethodSignature *sig;
123 if (strcmp (".ctor", mono_method_get_name (m)))
125 sig = mono_method_signature (m);
126 if (sig->param_count != count)
129 if (sig->params [0]->type != MONO_TYPE_STRING)
131 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
139 mono_runtime_invoke (method, o, args, NULL);
140 return (MonoException *) o;
144 * mono_exception_from_name_two_strings:
145 * @image: the Mono image where to look for the class
146 * @name_space: the namespace for the class
148 * @a1: first string argument to pass
149 * @a2: second string argument to pass
151 * Creates an exception from a constructor that takes two string
154 * Returns: the initialized exception instance.
157 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
158 const char *name, MonoString *a1, MonoString *a2)
160 MonoClass *klass = mono_class_from_name (image, name_space, name);
162 return create_exception_two_strings (klass, a1, a2);
166 * mono_exception_from_name_msg:
167 * @image: the Mono image where to look for the class
168 * @name_space: the namespace for the class
170 * @msg: the message to embed inside the exception
172 * Creates an exception and initializes its message field.
174 * Returns: the initialized exception instance.
177 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
178 const char *name, const char *msg)
182 ex = mono_exception_from_name (image, name_space, name);
185 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
191 * mono_exception_from_token_two_strings:
193 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
197 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
198 MonoString *a1, MonoString *a2)
200 MonoClass *klass = mono_class_get (image, token);
202 return create_exception_two_strings (klass, a1, a2);
206 * mono_get_exception_divide_by_zero:
208 * Returns: a new instance of the System.DivideByZeroException
211 mono_get_exception_divide_by_zero ()
213 return mono_exception_from_name (mono_get_corlib (), "System",
214 "DivideByZeroException");
218 * mono_get_exception_security:
220 * Returns: a new instance of the System.Security.SecurityException
223 mono_get_exception_security ()
225 return mono_exception_from_name (mono_get_corlib (), "System.Security",
226 "SecurityException");
230 * mono_get_exception_thread_abort:
232 * Returns: a new instance of the System.Threading.ThreadAbortException.
235 mono_get_exception_thread_abort ()
237 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
238 "ThreadAbortException");
242 * mono_get_exception_thread_interrupted:
244 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
247 mono_get_exception_thread_interrupted ()
249 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
250 "ThreadInterruptedException");
254 * mono_get_exception_arithmetic:
256 * Returns: a new instance of the System.ArithmeticException.
259 mono_get_exception_arithmetic ()
261 return mono_exception_from_name (mono_get_corlib (), "System",
262 "ArithmeticException");
266 * mono_get_exception_overflow:
268 * Returns: a new instance of the System.OverflowException
271 mono_get_exception_overflow ()
273 return mono_exception_from_name (mono_get_corlib (), "System",
274 "OverflowException");
278 * mono_get_exception_null_reference:
280 * Returns: a new instance of the System.NullReferenceException
283 mono_get_exception_null_reference ()
285 return mono_exception_from_name (mono_get_corlib (), "System",
286 "NullReferenceException");
290 * mono_get_exception_execution_engine:
291 * @msg: the message to pass to the user
293 * Returns: a new instance of the System.ExecutionEngineException
296 mono_get_exception_execution_engine (const char *msg)
298 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
302 * mono_get_exception_serialization:
303 * @msg: the message to pass to the user
305 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
308 mono_get_exception_serialization (const char *msg)
310 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
314 * mono_get_exception_invalid_cast:
316 * Returns: a new instance of the System.InvalidCastException
319 mono_get_exception_invalid_cast ()
321 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
325 * mono_get_exception_invalid_operation:
326 * @msg: the message to pass to the user
328 * Returns: a new instance of the System.InvalidOperationException
331 mono_get_exception_invalid_operation (const char *msg)
333 return mono_exception_from_name_msg (mono_get_corlib (), "System",
334 "InvalidOperationException", msg);
338 * mono_get_exception_index_out_of_range:
340 * Returns: a new instance of the System.IndexOutOfRangeException
343 mono_get_exception_index_out_of_range ()
345 return mono_exception_from_name (mono_get_corlib (), "System",
346 "IndexOutOfRangeException");
350 * mono_get_exception_array_type_mismatch:
352 * Returns: a new instance of the System.ArrayTypeMismatchException
355 mono_get_exception_array_type_mismatch ()
357 return mono_exception_from_name (mono_get_corlib (), "System",
358 "ArrayTypeMismatchException");
362 * mono_get_exception_type_load:
363 * @class_name: the name of the class that could not be loaded
364 * @assembly_name: the assembly where the class was looked up.
366 * Returns: a new instance of the System.TypeLoadException.
369 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
371 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
373 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
374 "TypeLoadException", class_name, s);
378 * mono_get_exception_not_implemented:
379 * @msg: the message to pass to the user
381 * Returns: a new instance of the System.NotImplementedException
384 mono_get_exception_not_implemented (const char *msg)
386 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
390 * mono_get_exception_not_supported:
391 * @msg: the message to pass to the user
393 * Returns: a new instance of the System.NotSupportedException
396 mono_get_exception_not_supported (const char *msg)
398 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
402 * mono_get_exception_missing_method:
403 * @class_name: the class where the lookup was performed.
404 * @member_name: the name of the missing method.
406 * Returns: a new instance of the System.MissingMethodException
409 mono_get_exception_missing_method (const char *class_name, const char *member_name)
411 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
412 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
414 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
415 "MissingMethodException", s1, s2);
419 * mono_get_exception_missing_field:
420 * @class_name: the class where the lookup was performed
421 * @member_name: the name of the missing method.
423 * Returns: a new instance of the System.MissingFieldException
426 mono_get_exception_missing_field (const char *class_name, const char *member_name)
428 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
429 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
431 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
432 "MissingFieldException", s1, s2);
436 * mono_get_exception_argument_null:
437 * @arg: the name of the argument that is null
439 * Returns: a new instance of the System.ArgumentNullException
442 mono_get_exception_argument_null (const char *arg)
446 ex = mono_exception_from_name (
447 mono_get_corlib (), "System", "ArgumentNullException");
450 MonoArgumentException *argex = (MonoArgumentException *)ex;
451 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
458 * mono_get_exception_argument:
459 * @arg: the name of the invalid argument.
461 * Returns: a new instance of the System.ArgumentException
464 mono_get_exception_argument (const char *arg, const char *msg)
468 ex = mono_exception_from_name_msg (
469 mono_get_corlib (), "System", "ArgumentException", msg);
472 MonoArgumentException *argex = (MonoArgumentException *)ex;
473 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
480 * mono_get_exception_argument_out_of_range:
481 * @arg: the name of the out of range argument.
483 * Returns: a new instance of the System.ArgumentOutOfRangeException
486 mono_get_exception_argument_out_of_range (const char *arg)
490 ex = mono_exception_from_name (
491 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
494 MonoArgumentException *argex = (MonoArgumentException *)ex;
495 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
502 * mono_get_exception_thread_state:
503 * @msg: the message to present to the user
505 * Returns: a new instance of the System.Threading.ThreadStateException
508 mono_get_exception_thread_state (const char *msg)
510 return mono_exception_from_name_msg (
511 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
515 * mono_get_exception_io:
516 * @msg: the message to present to the user
518 * Returns: a new instance of the System.IO.IOException
521 mono_get_exception_io (const char *msg)
523 return mono_exception_from_name_msg (
524 mono_get_corlib (), "System.IO", "IOException", msg);
528 * mono_get_exception_file_not_found:
529 * @fname: the name of the file not found.
531 * Returns: a new instance of the System.IO.FileNotFoundException
534 mono_get_exception_file_not_found (MonoString *fname)
536 return mono_exception_from_name_two_strings (
537 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
541 * mono_get_exception_file_not_found2:
542 * @msg: an informative message for the user.
543 * @fname: the name of the file not found.
545 * Returns: a new instance of the System.IO.FileNotFoundException
548 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
550 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
552 return mono_exception_from_name_two_strings (
553 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
557 * mono_get_exception_type_initialization:
558 * @type_name: the name of the type that failed to initialize.
559 * @inner: the inner exception.
561 * Returns: a new instance of the System.TypeInitializationException
564 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
572 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
575 mono_class_init (klass);
578 while ((method = mono_class_get_methods (klass, &iter))) {
579 if (!strcmp (".ctor", mono_method_get_name (method))) {
580 MonoMethodSignature *sig = mono_method_signature (method);
582 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
589 args [0] = mono_string_new (mono_domain_get (), type_name);
592 exc = mono_object_new (mono_domain_get (), klass);
593 mono_runtime_invoke (method, exc, args, NULL);
595 return (MonoException *) exc;
599 * mono_get_exception_synchronization_lock:
600 * @inner: the inner exception.
602 * Returns: a new instance of the System.SynchronizationLockException
605 mono_get_exception_synchronization_lock (const char *msg)
607 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
611 * mono_get_exception_cannot_unload_appdomain:
612 * @inner: the inner exception.
614 * Returns: a new instance of the System.CannotUnloadAppDomainException
617 mono_get_exception_cannot_unload_appdomain (const char *msg)
619 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
623 * mono_get_exception_appdomain_unloaded
625 * Returns: a new instance of the System.AppDomainUnloadedException
628 mono_get_exception_appdomain_unloaded (void)
630 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
634 * mono_get_exception_bad_image_format:
635 * @msg: an informative message for the user.
637 * Returns: a new instance of the System.BadImageFormatException
640 mono_get_exception_bad_image_format (const char *msg)
642 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
646 * mono_get_exception_bad_image_format2:
647 * @msg: an informative message for the user.
648 * @fname: The full name of the file with the invalid image.
650 * Returns: a new instance of the System.BadImageFormatException
653 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
655 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
657 return mono_exception_from_name_two_strings (
658 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
662 * mono_get_exception_stack_overflow:
664 * Returns: a new instance of the System.StackOverflowException
667 mono_get_exception_stack_overflow (void)
669 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
673 * mono_get_exception_out_of_memory:
675 * Returns: a new instance of the System.OutOfMemoryException
678 mono_get_exception_out_of_memory (void)
680 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
684 * mono_get_exception_field_access:
686 * Returns: a new instance of the System.FieldAccessException
689 mono_get_exception_field_access (void)
691 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
695 * mono_get_exception_field_access2:
696 * @msg: an informative message for the user.
698 * Returns: a new instance of the System.FieldAccessException
701 mono_get_exception_field_access_msg (const char *msg)
703 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
707 * mono_get_exception_method_access:
709 * Returns: a new instance of the System.MethodAccessException
712 mono_get_exception_method_access (void)
714 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
718 * mono_get_exception_method_access2:
719 * @msg: an informative message for the user.
721 * Returns: a new instance of the System.MethodAccessException
724 mono_get_exception_method_access_msg (const char *msg)
726 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
730 * mono_get_exception_reflection_type_load:
731 * @types: an array of types that were defined in the moduled loaded.
732 * @exceptions: an array of exceptions that were thrown during the type loading.
734 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
737 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
745 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
747 mono_class_init (klass);
749 /* Find the Type[], Exception[] ctor */
751 while ((method = mono_class_get_methods (klass, &iter))) {
752 if (!strcmp (".ctor", mono_method_get_name (method))) {
753 MonoMethodSignature *sig = mono_method_signature (method);
755 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
763 args [1] = exceptions;
765 exc = mono_object_new (mono_domain_get (), klass);
766 mono_runtime_invoke (method, exc, args, NULL);
768 return (MonoException *) exc;
772 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
774 MonoRuntimeWrappedException *ex = (MonoRuntimeWrappedException*)
775 mono_exception_from_name (mono_get_corlib (), "System.Runtime.CompilerServices",
776 "RuntimeWrappedException");
778 MONO_OBJECT_SETREF (ex, wrapped_exception, wrapped_exception);
779 return (MonoException*)ex;
783 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
785 MonoDomain *domain = mono_domain_get ();
786 GString *text = (GString*)user_data;
789 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
790 g_string_append_printf (text, "%s\n", msg);
793 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
800 mono_exception_get_managed_backtrace (MonoException *exc)
804 text = g_string_new_len (NULL, 20);
806 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
807 g_string_append (text, "managed backtrace not available\n");
809 return g_string_free (text, FALSE);
813 mono_exception_get_native_backtrace (MonoException *exc)
815 #ifdef HAVE_BACKTRACE_SYMBOLS
817 MonoArray *arr = exc->native_trace_ips;
823 return g_strdup ("");
824 domain = mono_domain_get ();
825 len = mono_array_length (arr);
826 text = g_string_new_len (NULL, len * 20);
827 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
830 for (i = 0; i < len; ++i) {
831 gpointer ip = mono_array_get (arr, gpointer, i);
832 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
834 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
835 g_string_append_printf (text, "%s\n", msg);
838 g_string_append_printf (text, "%s\n", messages [i]);
843 return g_string_free (text, FALSE);
845 return g_strdup ("");
850 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
855 mono_raise_exception (mono_get_exception_argument_null ("exception"));
857 trace = mono_exception_get_native_backtrace (exc);
858 res = mono_string_new (mono_domain_get (), trace);