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/appdomain.h>
20 * mono_exception_from_name:
21 * @image: the Mono image where to look for the class
22 * @name_space: the namespace for the class
25 * Creates an exception of the given namespace/name class in the
28 * Returns: the initialized exception instance.
31 mono_exception_from_name (MonoImage
*image
, const char *name_space
,
34 return mono_exception_from_name_domain (mono_domain_get (), image
, name_space
, name
);
38 * mono_exception_from_name_domain:
39 * @domain: Domain where the return object will be created.
40 * @image: the Mono image where to look for the class
41 * @name_space: the namespace for the class
44 * Creates an exception object of the given namespace/name class on
47 * Returns: the initialized exception instance.
50 mono_exception_from_name_domain (MonoDomain
*domain
, MonoImage
*image
,
51 const char* name_space
, const char *name
)
56 klass
= mono_class_from_name (image
, name_space
, name
);
58 o
= mono_object_new (domain
, klass
);
61 mono_runtime_object_init (o
);
63 return (MonoException
*)o
;
68 * mono_exception_from_token:
69 * @image: the Mono image where to look for the class
70 * @token: The type token of the class
72 * Creates an exception of the type given by @token.
74 * Returns: the initialized exception instance.
77 mono_exception_from_token (MonoImage
*image
, guint32 token
)
82 klass
= mono_class_get (image
, token
);
84 o
= mono_object_new (mono_domain_get (), klass
);
87 mono_runtime_object_init (o
);
89 return (MonoException
*)o
;
92 static MonoException
*
93 create_exception_two_strings (MonoClass
*klass
, MonoString
*a1
, MonoString
*a2
)
95 MonoDomain
*domain
= mono_domain_get ();
96 MonoMethod
*method
= NULL
;
106 o
= mono_object_new (domain
, klass
);
109 while ((m
= mono_class_get_methods (klass
, &iter
))) {
110 MonoMethodSignature
*sig
;
112 if (strcmp (".ctor", mono_method_get_name (m
)))
114 sig
= mono_method_signature (m
);
115 if (sig
->param_count
!= count
)
118 if (sig
->params
[0]->type
!= MONO_TYPE_STRING
)
120 if (count
== 2 && sig
->params
[1]->type
!= MONO_TYPE_STRING
)
128 mono_runtime_invoke (method
, o
, args
, NULL
);
129 return (MonoException
*) o
;
133 * mono_exception_from_name_two_strings:
134 * @image: the Mono image where to look for the class
135 * @name_space: the namespace for the class
137 * @a1: first string argument to pass
138 * @a2: second string argument to pass
140 * Creates an exception from a constructor that takes two string
143 * Returns: the initialized exception instance.
146 mono_exception_from_name_two_strings (MonoImage
*image
, const char *name_space
,
147 const char *name
, MonoString
*a1
, MonoString
*a2
)
149 MonoClass
*klass
= mono_class_from_name (image
, name_space
, name
);
151 return create_exception_two_strings (klass
, a1
, a2
);
155 * mono_exception_from_name_msg:
156 * @image: the Mono image where to look for the class
157 * @name_space: the namespace for the class
159 * @msg: the message to embed inside the exception
161 * Creates an exception and initializes its message field.
163 * Returns: the initialized exception instance.
166 mono_exception_from_name_msg (MonoImage
*image
, const char *name_space
,
167 const char *name
, const char *msg
)
171 ex
= mono_exception_from_name (image
, name_space
, name
);
174 MONO_OBJECT_SETREF (ex
, message
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), msg
));
180 * mono_exception_from_token_two_strings:
182 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
186 mono_exception_from_token_two_strings (MonoImage
*image
, guint32 token
,
187 MonoString
*a1
, MonoString
*a2
)
189 MonoClass
*klass
= mono_class_get (image
, token
);
191 return create_exception_two_strings (klass
, a1
, a2
);
195 * mono_get_exception_divide_by_zero:
197 * Returns: a new instance of the System.DivideByZeroException
200 mono_get_exception_divide_by_zero ()
202 return mono_exception_from_name (mono_get_corlib (), "System",
203 "DivideByZeroException");
207 * mono_get_exception_security:
209 * Returns: a new instance of the System.Security.SecurityException
212 mono_get_exception_security ()
214 return mono_exception_from_name (mono_get_corlib (), "System.Security",
215 "SecurityException");
219 * mono_get_exception_thread_abort:
221 * Returns: a new instance of the System.Threading.ThreadAbortException.
224 mono_get_exception_thread_abort ()
226 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
227 "ThreadAbortException");
231 * mono_get_exception_thread_interrupted:
233 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
236 mono_get_exception_thread_interrupted ()
238 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
239 "ThreadInterruptedException");
243 * mono_get_exception_arithmetic:
245 * Returns: a new instance of the System.ArithmeticException.
248 mono_get_exception_arithmetic ()
250 return mono_exception_from_name (mono_get_corlib (), "System",
251 "ArithmeticException");
255 * mono_get_exception_overflow:
257 * Returns: a new instance of the System.OverflowException
260 mono_get_exception_overflow ()
262 return mono_exception_from_name (mono_get_corlib (), "System",
263 "OverflowException");
267 * mono_get_exception_null_reference:
269 * Returns: a new instance of the System.NullReferenceException
272 mono_get_exception_null_reference ()
274 return mono_exception_from_name (mono_get_corlib (), "System",
275 "NullReferenceException");
279 * mono_get_exception_execution_engine:
280 * @msg: the message to pass to the user
282 * Returns: a new instance of the System.ExecutionEngineException
285 mono_get_exception_execution_engine (const char *msg
)
287 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg
);
291 * mono_get_exception_serialization:
292 * @msg: the message to pass to the user
294 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
297 mono_get_exception_serialization (const char *msg
)
299 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg
);
303 * mono_get_exception_invalid_cast:
305 * Returns: a new instance of the System.InvalidCastException
308 mono_get_exception_invalid_cast ()
310 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
314 * mono_get_exception_invalid_operation:
315 * @msg: the message to pass to the user
317 * Returns: a new instance of the System.InvalidOperationException
320 mono_get_exception_invalid_operation (const char *msg
)
322 return mono_exception_from_name_msg (mono_get_corlib (), "System",
323 "InvalidOperationException", msg
);
327 * mono_get_exception_index_out_of_range:
329 * Returns: a new instance of the System.IndexOutOfRangeException
332 mono_get_exception_index_out_of_range ()
334 return mono_exception_from_name (mono_get_corlib (), "System",
335 "IndexOutOfRangeException");
339 * mono_get_exception_array_type_mismatch:
341 * Returns: a new instance of the System.ArrayTypeMismatchException
344 mono_get_exception_array_type_mismatch ()
346 return mono_exception_from_name (mono_get_corlib (), "System",
347 "ArrayTypeMismatchException");
351 * mono_get_exception_type_load:
352 * @class_name: the name of the class that could not be loaded
353 * @assembly_name: the assembly where the class was looked up.
355 * Returns: a new instance of the System.TypeLoadException.
358 mono_get_exception_type_load (MonoString
*class_name
, char *assembly_name
)
360 MonoString
*s
= assembly_name
? mono_string_new (mono_domain_get (), assembly_name
) : mono_string_new (mono_domain_get (), "");
362 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
363 "TypeLoadException", class_name
, s
);
367 * mono_get_exception_not_implemented:
368 * @msg: the message to pass to the user
370 * Returns: a new instance of the System.NotImplementedException
373 mono_get_exception_not_implemented (const char *msg
)
375 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg
);
379 * mono_get_exception_not_supported:
380 * @msg: the message to pass to the user
382 * Returns: a new instance of the System.NotSupportedException
385 mono_get_exception_not_supported (const char *msg
)
387 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg
);
391 * mono_get_exception_missing_method:
392 * @class_name: the class where the lookup was performed.
393 * @member_name: the name of the missing method.
395 * Returns: a new instance of the System.MissingMethodException
398 mono_get_exception_missing_method (const char *class_name
, const char *member_name
)
400 MonoString
*s1
= mono_string_new (mono_domain_get (), class_name
);
401 MonoString
*s2
= mono_string_new (mono_domain_get (), member_name
);
403 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
404 "MissingMethodException", s1
, s2
);
408 * mono_get_exception_missing_field:
409 * @class_name: the class where the lookup was performed
410 * @member_name: the name of the missing method.
412 * Returns: a new instance of the System.MissingFieldException
415 mono_get_exception_missing_field (const char *class_name
, const char *member_name
)
417 MonoString
*s1
= mono_string_new (mono_domain_get (), class_name
);
418 MonoString
*s2
= mono_string_new (mono_domain_get (), member_name
);
420 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
421 "MissingFieldException", s1
, s2
);
425 * mono_get_exception_argument_null:
426 * @arg: the name of the argument that is null
428 * Returns: a new instance of the System.ArgumentNullException
431 mono_get_exception_argument_null (const char *arg
)
435 ex
= mono_exception_from_name (
436 mono_get_corlib (), "System", "ArgumentNullException");
439 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
440 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
447 * mono_get_exception_argument:
448 * @arg: the name of the invalid argument.
450 * Returns: a new instance of the System.ArgumentException
453 mono_get_exception_argument (const char *arg
, const char *msg
)
457 ex
= mono_exception_from_name_msg (
458 mono_get_corlib (), "System", "ArgumentException", msg
);
461 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
462 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
469 * mono_get_exception_argument_out_of_range:
470 * @arg: the name of the out of range argument.
472 * Returns: a new instance of the System.ArgumentOutOfRangeException
475 mono_get_exception_argument_out_of_range (const char *arg
)
479 ex
= mono_exception_from_name (
480 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
483 MonoArgumentException
*argex
= (MonoArgumentException
*)ex
;
484 MONO_OBJECT_SETREF (argex
, param_name
, mono_string_new (mono_object_get_domain ((MonoObject
*)ex
), arg
));
491 * mono_get_exception_thread_state:
492 * @msg: the message to present to the user
494 * Returns: a new instance of the System.Threading.ThreadStateException
497 mono_get_exception_thread_state (const char *msg
)
499 return mono_exception_from_name_msg (
500 mono_get_corlib (), "System.Threading", "ThreadStateException", msg
);
504 * mono_get_exception_io:
505 * @msg: the message to present to the user
507 * Returns: a new instance of the System.IO.IOException
510 mono_get_exception_io (const char *msg
)
512 return mono_exception_from_name_msg (
513 mono_get_corlib (), "System.IO", "IOException", msg
);
517 * mono_get_exception_file_not_found:
518 * @fname: the name of the file not found.
520 * Returns: a new instance of the System.IO.FileNotFoundException
523 mono_get_exception_file_not_found (MonoString
*fname
)
525 return mono_exception_from_name_two_strings (
526 mono_get_corlib (), "System.IO", "FileNotFoundException", fname
, fname
);
530 * mono_get_exception_file_not_found2:
531 * @msg: an informative message for the user.
532 * @fname: the name of the file not found.
534 * Returns: a new instance of the System.IO.FileNotFoundException
537 mono_get_exception_file_not_found2 (const char *msg
, MonoString
*fname
)
539 MonoString
*s
= msg
? mono_string_new (mono_domain_get (), msg
) : NULL
;
541 return mono_exception_from_name_two_strings (
542 mono_get_corlib (), "System.IO", "FileNotFoundException", s
, fname
);
546 * mono_get_exception_type_initialization:
547 * @type_name: the name of the type that failed to initialize.
548 * @inner: the inner exception.
550 * Returns: a new instance of the System.TypeInitializationException
553 mono_get_exception_type_initialization (const gchar
*type_name
, MonoException
*inner
)
561 klass
= mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
564 mono_class_init (klass
);
566 /* TypeInitializationException only has 1 ctor with 2 args */
568 while ((method
= mono_class_get_methods (klass
, &iter
))) {
569 if (!strcmp (".ctor", mono_method_get_name (method
)) && mono_method_signature (method
)->param_count
== 2)
576 args
[0] = mono_string_new (mono_domain_get (), type_name
);
579 exc
= mono_object_new (mono_domain_get (), klass
);
580 mono_runtime_invoke (method
, exc
, args
, NULL
);
582 return (MonoException
*) exc
;
586 * mono_get_exception_synchronization_lock:
587 * @inner: the inner exception.
589 * Returns: a new instance of the System.TypeInitializationException
592 mono_get_exception_synchronization_lock (const char *msg
)
594 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg
);
598 * mono_get_exception_cannot_unload_appdomain:
599 * @inner: the inner exception.
601 * Returns: a new instance of the System.CannotUnloadAppDomainException
604 mono_get_exception_cannot_unload_appdomain (const char *msg
)
606 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg
);
610 * mono_get_exception_appdomain_unloaded
612 * Returns: a new instance of the System.AppDomainUnloadedException
615 mono_get_exception_appdomain_unloaded (void)
617 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
621 * mono_get_exception_bad_image_format:
622 * @msg: an informative message for the user.
624 * Returns: a new instance of the System.BadImageFormatException
627 mono_get_exception_bad_image_format (const char *msg
)
629 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg
);
633 * mono_get_exception_bad_image_format2:
634 * @msg: an informative message for the user.
635 * @fname: The full name of the file with the invalid image.
637 * Returns: a new instance of the System.BadImageFormatException
640 mono_get_exception_bad_image_format2 (const char *msg
, MonoString
*fname
)
642 MonoString
*s
= msg
? mono_string_new (mono_domain_get (), msg
) : NULL
;
644 return mono_exception_from_name_two_strings (
645 mono_get_corlib (), "System", "BadImageFormatException", s
, fname
);
649 * mono_get_exception_stack_overflow:
651 * Returns: a new instance of the System.StackOverflowException
654 mono_get_exception_stack_overflow (void)
656 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
660 * mono_get_exception_out_of_memory:
662 * Returns: a new instance of the System.OutOfMemoryException
665 mono_get_exception_out_of_memory (void)
667 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
671 * mono_get_exception_reflection_type_load:
672 * @types: an array of types that were defined in the moduled loaded.
673 * @exceptions: an array of exceptions that were thrown during the type loading.
675 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
678 mono_get_exception_reflection_type_load (MonoArray
*types
, MonoArray
*exceptions
)
685 klass
= mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
687 mono_class_init (klass
);
689 method
= mono_class_get_method_from_name (klass
, ".ctor", 2);
693 args
[1] = exceptions
;
695 exc
= mono_object_new (mono_domain_get (), klass
);
696 mono_runtime_invoke (method
, exc
, args
, NULL
);
698 return (MonoException
*) exc
;