Make coop handles typesafe. (#9183)
[mono-project.git] / mono / metadata / exception.c
blobc7ef4868dccc8c194c188bd240c0c7ffbba80457
1 /**
2 * \file
3 * Exception handling
5 * Authors:
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.
16 #include <glib.h>
17 #include <config.h>
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>
28 #include <string.h>
30 #ifdef HAVE_EXECINFO_H
31 #include <execinfo.h>
32 #endif
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);
41 /**
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
48 * current domain.
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);
58 /**
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
65 * current domain.
67 * \returns the initialized exception instance.
69 MonoException *
70 mono_exception_from_name (MonoImage *image, const char *name_space,
71 const char *name)
73 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
76 /**
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
84 * the given domain.
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);
112 return_null:
113 MONO_HANDLE_ASSIGN (o, NULL_HANDLE);
114 exit:
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
126 * the given domain.
128 * \returns the initialized exception instance.
130 MonoException *
131 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
132 const char* name_space, const char *name)
134 ERROR_DECL (error);
135 MonoClass *klass;
136 MonoObject *o;
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.
165 MonoException *
166 mono_exception_from_token (MonoImage *image, guint32 token)
168 ERROR_DECL (error);
169 MonoClass *klass;
170 MonoObject *o;
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;
189 MonoObject *o;
190 int count = 1;
191 gpointer args [2];
192 gpointer iter;
193 MonoMethod *m;
195 if (a2 != NULL)
196 count++;
198 o = mono_object_new_checked (domain, klass, error);
199 mono_error_assert_ok (error);
201 iter = NULL;
202 while ((m = mono_class_get_methods (klass, &iter))) {
203 MonoMethodSignature *sig;
205 if (strcmp (".ctor", mono_method_get_name (m)))
206 continue;
207 sig = mono_method_signature (m);
208 if (sig->param_count != count)
209 continue;
211 if (sig->params [0]->type != MONO_TYPE_STRING)
212 continue;
213 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
214 continue;
215 method = m;
216 break;
219 args [0] = a1;
220 args [1] = a2;
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
237 * arguments.
239 * \returns the initialized exception instance.
241 MonoException *
242 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
243 const char *name, MonoString *a1, MonoString *a2)
245 ERROR_DECL (error);
246 MonoException *ret;
248 ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, error);
249 mono_error_cleanup (error);
250 return ret;
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
263 * arguments.
265 * \returns the initialized exception instance. On failure returns
266 * NULL and sets \p error.
268 MonoException *
269 mono_exception_from_name_two_strings_checked (MonoImage *image, const char *name_space,
270 const char *name, MonoString *a1, MonoString *a2,
271 MonoError *error)
273 MonoClass *klass;
275 error_init (error);
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);
301 if (msg) {
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);
308 goto exit;
309 return_null:
310 MONO_HANDLE_ASSIGN (ex, NULL_HANDLE);
311 exit:
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.
326 MonoException *
327 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
328 const char *name, const char *msg)
330 ERROR_DECL (error);
331 MonoException *ex;
333 ex = mono_exception_from_name (image, name_space, name);
335 if (msg) {
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);
341 return ex;
345 * mono_exception_from_token_two_strings:
347 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
348 * IMAGE and TOKEN.
350 MonoException *
351 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
352 MonoString *a1, MonoString *a2)
354 ERROR_DECL (error);
355 MonoException *ret;
356 ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, error);
357 mono_error_cleanup (error);
358 return ret;
362 * mono_exception_from_token_two_strings_checked:
364 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
365 * IMAGE and TOKEN.
367 MonoException *
368 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
369 MonoString *a1, MonoString *a2,
370 MonoError *error)
372 MonoClass *klass;
374 error_init (error);
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
386 MonoException *
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
397 MonoException *
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
408 MonoExceptionHandle
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
418 MonoException *
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
429 MonoExceptionHandle
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
439 MonoException *
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
450 MonoException *
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
461 MonoException *
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
472 MonoException *
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
484 MonoException *
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
495 MonoException *
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
505 MonoException *
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
516 MonoException *
517 mono_get_exception_invalid_operation (const char *msg)
519 return mono_exception_from_name_msg (mono_get_corlib (), "System",
520 "InvalidOperationException", msg);
523 MonoExceptionHandle
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
534 MonoException *
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
545 MonoException *
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
558 MonoException *
559 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
561 ERROR_DECL (error);
562 MonoString *s = NULL;
563 if (assembly_name) {
564 s = mono_string_new_checked (mono_domain_get (), assembly_name, error);
565 mono_error_assert_ok (error);
566 } else
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);
572 return ret;
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
580 MonoException *
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
591 MonoException *
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
603 MonoException *
604 mono_get_exception_missing_method (const char *class_name, const char *member_name)
606 ERROR_DECL (error);
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);
615 return ret;
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
624 MonoException *
625 mono_get_exception_missing_field (const char *class_name, const char *member_name)
627 ERROR_DECL (error);
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);
636 return ret;
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);
652 if (arg) {
653 ERROR_DECL (error);
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);
660 return ex;
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
668 MonoException*
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
679 MonoException *
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);
687 MonoExceptionHandle
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);
699 return ex;
702 MonoExceptionHandle
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
715 MonoException *
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
727 MonoException *
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
739 MonoException *
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
751 MonoException *
752 mono_get_exception_file_not_found (MonoString *fname)
754 ERROR_DECL (error);
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);
758 return ret;
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
767 MonoException *
768 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
770 ERROR_DECL (error);
771 MonoString *s = NULL;
772 if (msg) {
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);
780 return ret;
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
789 MonoException *
790 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
792 ERROR_DECL (error);
793 MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, error);
794 if (!is_ok (error)) {
795 mono_error_cleanup (error);
796 return NULL;
799 return ret;
802 MonoException *
803 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
805 MonoClass *klass;
806 gpointer args [2];
807 MonoObject *exc;
808 MonoMethod *method;
809 gpointer iter;
811 error_init (error);
813 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
815 mono_class_init (klass);
817 iter = NULL;
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)
823 break;
825 method = NULL;
827 g_assert (method);
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;
832 args [1] = inner;
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
848 MonoException *
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
859 MonoException *
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
869 MonoException *
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
880 MonoException *
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
892 MonoException *
893 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
895 ERROR_DECL (error);
896 MonoString *s = NULL;
898 if (msg) {
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);
906 return ret;
910 * mono_get_exception_stack_overflow:
911 * \returns a new instance of the \c System.StackOverflowException
913 MonoException *
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
923 MonoException *
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
933 MonoException *
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
944 MonoException *
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
954 MonoException *
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
965 MonoException *
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
977 MonoException *
978 mono_get_exception_reflection_type_load (MonoArray *types_raw, MonoArray *exceptions_raw)
980 HANDLE_FUNCTION_ENTER ();
981 ERROR_DECL (error);
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);
985 if (is_ok (error)) {
986 mono_error_cleanup (error);
987 ret = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
989 HANDLE_FUNCTION_RETURN_OBJ (ret);
993 MonoExceptionHandle
994 mono_get_exception_reflection_type_load_checked (MonoArrayHandle types, MonoArrayHandle exceptions, MonoError *error)
996 MonoClass *klass;
997 MonoMethod *method;
998 gpointer iter;
1000 error_init (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 */
1007 iter = NULL;
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)
1013 break;
1015 method = NULL;
1017 g_assert (method);
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);
1022 gpointer args [2];
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));
1029 return exc;
1033 * mono_get_exception_runtime_wrapped:
1035 MonoException *
1036 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
1038 ERROR_DECL (error);
1039 MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, error);
1040 if (!is_ok (error)) {
1041 mono_error_cleanup (error);
1042 return NULL;
1045 return ret;
1048 MonoException *
1049 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
1051 MonoClass *klass;
1052 MonoObject *o;
1053 MonoMethod *method;
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);
1063 g_assert (method);
1065 mono_runtime_invoke_checked (method, o, (gpointer*)&wrapped_exception, error);
1066 return_val_if_nok (error, NULL);
1068 return (MonoException *)o;
1071 static gboolean
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;
1077 if (method) {
1078 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
1079 g_string_append_printf (text, "%s\n", msg);
1080 g_free (msg);
1081 } else {
1082 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
1085 return FALSE;
1088 char *
1089 mono_exception_get_managed_backtrace (MonoException *exc)
1091 GString *text;
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);
1101 char *
1102 mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc)
1104 #ifdef HAVE_BACKTRACE_SYMBOLS
1105 MonoDomain *domain;
1106 MonoArrayHandle arr = MONO_HANDLE_NEW(MonoArray, NULL);
1107 int i, len;
1108 GString *text;
1109 char **messages;
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);
1118 uint32_t gchandle;
1119 void *addr = MONO_ARRAY_HANDLE_PIN (arr, gpointer, 0, &gchandle);
1120 MONO_ENTER_GC_SAFE;
1121 messages = backtrace_symbols (addr, len);
1122 MONO_EXIT_GC_SAFE;
1123 mono_gchandle_free (gchandle);
1125 for (i = 0; i < len; ++i) {
1126 gpointer ip;
1127 MONO_HANDLE_ARRAY_GETVAL (ip, arr, gpointer, i);
1128 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
1129 if (ji) {
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);
1132 g_free (msg);
1133 } else {
1134 g_string_append_printf (text, "%s\n", messages [i]);
1138 g_free (messages);
1139 return g_string_free (text, FALSE);
1140 #else
1141 return g_strdup ("");
1142 #endif
1145 MonoStringHandle
1146 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc, MonoError *error)
1148 char *trace;
1149 MonoStringHandle res;
1150 error_init (error);
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);
1159 g_free (trace);
1160 return res;
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.
1173 void
1174 mono_error_raise_exception_deprecated (MonoError *target_error)
1176 MonoException *ex = mono_error_convert_to_exception (target_error);
1177 if (ex)
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 ... }
1188 * return;
1191 gboolean
1192 mono_error_set_pending_exception (MonoError *error)
1194 MonoException *ex = mono_error_convert_to_exception (error);
1195 if (ex) {
1196 mono_set_pending_exception (ex);
1197 return TRUE;
1198 } else {
1199 return FALSE;
1203 void
1204 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, void *user_data)
1206 unhandled_exception_hook = func;
1207 unhandled_exception_hook_data = user_data;
1210 void
1211 mono_invoke_unhandled_exception_hook (MonoObject *exc)
1213 if (unhandled_exception_hook) {
1214 unhandled_exception_hook (exc, unhandled_exception_hook_data);
1215 } else {
1216 ERROR_DECL_VALUE (inner_error);
1217 MonoObject *other = NULL;
1218 MonoString *str = mono_object_try_to_string (exc, &other, &inner_error);
1219 char *msg = NULL;
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);
1227 } else if (other) {
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);
1236 } else {
1237 msg = g_strdup ("Nested exception trying to figure out what went wrong");
1239 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
1240 g_free (msg);
1241 #if defined(HOST_IOS)
1242 g_assertion_message ("Terminating runtime due to unhandled exception");
1243 #else
1244 exit (mono_environment_exitcode_get ());
1245 #endif
1248 g_assert_not_reached ();
1251 MonoException *
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;
1256 error_init (error);
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);
1267 void
1268 mono_error_set_field_missing (MonoError *error, MonoClass *klass, const char *field_name, MonoType *sig, const char *reason, ...)
1270 char *result;
1271 GString *res;
1273 res = g_string_new ("Field not found: ");
1276 if (sig) {
1277 mono_type_get_desc (res, sig, TRUE);
1278 g_string_append_c (res, ' ');
1281 if (klass) {
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));
1288 else {
1289 g_string_append (res, "<unknown type>");
1292 g_string_append_c (res, '.');
1294 if (field_name)
1295 g_string_append (res, field_name);
1296 else
1297 g_string_append (res, "<unknown field>");
1299 if (reason && *reason) {
1300 va_list args;
1301 va_start (args, reason);
1303 g_string_append (res, " Due to: ");
1304 g_string_append_vprintf (res, reason, args);
1305 va_end (args);
1307 result = res->str;
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.
1316 void
1317 mono_error_set_method_missing (MonoError *error, MonoClass *klass, const char *method_name, MonoMethodSignature *sig, const char *reason, ...)
1319 int i;
1320 char *result;
1321 GString *res;
1323 res = g_string_new ("Method not found: ");
1325 if (sig) {
1326 mono_type_get_desc (res, sig->ret, TRUE);
1328 g_string_append_c (res, ' ');
1331 if (klass) {
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));
1338 else {
1339 g_string_append (res, "<unknown type>");
1342 g_string_append_c (res, '.');
1344 if (method_name)
1345 g_string_append (res, method_name);
1346 else
1347 g_string_append (res, "<unknown method>");
1349 if (sig) {
1350 if (sig->generic_param_count) {
1351 g_string_append_c (res, '<');
1352 for (i = 0; i < sig->generic_param_count; ++i) {
1353 if (i > 0)
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) {
1362 if (i > 0)
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) {
1370 va_list args;
1371 va_start (args, reason);
1373 g_string_append (res, " Due to: ");
1374 g_string_append_vprintf (res, reason, args);
1375 va_end (args);
1377 result = res->str;
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 { \
1384 va_list __args; \
1385 va_start (__args, FMT_STR); \
1386 STR_VAR = g_strdup_vprintf (FMT_STR, __args); \
1387 va_end(__args); \
1388 } while (0);
1391 * \p image_name argument will be g_strdup'd. Called must free passed value
1393 void
1394 mono_error_set_bad_image_by_name (MonoError *error, const char *image_name, const char *msg_format, ...)
1396 char *str;
1397 SET_ERROR_MSG (str, msg_format);
1399 mono_error_set_specific (error, MONO_ERROR_BAD_IMAGE, str);
1400 if (image_name)
1401 mono_error_set_first_argument (error, image_name);
1404 void
1405 mono_error_set_bad_image (MonoError *error, MonoImage *image, const char *msg_format, ...)
1407 char *str;
1408 SET_ERROR_MSG (str, msg_format);
1410 mono_error_set_specific (error, MONO_ERROR_BAD_IMAGE, str);
1411 if (image)
1412 mono_error_set_first_argument (error, mono_image_get_name (image));
1415 void
1416 mono_error_set_file_not_found (MonoError *error, const char *file_name, const char *msg_format, ...)
1418 char *str;
1419 SET_ERROR_MSG (str, msg_format);
1421 mono_error_set_specific (error, MONO_ERROR_FILE_NOT_FOUND, str);
1422 if (file_name)
1423 mono_error_set_first_argument (error, file_name);
1426 void
1427 mono_error_set_simple_file_not_found (MonoError *error, const char *file_name, gboolean refection_only)
1429 if (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.");
1431 else
1432 mono_error_set_file_not_found (error, file_name, "Could not load file or assembly '%s' or one of its dependencies.", file_name);
1435 void
1436 mono_error_set_argument_out_of_range (MonoError *error, const char *name)
1438 //FIXMEcoop
1439 mono_error_set_exception_instance (error, mono_get_exception_argument_out_of_range (name));
1442 MonoExceptionHandle
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));