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 * (C) 2001, 2002 Ximian, Inc.
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/appdomain.h>
19 * mono_exception_from_name:
20 * @image: the Mono image where to look for the class
21 * @name_space: the namespace for the class
24 * Creates an exception of the given namespace/name class in the
27 * Returns: the initialized exception instance.
30 mono_exception_from_name (MonoImage *image, const char *name_space,
33 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
37 * mono_exception_from_name_domain:
38 * @domain: Domain where the return object will be created.
39 * @image: the Mono image where to look for the class
40 * @name_space: the namespace for the class
43 * Creates an exception object of the given namespace/name class on
46 * Returns: the initialized exception instance.
49 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
50 const char* name_space, const char *name)
55 klass = mono_class_from_name (image, name_space, name);
57 o = mono_object_new (domain, klass);
60 mono_runtime_object_init (o);
62 return (MonoException *)o;
67 * mono_exception_from_token:
68 * @image: the Mono image where to look for the class
69 * @token: The type token of the class
71 * Creates an exception of the type given by @token.
73 * Returns: the initialized exception instance.
76 mono_exception_from_token (MonoImage *image, guint32 token)
81 klass = mono_class_get (image, token);
83 o = mono_object_new (mono_domain_get (), klass);
86 mono_runtime_object_init (o);
88 return (MonoException *)o;
91 static MonoException *
92 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
94 MonoDomain *domain = mono_domain_get ();
95 MonoMethod *method = NULL;
105 o = mono_object_new (domain, klass);
108 while ((m = mono_class_get_methods (klass, &iter))) {
109 MonoMethodSignature *sig;
111 if (strcmp (".ctor", mono_method_get_name (m)))
113 sig = mono_method_signature (m);
114 if (sig->param_count != count)
117 if (sig->params [0]->type != MONO_TYPE_STRING)
119 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
127 mono_runtime_invoke (method, o, args, NULL);
128 return (MonoException *) o;
132 * mono_exception_from_name_two_strings:
133 * @image: the Mono image where to look for the class
134 * @name_space: the namespace for the class
136 * @a1: first string argument to pass
137 * @a2: second string argument to pass
139 * Creates an exception from a constructor that takes two string
142 * Returns: the initialized exception instance.
145 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
146 const char *name, MonoString *a1, MonoString *a2)
148 MonoClass *klass = mono_class_from_name (image, name_space, name);
150 return create_exception_two_strings (klass, a1, a2);
154 * mono_exception_from_name_msg:
155 * @image: the Mono image where to look for the class
156 * @name_space: the namespace for the class
158 * @msg: the message to embed inside the exception
160 * Creates an exception and initializes its message field.
162 * Returns: the initialized exception instance.
165 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
166 const char *name, const char *msg)
170 ex = mono_exception_from_name (image, name_space, name);
173 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
179 * mono_exception_from_token_two_strings:
181 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
185 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
186 MonoString *a1, MonoString *a2)
188 MonoClass *klass = mono_class_get (image, token);
190 return create_exception_two_strings (klass, a1, a2);
194 * mono_get_exception_divide_by_zero:
196 * Returns: a new instance of the System.DivideByZeroException
199 mono_get_exception_divide_by_zero ()
201 return mono_exception_from_name (mono_get_corlib (), "System",
202 "DivideByZeroException");
206 * mono_get_exception_security:
208 * Returns: a new instance of the System.Security.SecurityException
211 mono_get_exception_security ()
213 return mono_exception_from_name (mono_get_corlib (), "System.Security",
214 "SecurityException");
218 * mono_get_exception_thread_abort:
220 * Returns: a new instance of the System.Threading.ThreadAbortException.
223 mono_get_exception_thread_abort ()
225 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
226 "ThreadAbortException");
230 * mono_get_exception_thread_interrupted:
232 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
235 mono_get_exception_thread_interrupted ()
237 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
238 "ThreadInterruptedException");
242 * mono_get_exception_arithmetic:
244 * Returns: a new instance of the System.ArithmeticException.
247 mono_get_exception_arithmetic ()
249 return mono_exception_from_name (mono_get_corlib (), "System",
250 "ArithmeticException");
254 * mono_get_exception_overflow:
256 * Returns: a new instance of the System.OverflowException
259 mono_get_exception_overflow ()
261 return mono_exception_from_name (mono_get_corlib (), "System",
262 "OverflowException");
266 * mono_get_exception_null_reference:
268 * Returns: a new instance of the System.NullReferenceException
271 mono_get_exception_null_reference ()
273 return mono_exception_from_name (mono_get_corlib (), "System",
274 "NullReferenceException");
278 * mono_get_exception_execution_engine:
279 * @msg: the message to pass to the user
281 * Returns: a new instance of the System.ExecutionEngineException
284 mono_get_exception_execution_engine (const char *msg)
286 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
290 * mono_get_exception_serialization:
291 * @msg: the message to pass to the user
293 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
296 mono_get_exception_serialization (const char *msg)
298 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
302 * mono_get_exception_invalid_cast:
304 * Returns: a new instance of the System.InvalidCastException
307 mono_get_exception_invalid_cast ()
309 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
313 * mono_get_exception_invalid_operation:
314 * @msg: the message to pass to the user
316 * Returns: a new instance of the System.InvalidOperationException
319 mono_get_exception_invalid_operation (const char *msg)
321 return mono_exception_from_name_msg (mono_get_corlib (), "System",
322 "InvalidOperationException", msg);
326 * mono_get_exception_index_out_of_range:
328 * Returns: a new instance of the System.IndexOutOfRangeException
331 mono_get_exception_index_out_of_range ()
333 return mono_exception_from_name (mono_get_corlib (), "System",
334 "IndexOutOfRangeException");
338 * mono_get_exception_array_type_mismatch:
340 * Returns: a new instance of the System.ArrayTypeMismatchException
343 mono_get_exception_array_type_mismatch ()
345 return mono_exception_from_name (mono_get_corlib (), "System",
346 "ArrayTypeMismatchException");
350 * mono_get_exception_type_load:
351 * @class_name: the name of the class that could not be loaded
352 * @assembly_name: the assembly where the class was looked up.
354 * Returns: a new instance of the System.TypeLoadException.
357 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
359 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
361 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
362 "TypeLoadException", class_name, s);
366 * mono_get_exception_not_implemented:
367 * @msg: the message to pass to the user
369 * Returns: a new instance of the System.NotImplementedException
372 mono_get_exception_not_implemented (const char *msg)
374 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
378 * mono_get_exception_not_supported:
379 * @msg: the message to pass to the user
381 * Returns: a new instance of the System.NotSupportedException
384 mono_get_exception_not_supported (const char *msg)
386 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
390 * mono_get_exception_missing_method:
391 * @class_name: the class where the lookup was performed.
392 * @member_name: the name of the missing method.
394 * Returns: a new instance of the System.MissingMethodException
397 mono_get_exception_missing_method (const char *class_name, const char *member_name)
399 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
400 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
402 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
403 "MissingMethodException", s1, s2);
407 * mono_get_exception_missing_field:
408 * @class_name: the class where the lookup was performed
409 * @member_name: the name of the missing method.
411 * Returns: a new instance of the System.MissingFieldException
414 mono_get_exception_missing_field (const char *class_name, const char *member_name)
416 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
417 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
419 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
420 "MissingFieldException", s1, s2);
424 * mono_get_exception_argument_null:
425 * @arg: the name of the argument that is null
427 * Returns: a new instance of the System.ArgumentNullException
430 mono_get_exception_argument_null (const char *arg)
434 ex = mono_exception_from_name (
435 mono_get_corlib (), "System", "ArgumentNullException");
438 MonoArgumentException *argex = (MonoArgumentException *)ex;
439 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
446 * mono_get_exception_argument:
447 * @arg: the name of the invalid argument.
449 * Returns: a new instance of the System.ArgumentException
452 mono_get_exception_argument (const char *arg, const char *msg)
456 ex = mono_exception_from_name_msg (
457 mono_get_corlib (), "System", "ArgumentException", msg);
460 MonoArgumentException *argex = (MonoArgumentException *)ex;
461 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
468 * mono_get_exception_argument_out_of_range:
469 * @arg: the name of the out of range argument.
471 * Returns: a new instance of the System.ArgumentOutOfRangeException
474 mono_get_exception_argument_out_of_range (const char *arg)
478 ex = mono_exception_from_name (
479 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
482 MonoArgumentException *argex = (MonoArgumentException *)ex;
483 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
490 * mono_get_exception_thread_state:
491 * @msg: the message to present to the user
493 * Returns: a new instance of the System.Threading.ThreadStateException
496 mono_get_exception_thread_state (const char *msg)
498 return mono_exception_from_name_msg (
499 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
503 * mono_get_exception_io:
504 * @msg: the message to present to the user
506 * Returns: a new instance of the System.IO.IOException
509 mono_get_exception_io (const char *msg)
511 return mono_exception_from_name_msg (
512 mono_get_corlib (), "System.IO", "IOException", msg);
516 * mono_get_exception_file_not_found:
517 * @fname: the name of the file not found.
519 * Returns: a new instance of the System.IO.FileNotFoundException
522 mono_get_exception_file_not_found (MonoString *fname)
524 return mono_exception_from_name_two_strings (
525 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
529 * mono_get_exception_file_not_found2:
530 * @msg: an informative message for the user.
531 * @fname: the name of the file not found.
533 * Returns: a new instance of the System.IO.FileNotFoundException
536 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
538 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
540 return mono_exception_from_name_two_strings (
541 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
545 * mono_get_exception_type_initialization:
546 * @type_name: the name of the type that failed to initialize.
547 * @inner: the inner exception.
549 * Returns: a new instance of the System.TypeInitializationException
552 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
560 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
563 mono_class_init (klass);
565 /* TypeInitializationException only has 1 ctor with 2 args */
567 while ((method = mono_class_get_methods (klass, &iter))) {
568 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
575 args [0] = mono_string_new (mono_domain_get (), type_name);
578 exc = mono_object_new (mono_domain_get (), klass);
579 mono_runtime_invoke (method, exc, args, NULL);
581 return (MonoException *) exc;
585 * mono_get_exception_synchronization_lock:
586 * @inner: the inner exception.
588 * Returns: a new instance of the System.TypeInitializationException
591 mono_get_exception_synchronization_lock (const char *msg)
593 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
597 * mono_get_exception_cannot_unload_appdomain:
598 * @inner: the inner exception.
600 * Returns: a new instance of the System.CannotUnloadAppDomainException
603 mono_get_exception_cannot_unload_appdomain (const char *msg)
605 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
609 * mono_get_exception_appdomain_unloaded
611 * Returns: a new instance of the System.AppDomainUnloadedException
614 mono_get_exception_appdomain_unloaded (void)
616 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
620 * mono_get_exception_bad_image_format:
621 * @msg: an informative message for the user.
623 * Returns: a new instance of the System.BadImageFormatException
626 mono_get_exception_bad_image_format (const char *msg)
628 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
632 * mono_get_exception_bad_image_format2:
633 * @msg: an informative message for the user.
634 * @fname: The full name of the file with the invalid image.
636 * Returns: a new instance of the System.BadImageFormatException
639 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
641 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
643 return mono_exception_from_name_two_strings (
644 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
648 * mono_get_exception_stack_overflow:
650 * Returns: a new instance of the System.StackOverflowException
653 mono_get_exception_stack_overflow (void)
655 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
659 * mono_get_exception_out_of_memory:
661 * Returns: a new instance of the System.OutOfMemoryException
664 mono_get_exception_out_of_memory (void)
666 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
670 * mono_get_exception_reflection_type_load:
671 * @types: an array of types that were defined in the moduled loaded.
672 * @exceptions: an array of exceptions that were thrown during the type loading.
674 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
677 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
684 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
686 mono_class_init (klass);
688 method = mono_class_get_method_from_name (klass, ".ctor", 2);
692 args [1] = exceptions;
694 exc = mono_object_new (mono_domain_get (), klass);
695 mono_runtime_invoke (method, exc, args, NULL);
697 return (MonoException *) exc;