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>
21 * mono_exception_from_name:
22 * @image: the Mono image where to look for the class
23 * @name_space: the namespace for the class
26 * Creates an exception of the given namespace/name class in the
29 * Returns: the initialized exception instance.
32 mono_exception_from_name (MonoImage
*image
, const char *name_space
,
35 return mono_exception_from_name_domain (mono_domain_get (), image
, name_space
, name
);
39 * mono_exception_from_name_domain:
40 * @domain: Domain where the return object will be created.
41 * @image: the Mono image where to look for the class
42 * @name_space: the namespace for the class
45 * Creates an exception object of the given namespace/name class on
48 * Returns: the initialized exception instance.
51 mono_exception_from_name_domain (MonoDomain
*domain
, MonoImage
*image
,
52 const char* name_space
, const char *name
)
56 MonoDomain
*caller_domain
= mono_domain_get ();
58 klass
= mono_class_from_name (image
, name_space
, name
);
60 o
= mono_object_new (domain
, klass
);
63 if (domain
!= caller_domain
)
64 mono_domain_set_internal (domain
);
65 mono_runtime_object_init (o
);
66 if (domain
!= caller_domain
)
67 mono_domain_set_internal (caller_domain
);
69 return (MonoException
*)o
;
74 * mono_exception_from_token:
75 * @image: the Mono image where to look for the class
76 * @token: The type token of the class
78 * Creates an exception of the type given by @token.
80 * Returns: the initialized exception instance.
83 mono_exception_from_token (MonoImage
*image
, guint32 token
)
88 klass
= mono_class_get (image
, token
);
90 o
= mono_object_new (mono_domain_get (), klass
);
93 mono_runtime_object_init (o
);
95 return (MonoException
*)o
;
98 static MonoException
*
99 create_exception_two_strings (MonoClass
*klass
, MonoString
*a1
, MonoString
*a2
)
101 MonoDomain
*domain
= mono_domain_get ();
102 MonoMethod
*method
= NULL
;
112 o
= mono_object_new (domain
, klass
);
115 while ((m
= mono_class_get_methods (klass
, &iter
))) {
116 MonoMethodSignature
*sig
;
118 if (strcmp (".ctor", mono_method_get_name (m
)))
120 sig
= mono_method_signature (m
);
121 if (sig
->param_count
!= count
)
124 if (sig
->params
[0]->type
!= MONO_TYPE_STRING
)
126 if (count
== 2 && sig
->params
[1]->type
!= MONO_TYPE_STRING
)
134 mono_runtime_invoke (method
, o
, args
, NULL
);
135 return (MonoException
*) o
;
139 * mono_exception_from_name_two_strings:
140 * @image: the Mono image where to look for the class
141 * @name_space: the namespace for the class
143 * @a1: first string argument to pass
144 * @a2: second string argument to pass
146 * Creates an exception from a constructor that takes two string
149 * Returns: the initialized exception instance.
152 mono_exception_from_name_two_strings (MonoImage
*image
, const char *name_space
,
153 const char *name
, MonoString
*a1
, MonoString
*a2
)
155 MonoClass
*klass
= mono_class_from_name (image
, name_space
, name
);
157 return create_exception_two_strings (klass
, a1
, a2
);
161 * mono_exception_from_name_msg:
162 * @image: the Mono image where to look for the class
163 * @name_space: the namespace for the class
165 * @msg: the message to embed inside the exception
167 * Creates an exception and initializes its message field.
169 * Returns: the initialized exception instance.
172 mono_exception_from_name_msg (MonoImage
*image
, const char *name_space
,
173 const char *name
, const char *msg
)
177 ex
= mono_exception_from_name (image
, name_space
, name
);
180 MONO_OBJECT_SETREF (ex
, message
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), msg
));
186 * mono_exception_from_token_two_strings:
188 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
192 mono_exception_from_token_two_strings (MonoImage
*image
, guint32 token
,
193 MonoString
*a1
, MonoString
*a2
)
195 MonoClass
*klass
= mono_class_get (image
, token
);
197 return create_exception_two_strings (klass
, a1
, a2
);
201 * mono_get_exception_divide_by_zero:
203 * Returns: a new instance of the System.DivideByZeroException
206 mono_get_exception_divide_by_zero ()
208 return mono_exception_from_name (mono_get_corlib (), "System",
209 "DivideByZeroException");
213 * mono_get_exception_security:
215 * Returns: a new instance of the System.Security.SecurityException
218 mono_get_exception_security ()
220 return mono_exception_from_name (mono_get_corlib (), "System.Security",
221 "SecurityException");
225 * mono_get_exception_thread_abort:
227 * Returns: a new instance of the System.Threading.ThreadAbortException.
230 mono_get_exception_thread_abort ()
232 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
233 "ThreadAbortException");
237 * mono_get_exception_thread_interrupted:
239 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
242 mono_get_exception_thread_interrupted ()
244 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
245 "ThreadInterruptedException");
249 * mono_get_exception_arithmetic:
251 * Returns: a new instance of the System.ArithmeticException.
254 mono_get_exception_arithmetic ()
256 return mono_exception_from_name (mono_get_corlib (), "System",
257 "ArithmeticException");
261 * mono_get_exception_overflow:
263 * Returns: a new instance of the System.OverflowException
266 mono_get_exception_overflow ()
268 return mono_exception_from_name (mono_get_corlib (), "System",
269 "OverflowException");
273 * mono_get_exception_null_reference:
275 * Returns: a new instance of the System.NullReferenceException
278 mono_get_exception_null_reference ()
280 return mono_exception_from_name (mono_get_corlib (), "System",
281 "NullReferenceException");
285 * mono_get_exception_execution_engine:
286 * @msg: the message to pass to the user
288 * Returns: a new instance of the System.ExecutionEngineException
291 mono_get_exception_execution_engine (const char *msg
)
293 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg
);
297 * mono_get_exception_serialization:
298 * @msg: the message to pass to the user
300 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
303 mono_get_exception_serialization (const char *msg
)
305 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg
);
309 * mono_get_exception_invalid_cast:
311 * Returns: a new instance of the System.InvalidCastException
314 mono_get_exception_invalid_cast ()
316 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
320 * mono_get_exception_invalid_operation:
321 * @msg: the message to pass to the user
323 * Returns: a new instance of the System.InvalidOperationException
326 mono_get_exception_invalid_operation (const char *msg
)
328 return mono_exception_from_name_msg (mono_get_corlib (), "System",
329 "InvalidOperationException", msg
);
333 * mono_get_exception_index_out_of_range:
335 * Returns: a new instance of the System.IndexOutOfRangeException
338 mono_get_exception_index_out_of_range ()
340 return mono_exception_from_name (mono_get_corlib (), "System",
341 "IndexOutOfRangeException");
345 * mono_get_exception_array_type_mismatch:
347 * Returns: a new instance of the System.ArrayTypeMismatchException
350 mono_get_exception_array_type_mismatch ()
352 return mono_exception_from_name (mono_get_corlib (), "System",
353 "ArrayTypeMismatchException");
357 * mono_get_exception_type_load:
358 * @class_name: the name of the class that could not be loaded
359 * @assembly_name: the assembly where the class was looked up.
361 * Returns: a new instance of the System.TypeLoadException.
364 mono_get_exception_type_load (MonoString
*class_name
, char *assembly_name
)
366 MonoString
*s
= assembly_name
? mono_string_new (mono_domain_get (), assembly_name
) : mono_string_new (mono_domain_get (), "");
368 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
369 "TypeLoadException", class_name
, s
);
373 * mono_get_exception_not_implemented:
374 * @msg: the message to pass to the user
376 * Returns: a new instance of the System.NotImplementedException
379 mono_get_exception_not_implemented (const char *msg
)
381 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg
);
385 * mono_get_exception_not_supported:
386 * @msg: the message to pass to the user
388 * Returns: a new instance of the System.NotSupportedException
391 mono_get_exception_not_supported (const char *msg
)
393 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg
);
397 * mono_get_exception_missing_method:
398 * @class_name: the class where the lookup was performed.
399 * @member_name: the name of the missing method.
401 * Returns: a new instance of the System.MissingMethodException
404 mono_get_exception_missing_method (const char *class_name
, const char *member_name
)
406 MonoString
*s1
= mono_string_new (mono_domain_get (), class_name
);
407 MonoString
*s2
= mono_string_new (mono_domain_get (), member_name
);
409 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
410 "MissingMethodException", s1
, s2
);
414 * mono_get_exception_missing_field:
415 * @class_name: the class where the lookup was performed
416 * @member_name: the name of the missing method.
418 * Returns: a new instance of the System.MissingFieldException
421 mono_get_exception_missing_field (const char *class_name
, const char *member_name
)
423 MonoString
*s1
= mono_string_new (mono_domain_get (), class_name
);
424 MonoString
*s2
= mono_string_new (mono_domain_get (), member_name
);
426 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
427 "MissingFieldException", s1
, s2
);
431 * mono_get_exception_argument_null:
432 * @arg: the name of the argument that is null
434 * Returns: a new instance of the System.ArgumentNullException
437 mono_get_exception_argument_null (const char *arg
)
441 ex
= mono_exception_from_name (
442 mono_get_corlib (), "System", "ArgumentNullException");
445 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
446 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
453 * mono_get_exception_argument:
454 * @arg: the name of the invalid argument.
456 * Returns: a new instance of the System.ArgumentException
459 mono_get_exception_argument (const char *arg
, const char *msg
)
463 ex
= mono_exception_from_name_msg (
464 mono_get_corlib (), "System", "ArgumentException", msg
);
467 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
468 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
475 * mono_get_exception_argument_out_of_range:
476 * @arg: the name of the out of range argument.
478 * Returns: a new instance of the System.ArgumentOutOfRangeException
481 mono_get_exception_argument_out_of_range (const char *arg
)
485 ex
= mono_exception_from_name (
486 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
489 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
490 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
497 * mono_get_exception_thread_state:
498 * @msg: the message to present to the user
500 * Returns: a new instance of the System.Threading.ThreadStateException
503 mono_get_exception_thread_state (const char *msg
)
505 return mono_exception_from_name_msg (
506 mono_get_corlib (), "System.Threading", "ThreadStateException", msg
);
510 * mono_get_exception_io:
511 * @msg: the message to present to the user
513 * Returns: a new instance of the System.IO.IOException
516 mono_get_exception_io (const char *msg
)
518 return mono_exception_from_name_msg (
519 mono_get_corlib (), "System.IO", "IOException", msg
);
523 * mono_get_exception_file_not_found:
524 * @fname: the name of the file not found.
526 * Returns: a new instance of the System.IO.FileNotFoundException
529 mono_get_exception_file_not_found (MonoString
*fname
)
531 return mono_exception_from_name_two_strings (
532 mono_get_corlib (), "System.IO", "FileNotFoundException", fname
, fname
);
536 * mono_get_exception_file_not_found2:
537 * @msg: an informative message for the user.
538 * @fname: the name of the file not found.
540 * Returns: a new instance of the System.IO.FileNotFoundException
543 mono_get_exception_file_not_found2 (const char *msg
, MonoString
*fname
)
545 MonoString
*s
= msg
? mono_string_new (mono_domain_get (), msg
) : NULL
;
547 return mono_exception_from_name_two_strings (
548 mono_get_corlib (), "System.IO", "FileNotFoundException", s
, fname
);
552 * mono_get_exception_type_initialization:
553 * @type_name: the name of the type that failed to initialize.
554 * @inner: the inner exception.
556 * Returns: a new instance of the System.TypeInitializationException
559 mono_get_exception_type_initialization (const gchar
*type_name
, MonoException
*inner
)
567 klass
= mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
570 mono_class_init (klass
);
572 /* TypeInitializationException only has 1 ctor with 2 args */
574 while ((method
= mono_class_get_methods (klass
, &iter
))) {
575 if (!strcmp (".ctor", mono_method_get_name (method
)) && mono_method_signature (method
)->param_count
== 2)
582 args
[0] = mono_string_new (mono_domain_get (), type_name
);
585 exc
= mono_object_new (mono_domain_get (), klass
);
586 mono_runtime_invoke (method
, exc
, args
, NULL
);
588 return (MonoException
*) exc
;
592 * mono_get_exception_synchronization_lock:
593 * @inner: the inner exception.
595 * Returns: a new instance of the System.SynchronizationLockException
598 mono_get_exception_synchronization_lock (const char *msg
)
600 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg
);
604 * mono_get_exception_cannot_unload_appdomain:
605 * @inner: the inner exception.
607 * Returns: a new instance of the System.CannotUnloadAppDomainException
610 mono_get_exception_cannot_unload_appdomain (const char *msg
)
612 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg
);
616 * mono_get_exception_appdomain_unloaded
618 * Returns: a new instance of the System.AppDomainUnloadedException
621 mono_get_exception_appdomain_unloaded (void)
623 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
627 * mono_get_exception_bad_image_format:
628 * @msg: an informative message for the user.
630 * Returns: a new instance of the System.BadImageFormatException
633 mono_get_exception_bad_image_format (const char *msg
)
635 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg
);
639 * mono_get_exception_bad_image_format2:
640 * @msg: an informative message for the user.
641 * @fname: The full name of the file with the invalid image.
643 * Returns: a new instance of the System.BadImageFormatException
646 mono_get_exception_bad_image_format2 (const char *msg
, MonoString
*fname
)
648 MonoString
*s
= msg
? mono_string_new (mono_domain_get (), msg
) : NULL
;
650 return mono_exception_from_name_two_strings (
651 mono_get_corlib (), "System", "BadImageFormatException", s
, fname
);
655 * mono_get_exception_stack_overflow:
657 * Returns: a new instance of the System.StackOverflowException
660 mono_get_exception_stack_overflow (void)
662 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
666 * mono_get_exception_out_of_memory:
668 * Returns: a new instance of the System.OutOfMemoryException
671 mono_get_exception_out_of_memory (void)
673 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
677 * mono_get_exception_field_access:
679 * Returns: a new instance of the System.FieldAccessException
682 mono_get_exception_field_access (void)
684 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
688 * mono_get_exception_field_access2:
689 * @msg: an informative message for the user.
691 * Returns: a new instance of the System.FieldAccessException
694 mono_get_exception_field_access_msg (const char *msg
)
696 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg
);
700 * mono_get_exception_method_access:
702 * Returns: a new instance of the System.MethodAccessException
705 mono_get_exception_method_access (void)
707 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
711 * mono_get_exception_method_access2:
712 * @msg: an informative message for the user.
714 * Returns: a new instance of the System.MethodAccessException
717 mono_get_exception_method_access_msg (const char *msg
)
719 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg
);
723 * mono_get_exception_reflection_type_load:
724 * @types: an array of types that were defined in the moduled loaded.
725 * @exceptions: an array of exceptions that were thrown during the type loading.
727 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
730 mono_get_exception_reflection_type_load (MonoArray
*types
, MonoArray
*exceptions
)
737 klass
= mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
739 mono_class_init (klass
);
741 method
= mono_class_get_method_from_name (klass
, ".ctor", 2);
745 args
[1] = exceptions
;
747 exc
= mono_object_new (mono_domain_get (), klass
);
748 mono_runtime_invoke (method
, exc
, args
, NULL
);
750 return (MonoException
*) exc
;