6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
8 * Dick Porter (dick@ximian.com)
9 * Miguel de Icaza (miguel@ximian.com)
11 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
12 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #include <mono/metadata/environment.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/exception-internals.h>
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/utils/mono-error-internals.h>
27 #include <mono/utils/mono-logger-internals.h>
30 #ifdef HAVE_EXECINFO_H
34 static MonoUnhandledExceptionFunc unhandled_exception_hook
= NULL
;
35 static gpointer unhandled_exception_hook_data
= NULL
;
37 static MonoExceptionHandle
38 mono_exception_new_by_name_domain (MonoDomain
*domain
, MonoImage
*image
,
39 const char* name_space
, const char *name
, MonoError
*error
);
42 * mono_exception_new_by_name:
43 * \param image the Mono image where to look for the class
44 * \param name_space the namespace for the class
45 * \param name class name
47 * Creates an exception of the given namespace/name class in the
50 * \returns the initialized exception instance.
52 static MonoExceptionHandle
53 mono_exception_new_by_name (MonoImage
*image
, const char *name_space
, const char *name
, MonoError
*error
)
55 return mono_exception_new_by_name_domain (mono_domain_get (), image
, name_space
, name
, error
);
59 * mono_exception_from_name:
60 * \param image the Mono image where to look for the class
61 * \param name_space the namespace for the class
62 * \param name class name
64 * Creates an exception of the given namespace/name class in the
67 * \returns the initialized exception instance.
70 mono_exception_from_name (MonoImage
*image
, const char *name_space
,
73 return mono_exception_from_name_domain (mono_domain_get (), image
, name_space
, name
);
77 * mono_exception_new_by_name_domain:
78 * \param domain Domain where the return object will be created.
79 * \param image the Mono image where to look for the class
80 * \param name_space the namespace for the class
81 * \param name class name
83 * Creates an exception object of the given namespace/name class on
86 * \returns the initialized exception instance.
88 static MonoExceptionHandle
89 mono_exception_new_by_name_domain (MonoDomain
*domain
, MonoImage
*image
,
90 const char* name_space
, const char *name
, MonoError
*error
)
92 HANDLE_FUNCTION_ENTER ()
94 MonoDomain
* const caller_domain
= mono_domain_get ();
96 MonoClass
* const klass
= mono_class_load_from_name (image
, name_space
, name
);
98 MonoObjectHandle o
= mono_object_new_handle (domain
, klass
, error
);
99 goto_if_nok (error
, return_null
);
101 if (domain
!= caller_domain
)
102 mono_domain_set_internal (domain
);
104 mono_runtime_object_init_handle (o
, error
);
105 mono_error_assert_ok (error
);
107 // Restore domain in success and error path.
108 if (domain
!= caller_domain
)
109 mono_domain_set_internal (caller_domain
);
111 goto_if_ok (error
, exit
);
113 MONO_HANDLE_ASSIGN (o
, NULL_HANDLE
);
115 HANDLE_FUNCTION_RETURN_REF (MonoException
, MONO_HANDLE_CAST (MonoException
, o
));
119 * mono_exception_from_name_domain:
120 * \param domain Domain where the return object will be created.
121 * \param image the Mono image where to look for the class
122 * \param name_space the namespace for the class
123 * \param name class name
125 * Creates an exception object of the given namespace/name class on
128 * \returns the initialized exception instance.
131 mono_exception_from_name_domain (MonoDomain
*domain
, MonoImage
*image
,
132 const char* name_space
, const char *name
)
137 MonoDomain
*caller_domain
= mono_domain_get ();
139 klass
= mono_class_load_from_name (image
, name_space
, name
);
141 o
= mono_object_new_checked (domain
, klass
, error
);
142 mono_error_assert_ok (error
);
144 if (domain
!= caller_domain
)
145 mono_domain_set_internal (domain
);
146 mono_runtime_object_init_checked (o
, error
);
147 mono_error_assert_ok (error
);
149 if (domain
!= caller_domain
)
150 mono_domain_set_internal (caller_domain
);
152 return (MonoException
*)o
;
157 * mono_exception_from_token:
158 * \param image the Mono image where to look for the class
159 * \param token The type token of the class
161 * Creates an exception of the type given by \p token.
163 * \returns the initialized exception instance.
166 mono_exception_from_token (MonoImage
*image
, guint32 token
)
172 klass
= mono_class_get_checked (image
, token
, error
);
173 mono_error_assert_ok (error
);
175 o
= mono_object_new_checked (mono_domain_get (), klass
, error
);
176 mono_error_assert_ok (error
);
178 mono_runtime_object_init_checked (o
, error
);
179 mono_error_assert_ok (error
);
181 return (MonoException
*)o
;
184 static MonoException
*
185 create_exception_two_strings (MonoClass
*klass
, MonoString
*a1
, MonoString
*a2
, MonoError
*error
)
187 MonoDomain
*domain
= mono_domain_get ();
188 MonoMethod
*method
= NULL
;
198 o
= mono_object_new_checked (domain
, klass
, error
);
199 mono_error_assert_ok (error
);
202 while ((m
= mono_class_get_methods (klass
, &iter
))) {
203 MonoMethodSignature
*sig
;
205 if (strcmp (".ctor", mono_method_get_name (m
)))
207 sig
= mono_method_signature (m
);
208 if (sig
->param_count
!= count
)
211 if (sig
->params
[0]->type
!= MONO_TYPE_STRING
)
213 if (count
== 2 && sig
->params
[1]->type
!= MONO_TYPE_STRING
)
222 mono_runtime_invoke_checked (method
, o
, args
, error
);
223 return_val_if_nok (error
, NULL
);
225 return (MonoException
*) o
;
229 * mono_exception_from_name_two_strings:
230 * \param image the Mono image where to look for the class
231 * \param name_space the namespace for the class
232 * \param name class name
233 * \param a1 first string argument to pass
234 * \param a2 second string argument to pass
236 * Creates an exception from a constructor that takes two string
239 * \returns the initialized exception instance.
242 mono_exception_from_name_two_strings (MonoImage
*image
, const char *name_space
,
243 const char *name
, MonoString
*a1
, MonoString
*a2
)
248 ret
= mono_exception_from_name_two_strings_checked (image
, name_space
, name
, a1
, a2
, error
);
249 mono_error_cleanup (error
);
254 * mono_exception_from_name_two_strings_checked:
255 * \param image the Mono image where to look for the class
256 * \param name_space the namespace for the class
257 * \param name class name
258 * \param a1 first string argument to pass
259 * \param a2 second string argument to pass
260 * \param error set on error
262 * Creates an exception from a constructor that takes two string
265 * \returns the initialized exception instance. On failure returns
266 * NULL and sets \p error.
269 mono_exception_from_name_two_strings_checked (MonoImage
*image
, const char *name_space
,
270 const char *name
, MonoString
*a1
, MonoString
*a2
,
276 klass
= mono_class_load_from_name (image
, name_space
, name
);
278 return create_exception_two_strings (klass
, a1
, a2
, error
);
282 * mono_exception_new_by_name_msg:
283 * \param image the Mono image where to look for the class
284 * \param name_space the namespace for the class
285 * \param name class name
286 * \param msg the message to embed inside the exception
288 * Creates an exception and initializes its message field.
290 * \returns the initialized exception instance.
292 static MonoExceptionHandle
293 mono_exception_new_by_name_msg (MonoImage
*image
, const char *name_space
,
294 const char *name
, const char *msg
, MonoError
*error
)
296 HANDLE_FUNCTION_ENTER ()
298 MonoExceptionHandle ex
= mono_exception_new_by_name (image
, name_space
, name
, error
);
299 goto_if_nok (error
, return_null
);
302 MonoStringHandle msg_str
= mono_string_new_handle (MONO_HANDLE_DOMAIN (ex
), msg
, error
);
303 // FIXME? Maybe just ignore this error, the exception is close to correct.
304 goto_if_nok (error
, return_null
);
305 // ex->message = msg_str;
306 MONO_HANDLE_SET (ex
, message
, msg_str
);
310 MONO_HANDLE_ASSIGN (ex
, NULL_HANDLE
);
312 HANDLE_FUNCTION_RETURN_REF (MonoException
, ex
)
316 * mono_exception_from_name_msg:
317 * \param image the Mono image where to look for the class
318 * \param name_space the namespace for the class
319 * \param name class name
320 * \param msg the message to embed inside the exception
322 * Creates an exception and initializes its message field.
324 * \returns the initialized exception instance.
327 mono_exception_from_name_msg (MonoImage
*image
, const char *name_space
,
328 const char *name
, const char *msg
)
333 ex
= mono_exception_from_name (image
, name_space
, name
);
336 MonoString
*msg_str
= mono_string_new_checked (mono_object_get_domain ((MonoObject
*)ex
), msg
, error
);
337 mono_error_assert_ok (error
);
338 MONO_OBJECT_SETREF (ex
, message
, msg_str
);
345 * mono_exception_from_token_two_strings:
347 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
351 mono_exception_from_token_two_strings (MonoImage
*image
, guint32 token
,
352 MonoString
*a1
, MonoString
*a2
)
356 ret
= mono_exception_from_token_two_strings_checked (image
, token
, a1
, a2
, error
);
357 mono_error_cleanup (error
);
362 * mono_exception_from_token_two_strings_checked:
364 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
368 mono_exception_from_token_two_strings_checked (MonoImage
*image
, guint32 token
,
369 MonoString
*a1
, MonoString
*a2
,
376 klass
= mono_class_get_checked (image
, token
, error
);
377 mono_error_assert_ok (error
); /* FIXME handle the error. */
379 return create_exception_two_strings (klass
, a1
, a2
, error
);
383 * mono_get_exception_divide_by_zero:
384 * \returns a new instance of the \c System.DivideByZeroException
387 mono_get_exception_divide_by_zero (void)
389 return mono_exception_from_name (mono_get_corlib (), "System",
390 "DivideByZeroException");
394 * mono_get_exception_security:
395 * \returns a new instance of the \c System.Security.SecurityException
398 mono_get_exception_security ()
400 return mono_exception_from_name (mono_get_corlib (), "System.Security",
401 "SecurityException");
405 * mono_exception_new_thread_abort:
406 * \returns a new instance of the \c System.Threading.ThreadAbortException
409 mono_exception_new_thread_abort (MonoError
*error
)
411 return mono_exception_new_by_name (mono_get_corlib (), "System.Threading", "ThreadAbortException", error
);
415 * mono_get_exception_thread_abort:
416 * \returns a new instance of the \c System.Threading.ThreadAbortException
419 mono_get_exception_thread_abort ()
421 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
422 "ThreadAbortException");
426 * mono_exception_new_thread_interrupted:
427 * \returns a new instance of the \c System.Threading.ThreadInterruptedException
430 mono_exception_new_thread_interrupted (MonoError
*error
)
432 return mono_exception_new_by_name (mono_get_corlib (), "System.Threading", "ThreadInterruptedException", error
);
436 * mono_get_exception_thread_interrupted:
437 * \returns a new instance of the \c System.Threading.ThreadInterruptedException
440 mono_get_exception_thread_interrupted (void)
442 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
443 "ThreadInterruptedException");
447 * mono_get_exception_arithmetic:
448 * \returns a new instance of the \c System.ArithmeticException
451 mono_get_exception_arithmetic (void)
453 return mono_exception_from_name (mono_get_corlib (), "System",
454 "ArithmeticException");
458 * mono_get_exception_overflow:
459 * \returns a new instance of the \c System.OverflowException
462 mono_get_exception_overflow (void)
464 return mono_exception_from_name (mono_get_corlib (), "System",
465 "OverflowException");
469 * mono_get_exception_null_reference:
470 * \returns a new instance of the \c System.NullReferenceException
473 mono_get_exception_null_reference (void)
475 return mono_exception_from_name (mono_get_corlib (), "System",
476 "NullReferenceException");
480 * mono_get_exception_execution_engine:
481 * \param msg the message to pass to the user
482 * \returns a new instance of the \c System.ExecutionEngineException
485 mono_get_exception_execution_engine (const char *msg
)
487 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg
);
491 * mono_get_exception_serialization:
492 * \param msg the message to pass to the user
493 * \returns a new instance of the \c System.Runtime.Serialization.SerializationException
496 mono_get_exception_serialization (const char *msg
)
498 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg
);
502 * mono_get_exception_invalid_cast:
503 * \returns a new instance of the \c System.InvalidCastException
506 mono_get_exception_invalid_cast ()
508 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
512 * mono_get_exception_invalid_operation:
513 * \param msg the message to pass to the user
514 * \returns a new instance of the \c System.InvalidOperationException
517 mono_get_exception_invalid_operation (const char *msg
)
519 return mono_exception_from_name_msg (mono_get_corlib (), "System",
520 "InvalidOperationException", msg
);
524 mono_exception_new_invalid_operation (const char *msg
, MonoError
*error
)
526 return mono_exception_new_by_name_msg (mono_get_corlib (), "System",
527 "InvalidOperationException", msg
, error
);
531 * mono_get_exception_index_out_of_range:
532 * \returns a new instance of the \c System.IndexOutOfRangeException
535 mono_get_exception_index_out_of_range ()
537 return mono_exception_from_name (mono_get_corlib (), "System",
538 "IndexOutOfRangeException");
542 * mono_get_exception_array_type_mismatch:
543 * \returns a new instance of the \c System.ArrayTypeMismatchException
546 mono_get_exception_array_type_mismatch (void)
548 return mono_exception_from_name (mono_get_corlib (), "System",
549 "ArrayTypeMismatchException");
553 * mono_get_exception_type_load:
554 * \param class_name the name of the class that could not be loaded
555 * \param assembly_name the assembly where the class was looked up.
556 * \returns a new instance of the \c System.TypeLoadException
559 mono_get_exception_type_load (MonoString
*class_name
, char *assembly_name
)
562 MonoString
*s
= NULL
;
564 s
= mono_string_new_checked (mono_domain_get (), assembly_name
, error
);
565 mono_error_assert_ok (error
);
567 s
= mono_string_empty (mono_domain_get ());
569 MonoException
*ret
= mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
570 "TypeLoadException", class_name
, s
, error
);
571 mono_error_assert_ok (error
);
576 * mono_get_exception_not_implemented:
577 * \param msg the message to pass to the user
578 * \returns a new instance of the \c System.NotImplementedException
581 mono_get_exception_not_implemented (const char *msg
)
583 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg
);
587 * mono_get_exception_not_supported:
588 * \param msg the message to pass to the user
589 * \returns a new instance of the \c System.NotSupportedException
592 mono_get_exception_not_supported (const char *msg
)
594 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg
);
598 * mono_get_exception_missing_method:
599 * \param class_name the class where the lookup was performed.
600 * \param member_name the name of the missing method.
601 * \returns a new instance of the \c System.MissingMethodException
604 mono_get_exception_missing_method (const char *class_name
, const char *member_name
)
607 MonoString
*s1
= mono_string_new_checked (mono_domain_get (), class_name
, error
);
608 mono_error_assert_ok (error
);
609 MonoString
*s2
= mono_string_new_checked (mono_domain_get (), member_name
, error
);
610 mono_error_assert_ok (error
);
612 MonoException
*ret
= mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
613 "MissingMethodException", s1
, s2
, error
);
614 mono_error_assert_ok (error
);
619 * mono_get_exception_missing_field:
620 * \param class_name the class where the lookup was performed
621 * \param member_name the name of the missing method.
622 * \returns a new instance of the \c System.MissingFieldException
625 mono_get_exception_missing_field (const char *class_name
, const char *member_name
)
628 MonoString
*s1
= mono_string_new_checked (mono_domain_get (), class_name
, error
);
629 mono_error_assert_ok (error
);
630 MonoString
*s2
= mono_string_new_checked (mono_domain_get (), member_name
, error
);
631 mono_error_assert_ok (error
);
633 MonoException
*ret
= mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
634 "MissingFieldException", s1
, s2
, error
);
635 mono_error_assert_ok (error
);
641 * mono_get_exception_argument_internal:
642 * \param type the actual type
643 * \param arg the name of the argument that is invalid or null, etc.
644 * \param msg optional message
645 * \returns a new instance of the \c System.ArgumentException or derived
647 static MonoException
*
648 mono_get_exception_argument_internal (const char *type
, const char *arg
, const char *msg
)
650 MonoException
*ex
= mono_exception_from_name_msg (mono_get_corlib (), "System", type
, msg
);
654 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
655 MonoString
*arg_str
= mono_string_new_checked (mono_object_get_domain ((MonoObject
*)ex
), arg
, error
);
656 mono_error_assert_ok (error
);
657 MONO_OBJECT_SETREF (argex
, param_name
, arg_str
);
664 * mono_get_exception_argument_null:
665 * \param arg the name of the argument that is null
666 * \returns a new instance of the \c System.ArgumentNullException
669 mono_get_exception_argument_null (const char *arg
)
671 return mono_get_exception_argument_internal ("ArgumentNullException", arg
, NULL
);
675 * mono_get_exception_argument:
676 * \param arg the name of the invalid argument.
677 * \returns a new instance of the \c System.ArgumentException
680 mono_get_exception_argument (const char *arg
, const char *msg
)
682 return mono_get_exception_argument_internal ("ArgumentException", arg
, msg
);
685 TYPED_HANDLE_DECL (MonoArgumentException
);
688 mono_exception_new_argument (const char *arg
, const char *msg
, MonoError
*error
)
690 MonoExceptionHandle ex
;
691 ex
= mono_exception_new_by_name_msg (mono_get_corlib (), "System", "ArgumentException", msg
, error
);
693 if (arg
&& !MONO_HANDLE_IS_NULL (ex
)) {
694 MonoArgumentExceptionHandle argex
= MONO_HANDLE_CAST (MonoArgumentException
, ex
);
695 MonoStringHandle arg_str
= mono_string_new_handle (MONO_HANDLE_DOMAIN (ex
), arg
, error
);
696 MONO_HANDLE_SET (argex
, param_name
, arg_str
);
703 mono_exception_new_serialization (const char *msg
, MonoError
*error
)
705 return mono_exception_new_by_name_msg (mono_get_corlib (),
706 "System.Runtime.Serialization", "SerializationException",
707 "Could not serialize unhandled exception.", error
);
711 * mono_get_exception_argument_out_of_range:
712 * \param arg the name of the out of range argument.
713 * \returns a new instance of the \c System.ArgumentOutOfRangeException
716 mono_get_exception_argument_out_of_range (const char *arg
)
718 return mono_get_exception_argument_internal ("ArgumentOutOfRangeException", arg
, NULL
);
722 * mono_get_exception_thread_state:
723 * \param msg the message to present to the user
724 * \returns a new instance of the \c System.Threading.ThreadStateException
726 MONO_RT_EXTERNAL_ONLY
728 mono_get_exception_thread_state (const char *msg
)
730 return mono_exception_from_name_msg (
731 mono_get_corlib (), "System.Threading", "ThreadStateException", msg
);
735 * mono_get_exception_io:
736 * \param msg the message to present to the user
737 * \returns a new instance of the \c System.IO.IOException
740 mono_get_exception_io (const char *msg
)
742 return mono_exception_from_name_msg (
743 mono_get_corlib (), "System.IO", "IOException", msg
);
747 * mono_get_exception_file_not_found:
748 * \param fname the name of the file not found.
749 * \returns a new instance of the \c System.IO.FileNotFoundException
752 mono_get_exception_file_not_found (MonoString
*fname
)
755 MonoException
*ret
= mono_exception_from_name_two_strings_checked (
756 mono_get_corlib (), "System.IO", "FileNotFoundException", fname
, fname
, error
);
757 mono_error_assert_ok (error
);
762 * mono_get_exception_file_not_found2:
763 * \param msg an informative message for the user.
764 * \param fname the name of the file not found.
765 * \returns a new instance of the \c System.IO.FileNotFoundException
768 mono_get_exception_file_not_found2 (const char *msg
, MonoString
*fname
)
771 MonoString
*s
= NULL
;
773 s
= mono_string_new_checked (mono_domain_get (), msg
, error
);
774 mono_error_assert_ok (error
);
777 MonoException
*ret
= mono_exception_from_name_two_strings_checked (
778 mono_get_corlib (), "System.IO", "FileNotFoundException", s
, fname
, error
);
779 mono_error_assert_ok (error
);
784 * mono_get_exception_type_initialization:
785 * \param type_name the name of the type that failed to initialize.
786 * \param inner the inner exception.
787 * \returns a new instance of the \c System.TypeInitializationException
790 mono_get_exception_type_initialization (const gchar
*type_name
, MonoException
*inner
)
793 MonoException
*ret
= mono_get_exception_type_initialization_checked (type_name
, inner
, error
);
794 if (!is_ok (error
)) {
795 mono_error_cleanup (error
);
803 mono_get_exception_type_initialization_checked (const gchar
*type_name
, MonoException
*inner
, MonoError
*error
)
813 klass
= mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
815 mono_class_init (klass
);
818 while ((method
= mono_class_get_methods (klass
, &iter
))) {
819 if (!strcmp (".ctor", mono_method_get_name (method
))) {
820 MonoMethodSignature
*sig
= mono_method_signature (method
);
822 if (sig
->param_count
== 2 && sig
->params
[0]->type
== MONO_TYPE_STRING
&& mono_class_from_mono_type (sig
->params
[1]) == mono_defaults
.exception_class
)
829 MonoString
*type_name_str
= mono_string_new_checked (mono_domain_get (), type_name
, error
);
830 mono_error_assert_ok (error
);
831 args
[0] = type_name_str
;
834 exc
= mono_object_new_checked (mono_domain_get (), klass
, error
);
835 mono_error_assert_ok (error
);
837 mono_runtime_invoke_checked (method
, exc
, args
, error
);
838 return_val_if_nok (error
, NULL
);
840 return (MonoException
*) exc
;
844 * mono_get_exception_synchronization_lock:
845 * \param inner the inner exception.
846 * \returns a new instance of the \c System.SynchronizationLockException
849 mono_get_exception_synchronization_lock (const char *msg
)
851 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg
);
855 * mono_get_exception_cannot_unload_appdomain:
856 * \param inner the inner exception.
857 * \returns a new instance of the \c System.CannotUnloadAppDomainException
860 mono_get_exception_cannot_unload_appdomain (const char *msg
)
862 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg
);
866 * mono_get_exception_appdomain_unloaded
867 * \returns a new instance of the \c System.AppDomainUnloadedException
870 mono_get_exception_appdomain_unloaded (void)
872 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
876 * mono_get_exception_bad_image_format:
877 * \param msg an informative message for the user.
878 * \returns a new instance of the \c System.BadImageFormatException
881 mono_get_exception_bad_image_format (const char *msg
)
883 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg
);
887 * mono_get_exception_bad_image_format2:
888 * \param msg an informative message for the user.
889 * \param fname The full name of the file with the invalid image.
890 * \returns a new instance of the \c System.BadImageFormatException
893 mono_get_exception_bad_image_format2 (const char *msg
, MonoString
*fname
)
896 MonoString
*s
= NULL
;
899 s
= mono_string_new_checked (mono_domain_get (), msg
, error
);
900 mono_error_assert_ok (error
);
903 MonoException
*ret
= mono_exception_from_name_two_strings_checked (
904 mono_get_corlib (), "System", "BadImageFormatException", s
, fname
, error
);
905 mono_error_assert_ok (error
);
910 * mono_get_exception_stack_overflow:
911 * \returns a new instance of the \c System.StackOverflowException
914 mono_get_exception_stack_overflow (void)
916 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
920 * mono_get_exception_out_of_memory:
921 * \returns a new instance of the \c System.OutOfMemoryException
924 mono_get_exception_out_of_memory (void)
926 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
930 * mono_get_exception_field_access:
931 * \returns a new instance of the \c System.FieldAccessException
934 mono_get_exception_field_access (void)
936 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
940 * mono_get_exception_field_access2:
941 * \param msg an informative message for the user.
942 * \returns a new instance of the \c System.FieldAccessException
945 mono_get_exception_field_access_msg (const char *msg
)
947 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg
);
951 * mono_get_exception_method_access:
952 * \returns a new instance of the \c System.MethodAccessException
955 mono_get_exception_method_access (void)
957 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
961 * mono_get_exception_method_access2:
962 * \param msg an informative message for the user.
963 * \returns a new instance of the \c System.MethodAccessException
966 mono_get_exception_method_access_msg (const char *msg
)
968 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg
);
972 * mono_get_exception_reflection_type_load:
973 * \param types an array of types that were defined in the moduled loaded.
974 * \param exceptions an array of exceptions that were thrown during the type loading.
975 * \returns a new instance of the \c System.Reflection.ReflectionTypeLoadException
978 mono_get_exception_reflection_type_load (MonoArray
*types_raw
, MonoArray
*exceptions_raw
)
980 HANDLE_FUNCTION_ENTER ();
982 MONO_HANDLE_DCL (MonoArray
, types
);
983 MONO_HANDLE_DCL (MonoArray
, exceptions
);
984 MonoExceptionHandle ret
= mono_get_exception_reflection_type_load_checked (types
, exceptions
, error
);
986 mono_error_cleanup (error
);
987 ret
= MONO_HANDLE_CAST (MonoException
, NULL_HANDLE
);
989 HANDLE_FUNCTION_RETURN_OBJ (ret
);
994 mono_get_exception_reflection_type_load_checked (MonoArrayHandle types
, MonoArrayHandle exceptions
, MonoError
*error
)
1002 klass
= mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
1004 mono_class_init (klass
);
1006 /* Find the Type[], Exception[] ctor */
1008 while ((method
= mono_class_get_methods (klass
, &iter
))) {
1009 if (!strcmp (".ctor", mono_method_get_name (method
))) {
1010 MonoMethodSignature
*sig
= mono_method_signature (method
);
1012 if (sig
->param_count
== 2 && sig
->params
[0]->type
== MONO_TYPE_SZARRAY
&& sig
->params
[1]->type
== MONO_TYPE_SZARRAY
)
1019 MonoExceptionHandle exc
= MONO_HANDLE_CAST (MonoException
, MONO_HANDLE_NEW (MonoObject
, mono_object_new_checked (mono_domain_get (), klass
, error
)));
1020 mono_error_assert_ok (error
);
1023 args
[0] = MONO_HANDLE_RAW (types
);
1024 args
[1] = MONO_HANDLE_RAW (exceptions
);
1026 mono_runtime_invoke_checked (method
, MONO_HANDLE_RAW (exc
), args
, error
);
1027 return_val_if_nok (error
, MONO_HANDLE_CAST (MonoException
, NULL_HANDLE
));
1033 * mono_get_exception_runtime_wrapped:
1036 mono_get_exception_runtime_wrapped (MonoObject
*wrapped_exception
)
1039 MonoException
*ret
= mono_get_exception_runtime_wrapped_checked (wrapped_exception
, error
);
1040 if (!is_ok (error
)) {
1041 mono_error_cleanup (error
);
1049 mono_get_exception_runtime_wrapped_checked (MonoObject
*wrapped_exception
, MonoError
*error
)
1054 MonoDomain
*domain
= mono_domain_get ();
1056 klass
= mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
1058 o
= mono_object_new_checked (domain
, klass
, error
);
1059 mono_error_assert_ok (error
);
1060 g_assert (o
!= NULL
);
1062 method
= mono_class_get_method_from_name (klass
, ".ctor", 1);
1065 mono_runtime_invoke_checked (method
, o
, (gpointer
*)&wrapped_exception
, error
);
1066 return_val_if_nok (error
, NULL
);
1068 return (MonoException
*)o
;
1072 append_frame_and_continue (MonoMethod
*method
, gpointer ip
, size_t native_offset
, gboolean managed
, gpointer user_data
)
1074 MonoDomain
*domain
= mono_domain_get ();
1075 GString
*text
= (GString
*)user_data
;
1078 char *msg
= mono_debug_print_stack_frame (method
, native_offset
, domain
);
1079 g_string_append_printf (text
, "%s\n", msg
);
1082 g_string_append_printf (text
, "<unknown native frame 0x%x>\n", ip
);
1089 mono_exception_get_managed_backtrace (MonoException
*exc
)
1093 text
= g_string_new_len (NULL
, 20);
1095 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc
, append_frame_and_continue
, text
))
1096 g_string_append (text
, "managed backtrace not available\n");
1098 return g_string_free (text
, FALSE
);
1102 mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc
)
1104 #ifdef HAVE_BACKTRACE_SYMBOLS
1106 MonoArrayHandle arr
= MONO_HANDLE_NEW(MonoArray
, NULL
);
1111 MONO_HANDLE_GET (arr
, exc
, native_trace_ips
);
1113 if (MONO_HANDLE_IS_NULL(arr
))
1114 return g_strdup ("");
1115 domain
= mono_domain_get ();
1116 len
= mono_array_handle_length (arr
);
1117 text
= g_string_new_len (NULL
, len
* 20);
1119 void *addr
= MONO_ARRAY_HANDLE_PIN (arr
, gpointer
, 0, &gchandle
);
1121 messages
= backtrace_symbols (addr
, len
);
1123 mono_gchandle_free (gchandle
);
1125 for (i
= 0; i
< len
; ++i
) {
1127 MONO_HANDLE_ARRAY_GETVAL (ip
, arr
, gpointer
, i
);
1128 MonoJitInfo
*ji
= mono_jit_info_table_find (mono_domain_get (), ip
);
1130 char *msg
= mono_debug_print_stack_frame (mono_jit_info_get_method (ji
), (char*)ip
- (char*)ji
->code_start
, domain
);
1131 g_string_append_printf (text
, "%s\n", msg
);
1134 g_string_append_printf (text
, "%s\n", messages
[i
]);
1139 return g_string_free (text
, FALSE
);
1141 return g_strdup ("");
1146 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc
, MonoError
*error
)
1149 MonoStringHandle res
;
1152 if (MONO_HANDLE_IS_NULL (exc
)) {
1153 mono_error_set_argument_null (error
, "exception", "");
1154 return NULL_HANDLE_STRING
;
1157 trace
= mono_exception_handle_get_native_backtrace (exc
);
1158 res
= mono_string_new_handle (mono_domain_get (), trace
, error
);
1164 * mono_error_raise_exception_deprecated:
1165 * \param target_error the exception to raise
1167 * Raises the exception of \p target_error.
1168 * Does nothing if \p target_error has a success error code.
1169 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
1170 * to construct the first exception object.
1171 * The error object \p target_error is cleaned up.
1174 mono_error_raise_exception_deprecated (MonoError
*target_error
)
1176 MonoException
*ex
= mono_error_convert_to_exception (target_error
);
1178 mono_raise_exception_deprecated (ex
);
1182 * mono_error_set_pending_exception:
1183 * \param error The error
1184 * If \p error is set, convert it to an exception and set the pending exception for the current icall.
1185 * \returns TRUE if \p error was set, or FALSE otherwise, so that you can write:
1186 * if (mono_error_set_pending_exception (error)) {
1187 * { ... cleanup code ... }
1192 mono_error_set_pending_exception (MonoError
*error
)
1194 MonoException
*ex
= mono_error_convert_to_exception (error
);
1196 mono_set_pending_exception (ex
);
1204 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func
, void *user_data
)
1206 unhandled_exception_hook
= func
;
1207 unhandled_exception_hook_data
= user_data
;
1211 mono_invoke_unhandled_exception_hook (MonoObject
*exc
)
1213 if (unhandled_exception_hook
) {
1214 unhandled_exception_hook (exc
, unhandled_exception_hook_data
);
1216 ERROR_DECL_VALUE (inner_error
);
1217 MonoObject
*other
= NULL
;
1218 MonoString
*str
= mono_object_try_to_string (exc
, &other
, &inner_error
);
1221 if (str
&& is_ok (&inner_error
)) {
1222 msg
= mono_string_to_utf8_checked (str
, &inner_error
);
1223 if (!is_ok (&inner_error
)) {
1224 msg
= g_strdup_printf ("Nested exception while formatting original exception");
1225 mono_error_cleanup (&inner_error
);
1228 char *original_backtrace
= mono_exception_get_managed_backtrace ((MonoException
*)exc
);
1229 char *nested_backtrace
= mono_exception_get_managed_backtrace ((MonoException
*)other
);
1231 msg
= g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
1232 original_backtrace
, nested_backtrace
);
1234 g_free (original_backtrace
);
1235 g_free (nested_backtrace
);
1237 msg
= g_strdup ("Nested exception trying to figure out what went wrong");
1239 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg
);
1241 #if defined(HOST_IOS)
1242 g_assertion_message ("Terminating runtime due to unhandled exception");
1244 exit (mono_environment_exitcode_get ());
1248 g_assert_not_reached ();
1252 mono_corlib_exception_new_with_args (const char *name_space
, const char *name
, const char *arg_0
, const char *arg_1
, MonoError
*error
)
1254 MonoDomain
*domain
= mono_domain_get ();
1255 MonoString
*str_0
, *str_1
;
1258 str_0
= arg_0
? mono_string_new_checked (domain
, arg_0
, error
) : NULL
;
1259 return_val_if_nok (error
, NULL
);
1261 str_1
= arg_1
? mono_string_new_checked (domain
, arg_1
, error
) : NULL
;
1262 return_val_if_nok (error
, NULL
);
1264 return mono_exception_from_name_two_strings_checked (mono_defaults
.corlib
, name_space
, name
, str_0
, str_1
, error
);
1268 mono_error_set_field_missing (MonoError
*error
, MonoClass
*klass
, const char *field_name
, MonoType
*sig
, const char *reason
, ...)
1273 res
= g_string_new ("Field not found: ");
1277 mono_type_get_desc (res
, sig
, TRUE
);
1278 g_string_append_c (res
, ' ');
1282 if (m_class_get_name_space (klass
)) {
1283 g_string_append (res
, m_class_get_name_space (klass
));
1284 g_string_append_c (res
, '.');
1286 g_string_append (res
, m_class_get_name (klass
));
1289 g_string_append (res
, "<unknown type>");
1292 g_string_append_c (res
, '.');
1295 g_string_append (res
, field_name
);
1297 g_string_append (res
, "<unknown field>");
1299 if (reason
&& *reason
) {
1301 va_start (args
, reason
);
1303 g_string_append (res
, " Due to: ");
1304 g_string_append_vprintf (res
, reason
, args
);
1308 g_string_free (res
, FALSE
);
1310 mono_error_set_specific (error
, MONO_ERROR_MISSING_FIELD
, result
);
1314 * Sets @error to a method missing error.
1317 mono_error_set_method_missing (MonoError
*error
, MonoClass
*klass
, const char *method_name
, MonoMethodSignature
*sig
, const char *reason
, ...)
1323 res
= g_string_new ("Method not found: ");
1326 mono_type_get_desc (res
, sig
->ret
, TRUE
);
1328 g_string_append_c (res
, ' ');
1332 if (m_class_get_name_space (klass
)) {
1333 g_string_append (res
, m_class_get_name_space (klass
));
1334 g_string_append_c (res
, '.');
1336 g_string_append (res
, m_class_get_name (klass
));
1339 g_string_append (res
, "<unknown type>");
1342 g_string_append_c (res
, '.');
1345 g_string_append (res
, method_name
);
1347 g_string_append (res
, "<unknown method>");
1350 if (sig
->generic_param_count
) {
1351 g_string_append_c (res
, '<');
1352 for (i
= 0; i
< sig
->generic_param_count
; ++i
) {
1354 g_string_append (res
, ",");
1355 g_string_append_printf (res
, "!%d", i
);
1357 g_string_append_c (res
, '>');
1360 g_string_append_c (res
, '(');
1361 for (i
= 0; i
< sig
->param_count
; ++i
) {
1363 g_string_append_c (res
, ',');
1364 mono_type_get_desc (res
, sig
->params
[i
], TRUE
);
1366 g_string_append_c (res
, ')');
1369 if (reason
&& *reason
) {
1371 va_start (args
, reason
);
1373 g_string_append (res
, " Due to: ");
1374 g_string_append_vprintf (res
, reason
, args
);
1378 g_string_free (res
, FALSE
);
1380 mono_error_set_specific (error
, MONO_ERROR_MISSING_METHOD
, result
);
1383 #define SET_ERROR_MSG(STR_VAR, FMT_STR) do { \
1385 va_start (__args, FMT_STR); \
1386 STR_VAR = g_strdup_vprintf (FMT_STR, __args); \
1391 * \p image_name argument will be g_strdup'd. Called must free passed value
1394 mono_error_set_bad_image_by_name (MonoError
*error
, const char *image_name
, const char *msg_format
, ...)
1397 SET_ERROR_MSG (str
, msg_format
);
1399 mono_error_set_specific (error
, MONO_ERROR_BAD_IMAGE
, str
);
1401 mono_error_set_first_argument (error
, image_name
);
1405 mono_error_set_bad_image (MonoError
*error
, MonoImage
*image
, const char *msg_format
, ...)
1408 SET_ERROR_MSG (str
, msg_format
);
1410 mono_error_set_specific (error
, MONO_ERROR_BAD_IMAGE
, str
);
1412 mono_error_set_first_argument (error
, mono_image_get_name (image
));
1416 mono_error_set_file_not_found (MonoError
*error
, const char *file_name
, const char *msg_format
, ...)
1419 SET_ERROR_MSG (str
, msg_format
);
1421 mono_error_set_specific (error
, MONO_ERROR_FILE_NOT_FOUND
, str
);
1423 mono_error_set_first_argument (error
, file_name
);
1427 mono_error_set_simple_file_not_found (MonoError
*error
, const char *file_name
, gboolean refection_only
)
1430 mono_error_set_file_not_found (error
, file_name
, "Cannot resolve dependency to assembly because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.");
1432 mono_error_set_file_not_found (error
, file_name
, "Could not load file or assembly '%s' or one of its dependencies.", file_name
);
1436 mono_error_set_argument_out_of_range (MonoError
*error
, const char *name
)
1439 mono_error_set_exception_instance (error
, mono_get_exception_argument_out_of_range (name
));
1443 mono_error_convert_to_exception_handle (MonoError
*error
)
1445 //FIXMEcoop mono_error_convert_to_exception is raw pointer
1446 return MONO_HANDLE_NEW (MonoException
, mono_error_convert_to_exception (error
));