remove genxs2 (no 2.0 profile assembly is built)
[mono-project/dkf.git] / mono / metadata / exception.c
blob7646208a5aa4021f8f77f1572d4e0b310b3de67f
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/appdomain.h>
17 #include <string.h>
19 /**
20 * mono_exception_from_name:
21 * @image: the Mono image where to look for the class
22 * @name_space: the namespace for the class
23 * @name: class name
25 * Creates an exception of the given namespace/name class in the
26 * current domain.
28 * Returns: the initialized exception instance.
30 MonoException *
31 mono_exception_from_name (MonoImage *image, const char *name_space,
32 const char *name)
34 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
37 /**
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
42 * @name: class name
44 * Creates an exception object of the given namespace/name class on
45 * the given domain.
47 * Returns: the initialized exception instance.
49 MonoException *
50 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
51 const char* name_space, const char *name)
53 MonoClass *klass;
54 MonoObject *o;
56 klass = mono_class_from_name (image, name_space, name);
58 o = mono_object_new (domain, klass);
59 g_assert (o != NULL);
61 mono_runtime_object_init (o);
63 return (MonoException *)o;
67 /**
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.
76 MonoException *
77 mono_exception_from_token (MonoImage *image, guint32 token)
79 MonoClass *klass;
80 MonoObject *o;
82 klass = mono_class_get (image, token);
84 o = mono_object_new (mono_domain_get (), klass);
85 g_assert (o != NULL);
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;
97 MonoObject *o;
98 int count = 1;
99 gpointer args [2];
100 gpointer iter;
101 MonoMethod *m;
103 if (a2 != NULL)
104 count++;
106 o = mono_object_new (domain, klass);
108 iter = NULL;
109 while ((m = mono_class_get_methods (klass, &iter))) {
110 MonoMethodSignature *sig;
112 if (strcmp (".ctor", mono_method_get_name (m)))
113 continue;
114 sig = mono_method_signature (m);
115 if (sig->param_count != count)
116 continue;
118 if (sig->params [0]->type != MONO_TYPE_STRING)
119 continue;
120 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
121 continue;
122 method = m;
123 break;
126 args [0] = a1;
127 args [1] = a2;
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
136 * @name: class name
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
141 * arguments.
143 * Returns: the initialized exception instance.
145 MonoException *
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
158 * @name: class name
159 * @msg: the message to embed inside the exception
161 * Creates an exception and initializes its message field.
163 * Returns: the initialized exception instance.
165 MonoException *
166 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
167 const char *name, const char *msg)
169 MonoException *ex;
171 ex = mono_exception_from_name (image, name_space, name);
173 if (msg)
174 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
176 return ex;
180 * mono_exception_from_token_two_strings:
182 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
183 * IMAGE and TOKEN.
185 MonoException *
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
199 MonoException *
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
211 MonoException *
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.
223 MonoException *
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.
235 MonoException *
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.
247 MonoException *
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
259 MonoException *
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
271 MonoException *
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
284 MonoException *
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
296 MonoException *
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
307 MonoException *
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
319 MonoException *
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
331 MonoException *
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
343 MonoException *
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.
357 MonoException *
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
372 MonoException *
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
384 MonoException *
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
397 MonoException *
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
414 MonoException *
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
430 MonoException*
431 mono_get_exception_argument_null (const char *arg)
433 MonoException *ex;
435 ex = mono_exception_from_name (
436 mono_get_corlib (), "System", "ArgumentNullException");
438 if (arg) {
439 MonoArgumentException *argex = (MonoArgumentException *)ex;
440 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
443 return ex;
447 * mono_get_exception_argument:
448 * @arg: the name of the invalid argument.
450 * Returns: a new instance of the System.ArgumentException
452 MonoException *
453 mono_get_exception_argument (const char *arg, const char *msg)
455 MonoException *ex;
457 ex = mono_exception_from_name_msg (
458 mono_get_corlib (), "System", "ArgumentException", msg);
460 if (arg) {
461 MonoArgumentException *argex = (MonoArgumentException *)ex;
462 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
465 return ex;
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
474 MonoException *
475 mono_get_exception_argument_out_of_range (const char *arg)
477 MonoException *ex;
479 ex = mono_exception_from_name (
480 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
482 if (arg) {
483 MonoArgumentException *argex = (MonoArgumentException *)ex;
484 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
487 return ex;
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
496 MonoException *
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
509 MonoException *
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
522 MonoException *
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
536 MonoException *
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
552 MonoException *
553 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
555 MonoClass *klass;
556 gpointer args [2];
557 MonoObject *exc;
558 MonoMethod *method;
559 gpointer iter;
561 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
562 g_assert (klass);
564 mono_class_init (klass);
566 /* TypeInitializationException only has 1 ctor with 2 args */
567 iter = NULL;
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)
570 break;
571 method = NULL;
574 g_assert (method);
576 args [0] = mono_string_new (mono_domain_get (), type_name);
577 args [1] = inner;
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
591 MonoException *
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
603 MonoException *
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
614 MonoException *
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
626 MonoException *
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
639 MonoException *
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
653 MonoException *
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
664 MonoException *
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
677 MonoException *
678 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
680 MonoClass *klass;
681 gpointer args [2];
682 MonoObject *exc;
683 MonoMethod *method;
685 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
686 g_assert (klass);
687 mono_class_init (klass);
689 method = mono_class_get_method_from_name (klass, ".ctor", 2);
690 g_assert (method);
692 args [0] = types;
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;