New tests.
[mono.git] / mono / metadata / exception.c
1 /*
2  * exception.c: Exception handling
3  *
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)
9  *
10  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12  */
13
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>
19
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
25  *
26  * Creates an exception of the given namespace/name class in the
27  * current domain.
28  *
29  * Returns: the initialized exception instance.
30  */
31 MonoException *
32 mono_exception_from_name (MonoImage *image, const char *name_space,
33                           const char *name)
34 {
35         return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
36 }
37
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
44  *
45  * Creates an exception object of the given namespace/name class on
46  * the given domain.
47  *
48  * Returns: the initialized exception instance.
49  */
50 MonoException *
51 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
52                                  const char* name_space, const char *name)
53 {
54         MonoClass *klass;
55         MonoObject *o;
56         MonoDomain *caller_domain = mono_domain_get ();
57
58         klass = mono_class_from_name (image, name_space, name);
59
60         o = mono_object_new (domain, klass);
61         g_assert (o != NULL);
62
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);
68
69         return (MonoException *)o;
70 }
71
72
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
77  *
78  * Creates an exception of the type given by @token.
79  *
80  * Returns: the initialized exception instance.
81  */
82 MonoException *
83 mono_exception_from_token (MonoImage *image, guint32 token)
84 {
85         MonoClass *klass;
86         MonoObject *o;
87
88         klass = mono_class_get (image, token);
89
90         o = mono_object_new (mono_domain_get (), klass);
91         g_assert (o != NULL);
92         
93         mono_runtime_object_init (o);
94
95         return (MonoException *)o;
96 }
97
98 static MonoException *
99 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
100 {
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;
108
109         if (a2 != NULL)
110                 count++;
111         
112         o = mono_object_new (domain, klass);
113
114         iter = NULL;
115         while ((m = mono_class_get_methods (klass, &iter))) {
116                 MonoMethodSignature *sig;
117                 
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;
123
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;
130         }
131
132         args [0] = a1;
133         args [1] = a2;
134         mono_runtime_invoke (method, o, args, NULL);
135         return (MonoException *) o;
136 }
137
138 /**
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
145  *
146  * Creates an exception from a constructor that takes two string
147  * arguments.
148  *
149  * Returns: the initialized exception instance.
150  */
151 MonoException *
152 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
153                                       const char *name, MonoString *a1, MonoString *a2)
154 {
155         MonoClass *klass = mono_class_from_name (image, name_space, name);
156
157         return create_exception_two_strings (klass, a1, a2);
158 }
159
160 /**
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
166  *
167  * Creates an exception and initializes its message field.
168  *
169  * Returns: the initialized exception instance.
170  */
171 MonoException *
172 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
173                               const char *name, const char *msg)
174 {
175         MonoException *ex;
176
177         ex = mono_exception_from_name (image, name_space, name);
178
179         if (msg)
180                 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
181
182         return ex;
183 }
184
185 /**
186  * mono_exception_from_token_two_strings:
187  *
188  *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
189  * IMAGE and TOKEN.
190  */
191 MonoException *
192 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
193                                                                            MonoString *a1, MonoString *a2)
194 {
195         MonoClass *klass = mono_class_get (image, token);
196
197         return create_exception_two_strings (klass, a1, a2);
198 }
199
200 /**
201  * mono_get_exception_divide_by_zero:
202  *
203  * Returns: a new instance of the System.DivideByZeroException
204  */
205 MonoException *
206 mono_get_exception_divide_by_zero ()
207 {
208         return mono_exception_from_name (mono_get_corlib (), "System",
209                                          "DivideByZeroException");
210 }
211
212 /**
213  * mono_get_exception_security:
214  *
215  * Returns: a new instance of the System.Security.SecurityException
216  */
217 MonoException *
218 mono_get_exception_security ()
219 {
220         return mono_exception_from_name (mono_get_corlib (), "System.Security",
221                                          "SecurityException");
222 }
223
224 /**
225  * mono_get_exception_thread_abort:
226  *
227  * Returns: a new instance of the System.Threading.ThreadAbortException.
228  */
229 MonoException *
230 mono_get_exception_thread_abort ()
231 {
232         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
233                                          "ThreadAbortException");
234 }
235
236 /**
237  * mono_get_exception_thread_interrupted:
238  *
239  * Returns: a new instance of the System.Threading.ThreadInterruptedException.
240  */
241 MonoException *
242 mono_get_exception_thread_interrupted ()
243 {
244         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
245                                          "ThreadInterruptedException");
246 }
247
248 /**
249  * mono_get_exception_arithmetic:
250  *
251  * Returns: a new instance of the System.ArithmeticException.
252  */
253 MonoException *
254 mono_get_exception_arithmetic ()
255 {
256         return mono_exception_from_name (mono_get_corlib (), "System",
257                                          "ArithmeticException");
258 }
259
260 /**
261  * mono_get_exception_overflow:
262  *
263  * Returns: a new instance of the System.OverflowException
264  */
265 MonoException *
266 mono_get_exception_overflow ()
267 {
268         return mono_exception_from_name (mono_get_corlib (), "System",
269                                          "OverflowException");
270 }
271
272 /**
273  * mono_get_exception_null_reference:
274  *
275  * Returns: a new instance of the System.NullReferenceException
276  */
277 MonoException *
278 mono_get_exception_null_reference ()
279 {
280         return mono_exception_from_name (mono_get_corlib (), "System",
281                                          "NullReferenceException");
282 }
283
284 /**
285  * mono_get_exception_execution_engine:
286  * @msg: the message to pass to the user
287  *
288  * Returns: a new instance of the System.ExecutionEngineException
289  */
290 MonoException *
291 mono_get_exception_execution_engine (const char *msg)
292 {
293         return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
294 }
295
296 /**
297  * mono_get_exception_serialization:
298  * @msg: the message to pass to the user
299  *
300  * Returns: a new instance of the System.Runtime.Serialization.SerializationException
301  */
302 MonoException *
303 mono_get_exception_serialization (const char *msg)
304 {
305         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
306 }
307
308 /**
309  * mono_get_exception_invalid_cast:
310  *
311  * Returns: a new instance of the System.InvalidCastException
312  */
313 MonoException *
314 mono_get_exception_invalid_cast ()
315 {
316         return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
317 }
318
319 /**
320  * mono_get_exception_invalid_operation:
321  * @msg: the message to pass to the user
322  *
323  * Returns: a new instance of the System.InvalidOperationException
324  */
325 MonoException *
326 mono_get_exception_invalid_operation (const char *msg)
327 {
328         return mono_exception_from_name_msg (mono_get_corlib (), "System",
329                                         "InvalidOperationException", msg);
330 }
331
332 /**
333  * mono_get_exception_index_out_of_range:
334  *
335  * Returns: a new instance of the System.IndexOutOfRangeException
336  */
337 MonoException *
338 mono_get_exception_index_out_of_range ()
339 {
340         return mono_exception_from_name (mono_get_corlib (), "System",
341                                          "IndexOutOfRangeException");
342 }
343
344 /**
345  * mono_get_exception_array_type_mismatch:
346  *
347  * Returns: a new instance of the System.ArrayTypeMismatchException
348  */
349 MonoException *
350 mono_get_exception_array_type_mismatch ()
351 {
352         return mono_exception_from_name (mono_get_corlib (), "System",
353                                          "ArrayTypeMismatchException");
354 }
355
356 /**
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.
360  *
361  * Returns: a new instance of the System.TypeLoadException.
362  */
363 MonoException *
364 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
365 {
366         MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
367
368         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
369                                                      "TypeLoadException", class_name, s);
370 }
371
372 /**
373  * mono_get_exception_not_implemented:
374  * @msg: the message to pass to the user
375  *
376  * Returns: a new instance of the System.NotImplementedException
377  */
378 MonoException *
379 mono_get_exception_not_implemented (const char *msg)
380 {
381         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
382 }
383
384 /**
385  * mono_get_exception_not_supported:
386  * @msg: the message to pass to the user
387  *
388  * Returns: a new instance of the System.NotSupportedException
389  */
390 MonoException *
391 mono_get_exception_not_supported (const char *msg)
392 {
393         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
394 }
395
396 /**
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.
400  *
401  * Returns: a new instance of the System.MissingMethodException
402  */
403 MonoException *
404 mono_get_exception_missing_method (const char *class_name, const char *member_name)
405 {
406         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
407         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
408
409         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
410                                                      "MissingMethodException", s1, s2);
411 }
412
413 /**
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.
417  *
418  * Returns: a new instance of the System.MissingFieldException
419  */
420 MonoException *
421 mono_get_exception_missing_field (const char *class_name, const char *member_name)
422 {
423         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
424         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
425
426         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
427                                                      "MissingFieldException", s1, s2);
428 }
429
430 /**
431  * mono_get_exception_argument_null:
432  * @arg: the name of the argument that is null
433  *
434  * Returns: a new instance of the System.ArgumentNullException
435  */
436 MonoException*
437 mono_get_exception_argument_null (const char *arg)
438 {
439         MonoException *ex;
440
441         ex = mono_exception_from_name ( 
442                 mono_get_corlib (), "System", "ArgumentNullException");
443
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));
447         }
448         
449         return ex;
450 }
451
452 /**
453  * mono_get_exception_argument:
454  * @arg: the name of the invalid argument.
455  *
456  * Returns: a new instance of the System.ArgumentException
457  */
458 MonoException *
459 mono_get_exception_argument (const char *arg, const char *msg)
460 {
461         MonoException *ex;
462
463         ex = mono_exception_from_name_msg (
464                 mono_get_corlib (), "System", "ArgumentException", msg);
465
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));
469         }
470         
471         return ex;
472 }
473
474 /**
475  * mono_get_exception_argument_out_of_range:
476  * @arg: the name of the out of range argument.
477  *
478  * Returns: a new instance of the System.ArgumentOutOfRangeException
479  */
480 MonoException *
481 mono_get_exception_argument_out_of_range (const char *arg)
482 {
483         MonoException *ex;
484
485         ex = mono_exception_from_name (
486                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
487
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));
491         }
492         
493         return ex;
494 }
495
496 /**
497  * mono_get_exception_thread_state:
498  * @msg: the message to present to the user
499  *
500  * Returns: a new instance of the System.Threading.ThreadStateException
501  */
502 MonoException *
503 mono_get_exception_thread_state (const char *msg)
504 {
505         return mono_exception_from_name_msg (
506                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
507 }
508
509 /**
510  * mono_get_exception_io:
511  * @msg: the message to present to the user
512  *
513  * Returns: a new instance of the System.IO.IOException
514  */
515 MonoException *
516 mono_get_exception_io (const char *msg)
517 {
518         return mono_exception_from_name_msg ( 
519                 mono_get_corlib (), "System.IO", "IOException", msg);
520 }
521
522 /**
523  * mono_get_exception_file_not_found:
524  * @fname: the name of the file not found.
525  *
526  * Returns: a new instance of the System.IO.FileNotFoundException
527  */
528 MonoException *
529 mono_get_exception_file_not_found (MonoString *fname)
530 {
531         return mono_exception_from_name_two_strings (
532                 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
533 }
534
535 /**
536  * mono_get_exception_file_not_found2:
537  * @msg: an informative message for the user.
538  * @fname: the name of the file not found.
539  *
540  * Returns: a new instance of the System.IO.FileNotFoundException
541  */
542 MonoException *
543 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
544 {
545         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
546
547         return mono_exception_from_name_two_strings (
548                 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
549 }
550
551 /**
552  * mono_get_exception_type_initialization:
553  * @type_name: the name of the type that failed to initialize.
554  * @inner: the inner exception.
555  *
556  * Returns: a new instance of the System.TypeInitializationException
557  */
558 MonoException *
559 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
560 {
561         MonoClass *klass;
562         gpointer args [2];
563         MonoObject *exc;
564         MonoMethod *method;
565         gpointer iter;
566
567         klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
568         g_assert (klass);
569
570         mono_class_init (klass);
571
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;
578         }
579
580         g_assert (method);
581
582         args [0] = mono_string_new (mono_domain_get (), type_name);
583         args [1] = inner;
584
585         exc = mono_object_new (mono_domain_get (), klass);
586         mono_runtime_invoke (method, exc, args, NULL);
587
588         return (MonoException *) exc;
589 }
590
591 /**
592  * mono_get_exception_synchronization_lock:
593  * @inner: the inner exception.
594  *
595  * Returns: a new instance of the System.SynchronizationLockException
596  */
597 MonoException *
598 mono_get_exception_synchronization_lock (const char *msg)
599 {
600         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
601 }
602
603 /**
604  * mono_get_exception_cannot_unload_appdomain:
605  * @inner: the inner exception.
606  *
607  * Returns: a new instance of the System.CannotUnloadAppDomainException
608  */
609 MonoException *
610 mono_get_exception_cannot_unload_appdomain (const char *msg)
611 {
612         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
613 }
614
615 /**
616  * mono_get_exception_appdomain_unloaded
617  *
618  * Returns: a new instance of the System.AppDomainUnloadedException
619  */
620 MonoException *
621 mono_get_exception_appdomain_unloaded (void)
622 {
623         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
624 }
625
626 /**
627  * mono_get_exception_bad_image_format:
628  * @msg: an informative message for the user.
629  *
630  * Returns: a new instance of the System.BadImageFormatException
631  */
632 MonoException *
633 mono_get_exception_bad_image_format (const char *msg)
634 {
635         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
636 }       
637
638 /**
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.
642  *
643  * Returns: a new instance of the System.BadImageFormatException
644  */
645 MonoException *
646 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
647 {
648         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
649
650         return mono_exception_from_name_two_strings (
651                 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
652 }
653
654 /**
655  * mono_get_exception_stack_overflow:
656  *
657  * Returns: a new instance of the System.StackOverflowException
658  */
659 MonoException *
660 mono_get_exception_stack_overflow (void)
661 {
662         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
663 }
664
665 /**
666  * mono_get_exception_out_of_memory:
667  *
668  * Returns: a new instance of the System.OutOfMemoryException
669  */
670 MonoException *
671 mono_get_exception_out_of_memory (void)
672 {
673         return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
674 }
675
676 /**
677  * mono_get_exception_field_access:
678  *
679  * Returns: a new instance of the System.FieldAccessException
680  */
681 MonoException *
682 mono_get_exception_field_access (void)
683 {
684         return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
685 }
686
687 /**
688  * mono_get_exception_field_access2:
689  * @msg: an informative message for the user.
690  *
691  * Returns: a new instance of the System.FieldAccessException
692  */
693 MonoException *
694 mono_get_exception_field_access_msg (const char *msg)
695 {
696         return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
697 }
698
699 /**
700  * mono_get_exception_method_access:
701  *
702  * Returns: a new instance of the System.MethodAccessException
703  */
704 MonoException *
705 mono_get_exception_method_access (void)
706 {
707         return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
708 }
709
710 /**
711  * mono_get_exception_method_access2:
712  * @msg: an informative message for the user.
713  *
714  * Returns: a new instance of the System.MethodAccessException
715  */
716 MonoException *
717 mono_get_exception_method_access_msg (const char *msg)
718 {
719         return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
720 }
721
722 /**
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.
726  *
727  * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
728  */
729 MonoException *
730 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
731 {
732         MonoClass *klass;
733         gpointer args [2];
734         MonoObject *exc;
735         MonoMethod *method;
736
737         klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
738         g_assert (klass);
739         mono_class_init (klass);
740
741         method = mono_class_get_method_from_name (klass, ".ctor", 2);
742         g_assert (method);
743
744         args [0] = types;
745         args [1] = exceptions;
746
747         exc = mono_object_new (mono_domain_get (), klass);
748         mono_runtime_invoke (method, exc, args, NULL);
749
750         return (MonoException *) exc;
751 }