2010-05-13 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / exception.c
blob7e12b63b824e1abdf55b4558943e3584e8468336
1 /*
2 * exception.c: Exception handling
4 * Authors:
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>
18 #include <string.h>
20 /**
21 * mono_exception_from_name:
22 * @image: the Mono image where to look for the class
23 * @name_space: the namespace for the class
24 * @name: class name
26 * Creates an exception of the given namespace/name class in the
27 * current domain.
29 * Returns: the initialized exception instance.
31 MonoException *
32 mono_exception_from_name (MonoImage *image, const char *name_space,
33 const char *name)
35 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
38 /**
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
43 * @name: class name
45 * Creates an exception object of the given namespace/name class on
46 * the given domain.
48 * Returns: the initialized exception instance.
50 MonoException *
51 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
52 const char* name_space, const char *name)
54 MonoClass *klass;
55 MonoObject *o;
56 MonoDomain *caller_domain = mono_domain_get ();
58 klass = mono_class_from_name (image, name_space, name);
60 o = mono_object_new (domain, klass);
61 g_assert (o != NULL);
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;
73 /**
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.
82 MonoException *
83 mono_exception_from_token (MonoImage *image, guint32 token)
85 MonoClass *klass;
86 MonoObject *o;
88 klass = mono_class_get (image, token);
90 o = mono_object_new (mono_domain_get (), klass);
91 g_assert (o != NULL);
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;
103 MonoObject *o;
104 int count = 1;
105 gpointer args [2];
106 gpointer iter;
107 MonoMethod *m;
109 if (a2 != NULL)
110 count++;
112 o = mono_object_new (domain, klass);
114 iter = NULL;
115 while ((m = mono_class_get_methods (klass, &iter))) {
116 MonoMethodSignature *sig;
118 if (strcmp (".ctor", mono_method_get_name (m)))
119 continue;
120 sig = mono_method_signature (m);
121 if (sig->param_count != count)
122 continue;
124 if (sig->params [0]->type != MONO_TYPE_STRING)
125 continue;
126 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
127 continue;
128 method = m;
129 break;
132 args [0] = a1;
133 args [1] = a2;
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
142 * @name: class name
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
147 * arguments.
149 * Returns: the initialized exception instance.
151 MonoException *
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
164 * @name: class name
165 * @msg: the message to embed inside the exception
167 * Creates an exception and initializes its message field.
169 * Returns: the initialized exception instance.
171 MonoException *
172 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
173 const char *name, const char *msg)
175 MonoException *ex;
177 ex = mono_exception_from_name (image, name_space, name);
179 if (msg)
180 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
182 return ex;
186 * mono_exception_from_token_two_strings:
188 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
189 * IMAGE and TOKEN.
191 MonoException *
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
205 MonoException *
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
217 MonoException *
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.
229 MonoException *
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.
241 MonoException *
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.
253 MonoException *
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
265 MonoException *
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
277 MonoException *
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
290 MonoException *
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
302 MonoException *
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
313 MonoException *
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
325 MonoException *
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
337 MonoException *
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
349 MonoException *
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.
363 MonoException *
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
378 MonoException *
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
390 MonoException *
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
403 MonoException *
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
420 MonoException *
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
436 MonoException*
437 mono_get_exception_argument_null (const char *arg)
439 MonoException *ex;
441 ex = mono_exception_from_name (
442 mono_get_corlib (), "System", "ArgumentNullException");
444 if (arg) {
445 MonoArgumentException *argex = (MonoArgumentException *)ex;
446 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
449 return ex;
453 * mono_get_exception_argument:
454 * @arg: the name of the invalid argument.
456 * Returns: a new instance of the System.ArgumentException
458 MonoException *
459 mono_get_exception_argument (const char *arg, const char *msg)
461 MonoException *ex;
463 ex = mono_exception_from_name_msg (
464 mono_get_corlib (), "System", "ArgumentException", msg);
466 if (arg) {
467 MonoArgumentException *argex = (MonoArgumentException *)ex;
468 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
471 return ex;
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
480 MonoException *
481 mono_get_exception_argument_out_of_range (const char *arg)
483 MonoException *ex;
485 ex = mono_exception_from_name (
486 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
488 if (arg) {
489 MonoArgumentException *argex = (MonoArgumentException *)ex;
490 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
493 return ex;
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
502 MonoException *
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
515 MonoException *
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
528 MonoException *
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
542 MonoException *
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
558 MonoException *
559 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
561 MonoClass *klass;
562 gpointer args [2];
563 MonoObject *exc;
564 MonoMethod *method;
565 gpointer iter;
567 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
568 g_assert (klass);
570 mono_class_init (klass);
572 /* TypeInitializationException only has 1 ctor with 2 args */
573 iter = NULL;
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)
576 break;
577 method = NULL;
580 g_assert (method);
582 args [0] = mono_string_new (mono_domain_get (), type_name);
583 args [1] = inner;
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
597 MonoException *
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
609 MonoException *
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
620 MonoException *
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
632 MonoException *
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
645 MonoException *
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
659 MonoException *
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
670 MonoException *
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
681 MonoException *
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
693 MonoException *
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
704 MonoException *
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
716 MonoException *
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
729 MonoException *
730 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
732 MonoClass *klass;
733 gpointer args [2];
734 MonoObject *exc;
735 MonoMethod *method;
737 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
738 g_assert (klass);
739 mono_class_init (klass);
741 method = mono_class_get_method_from_name (klass, ".ctor", 2);
742 g_assert (method);
744 args [0] = types;
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;