In Test/System.Data:
[mono-project.git] / mono / metadata / exception.c
blobb3bc6652efb4f917e1a88941fd5c4b38bbb6177f
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 * (C) 2001, 2002 Ximian, Inc.
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/appdomain.h>
16 #include <string.h>
18 /**
19 * mono_exception_from_name:
20 * @image: the Mono image where to look for the class
21 * @name_space: the namespace for the class
22 * @name: class name
24 * Creates an exception of the given namespace/name class in the
25 * current domain.
27 * Returns: the initialized exception instance.
29 MonoException *
30 mono_exception_from_name (MonoImage *image, const char *name_space,
31 const char *name)
33 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
36 /**
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
41 * @name: class name
43 * Creates an exception object of the given namespace/name class on
44 * the given domain.
46 * Returns: the initialized exception instance.
48 MonoException *
49 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
50 const char* name_space, const char *name)
52 MonoClass *klass;
53 MonoObject *o;
55 klass = mono_class_from_name (image, name_space, name);
57 o = mono_object_new (domain, klass);
58 g_assert (o != NULL);
60 mono_runtime_object_init (o);
62 return (MonoException *)o;
66 /**
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.
75 MonoException *
76 mono_exception_from_token (MonoImage *image, guint32 token)
78 MonoClass *klass;
79 MonoObject *o;
81 klass = mono_class_get (image, token);
83 o = mono_object_new (mono_domain_get (), klass);
84 g_assert (o != NULL);
86 mono_runtime_object_init (o);
88 return (MonoException *)o;
91 /**
92 * mono_exception_from_name_two_strings:
93 * @image: the Mono image where to look for the class
94 * @name_space: the namespace for the class
95 * @name: class name
96 * @a1: first string argument to pass
97 * @a2: second string argument to pass
99 * Creates an exception from a constructor that takes two string
100 * arguments.
102 * Returns: the initialized exception instance.
104 MonoException *
105 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
106 const char *name, MonoString *a1, MonoString *a2)
108 MonoDomain *domain = mono_domain_get ();
109 MonoClass *klass;
110 MonoMethod *method = NULL;
111 MonoObject *o;
112 int count = 1;
113 gpointer args [2];
114 gpointer iter;
115 MonoMethod *m;
117 if (a2 != NULL)
118 count++;
120 klass = mono_class_from_name (image, name_space, name);
121 o = mono_object_new (domain, klass);
123 iter = NULL;
124 while ((m = mono_class_get_methods (klass, &iter))) {
125 MonoMethodSignature *sig;
127 if (strcmp (".ctor", mono_method_get_name (m)))
128 continue;
129 sig = mono_method_signature (m);
130 if (sig->param_count != count)
131 continue;
133 if (sig->params [0]->type != MONO_TYPE_STRING)
134 continue;
135 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
136 continue;
137 method = m;
140 args [0] = a1;
141 args [1] = a2;
142 mono_runtime_invoke (method, o, args, NULL);
143 return (MonoException *) o;
147 * mono_exception_from_name_msg:
148 * @image: the Mono image where to look for the class
149 * @name_space: the namespace for the class
150 * @name: class name
151 * @msg: the message to embed inside the exception
153 * Creates an exception and initializes its message field.
155 * Returns: the initialized exception instance.
157 MonoException *
158 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
159 const char *name, const char *msg)
161 MonoException *ex;
163 ex = mono_exception_from_name (image, name_space, name);
165 if (msg)
166 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
168 return ex;
172 * mono_get_exception_divide_by_zero:
174 * Returns: a new instance of the System.DivideByZeroException
176 MonoException *
177 mono_get_exception_divide_by_zero ()
179 return mono_exception_from_name (mono_get_corlib (), "System",
180 "DivideByZeroException");
184 * mono_get_exception_security:
186 * Returns: a new instance of the System.Security.SecurityException
188 MonoException *
189 mono_get_exception_security ()
191 return mono_exception_from_name (mono_get_corlib (), "System.Security",
192 "SecurityException");
196 * mono_get_exception_thread_abort:
198 * Returns: a new instance of the System.Threading.ThreadAbortException.
200 MonoException *
201 mono_get_exception_thread_abort ()
203 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
204 "ThreadAbortException");
208 * mono_get_exception_thread_interrupted:
210 * Returns: a new instance of the System.Threading.ThreadInterruptedException.
212 MonoException *
213 mono_get_exception_thread_interrupted ()
215 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
216 "ThreadInterruptedException");
220 * mono_get_exception_arithmetic:
222 * Returns: a new instance of the System.ArithmeticException.
224 MonoException *
225 mono_get_exception_arithmetic ()
227 return mono_exception_from_name (mono_get_corlib (), "System",
228 "ArithmeticException");
232 * mono_get_exception_overflow:
234 * Returns: a new instance of the System.OverflowException
236 MonoException *
237 mono_get_exception_overflow ()
239 return mono_exception_from_name (mono_get_corlib (), "System",
240 "OverflowException");
244 * mono_get_exception_null_reference:
246 * Returns: a new instance of the System.NullReferenceException
248 MonoException *
249 mono_get_exception_null_reference ()
251 return mono_exception_from_name (mono_get_corlib (), "System",
252 "NullReferenceException");
256 * mono_get_exception_execution_engine:
257 * @msg: the message to pass to the user
259 * Returns: a new instance of the System.ExecutionEngineException
261 MonoException *
262 mono_get_exception_execution_engine (const char *msg)
264 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
268 * mono_get_exception_serialization:
269 * @msg: the message to pass to the user
271 * Returns: a new instance of the System.Runtime.Serialization.SerializationException
273 MonoException *
274 mono_get_exception_serialization (const char *msg)
276 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
280 * mono_get_exception_invalid_cast:
282 * Returns: a new instance of the System.InvalidCastException
284 MonoException *
285 mono_get_exception_invalid_cast ()
287 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
291 * mono_get_exception_invalid_operation:
292 * @msg: the message to pass to the user
294 * Returns: a new instance of the System.InvalidOperationException
296 MonoException *
297 mono_get_exception_invalid_operation (const char *msg)
299 return mono_exception_from_name_msg (mono_get_corlib (), "System",
300 "InvalidOperationException", msg);
304 * mono_get_exception_index_out_of_range:
306 * Returns: a new instance of the System.IndexOutOfRangeException
308 MonoException *
309 mono_get_exception_index_out_of_range ()
311 return mono_exception_from_name (mono_get_corlib (), "System",
312 "IndexOutOfRangeException");
316 * mono_get_exception_array_type_mismatch:
318 * Returns: a new instance of the System.ArrayTypeMismatchException
320 MonoException *
321 mono_get_exception_array_type_mismatch ()
323 return mono_exception_from_name (mono_get_corlib (), "System",
324 "ArrayTypeMismatchException");
328 * mono_get_exception_type_load:
329 * @class_name: the name of the class that could not be loaded
330 * @assembly_name: the assembly where the class was looked up.
332 * Returns: a new instance of the System.TypeLoadException.
334 MonoException *
335 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
337 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
339 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
340 "TypeLoadException", class_name, s);
344 * mono_get_exception_not_implemented:
345 * @msg: the message to pass to the user
347 * Returns: a new instance of the System.NotImplementedException
349 MonoException *
350 mono_get_exception_not_implemented (const char *msg)
352 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
356 * mono_get_exception_not_supported:
357 * @msg: the message to pass to the user
359 * Returns: a new instance of the System.NotSupportedException
361 MonoException *
362 mono_get_exception_not_supported (const char *msg)
364 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
368 * mono_get_exception_missing_method:
369 * @class_name: the class where the lookup was performed.
370 * @member_name: the name of the missing method.
372 * Returns: a new instance of the System.MissingMethodException
374 MonoException *
375 mono_get_exception_missing_method (const char *class_name, const char *member_name)
377 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
378 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
380 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
381 "MissingMethodException", s1, s2);
385 * mono_get_exception_missing_field:
386 * @class_name: the class where the lookup was performed
387 * @member_name: the name of the missing method.
389 * Returns: a new instance of the System.MissingFieldException
391 MonoException *
392 mono_get_exception_missing_field (const char *class_name, const char *member_name)
394 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
395 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
397 return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
398 "MissingFieldException", s1, s2);
402 * mono_get_exception_argument_null:
403 * @arg: the name of the argument that is null
405 * Returns: a new instance of the System.ArgumentNullException
407 MonoException*
408 mono_get_exception_argument_null (const char *arg)
410 MonoException *ex;
412 ex = mono_exception_from_name (
413 mono_get_corlib (), "System", "ArgumentNullException");
415 if (arg) {
416 MonoArgumentException *argex = (MonoArgumentException *)ex;
417 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
420 return ex;
424 * mono_get_exception_argument:
425 * @arg: the name of the invalid argument.
427 * Returns: a new instance of the System.ArgumentException
429 MonoException *
430 mono_get_exception_argument (const char *arg, const char *msg)
432 MonoException *ex;
434 ex = mono_exception_from_name_msg (
435 mono_get_corlib (), "System", "ArgumentException", msg);
437 if (arg) {
438 MonoArgumentException *argex = (MonoArgumentException *)ex;
439 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
442 return ex;
446 * mono_get_exception_argument_out_of_range:
447 * @arg: the name of the out of range argument.
449 * Returns: a new instance of the System.ArgumentOutOfRangeException
451 MonoException *
452 mono_get_exception_argument_out_of_range (const char *arg)
454 MonoException *ex;
456 ex = mono_exception_from_name (
457 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
459 if (arg) {
460 MonoArgumentException *argex = (MonoArgumentException *)ex;
461 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
464 return ex;
468 * mono_get_exception_thread_state:
469 * @msg: the message to present to the user
471 * Returns: a new instance of the System.Threading.ThreadStateException
473 MonoException *
474 mono_get_exception_thread_state (const char *msg)
476 return mono_exception_from_name_msg (
477 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
481 * mono_get_exception_io:
482 * @msg: the message to present to the user
484 * Returns: a new instance of the System.IO.IOException
486 MonoException *
487 mono_get_exception_io (const char *msg)
489 return mono_exception_from_name_msg (
490 mono_get_corlib (), "System.IO", "IOException", msg);
494 * mono_get_exception_file_not_found:
495 * @fname: the name of the file not found.
497 * Returns: a new instance of the System.IO.FileNotFoundException
499 MonoException *
500 mono_get_exception_file_not_found (MonoString *fname)
502 return mono_exception_from_name_two_strings (
503 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
507 * mono_get_exception_file_not_found2:
508 * @msg: an informative message for the user.
509 * @fname: the name of the file not found.
511 * Returns: a new instance of the System.IO.FileNotFoundException
513 MonoException *
514 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
516 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
518 return mono_exception_from_name_two_strings (
519 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
523 * mono_get_exception_type_initialization:
524 * @type_name: the name of the type that failed to initialize.
525 * @inner: the inner exception.
527 * Returns: a new instance of the System.TypeInitializationException
529 MonoException *
530 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
532 MonoClass *klass;
533 gpointer args [2];
534 MonoObject *exc;
535 MonoMethod *method;
536 gpointer iter;
538 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
539 g_assert (klass);
541 mono_class_init (klass);
543 /* TypeInitializationException only has 1 ctor with 2 args */
544 iter = NULL;
545 while ((method = mono_class_get_methods (klass, &iter))) {
546 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
547 break;
548 method = NULL;
551 g_assert (method);
553 args [0] = mono_string_new (mono_domain_get (), type_name);
554 args [1] = inner;
556 exc = mono_object_new (mono_domain_get (), klass);
557 mono_runtime_invoke (method, exc, args, NULL);
559 return (MonoException *) exc;
563 * mono_get_exception_synchronization_lock:
564 * @inner: the inner exception.
566 * Returns: a new instance of the System.TypeInitializationException
568 MonoException *
569 mono_get_exception_synchronization_lock (const char *msg)
571 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
575 * mono_get_exception_cannot_unload_appdomain:
576 * @inner: the inner exception.
578 * Returns: a new instance of the System.CannotUnloadAppDomainException
580 MonoException *
581 mono_get_exception_cannot_unload_appdomain (const char *msg)
583 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
587 * mono_get_exception_appdomain_unloaded
589 * Returns: a new instance of the System.AppDomainUnloadedException
591 MonoException *
592 mono_get_exception_appdomain_unloaded (void)
594 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
598 * mono_get_exception_bad_image_format:
599 * @msg: an informative message for the user.
601 * Returns: a new instance of the System.BadImageFormatException
603 MonoException *
604 mono_get_exception_bad_image_format (const char *msg)
606 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
610 * mono_get_exception_bad_image_format2:
611 * @msg: an informative message for the user.
612 * @fname: The full name of the file with the invalid image.
614 * Returns: a new instance of the System.BadImageFormatException
616 MonoException *
617 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
619 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
621 return mono_exception_from_name_two_strings (
622 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
626 * mono_get_exception_stack_overflow:
628 * Returns: a new instance of the System.StackOverflowException
630 MonoException *
631 mono_get_exception_stack_overflow (void)
633 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
637 * mono_get_exception_reflection_type_load:
638 * @types: an array of types that were defined in the moduled loaded.
639 * @exceptions: an array of exceptions that were thrown during the type loading.
641 * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
643 MonoException *
644 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
646 MonoClass *klass;
647 gpointer args [2];
648 MonoObject *exc;
649 MonoMethod *method;
651 klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
652 g_assert (klass);
653 mono_class_init (klass);
655 method = mono_class_get_method_from_name (klass, ".ctor", 2);
656 g_assert (method);
658 args [0] = types;
659 args [1] = exceptions;
661 exc = mono_object_new (mono_domain_get (), klass);
662 mono_runtime_invoke (method, exc, args, NULL);
664 return (MonoException *) exc;