merge r98600
[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  * (C) 2001, 2002 Ximian, Inc.
11  */
12
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/appdomain.h>
16 #include <string.h>
17
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
23  *
24  * Creates an exception of the given namespace/name class in the
25  * current domain.
26  *
27  * Returns: the initialized exception instance.
28  */
29 MonoException *
30 mono_exception_from_name (MonoImage *image, const char *name_space,
31                           const char *name)
32 {
33         return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
34 }
35
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
42  *
43  * Creates an exception object of the given namespace/name class on
44  * the given domain.
45  *
46  * Returns: the initialized exception instance.
47  */
48 MonoException *
49 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
50                                  const char* name_space, const char *name)
51 {
52         MonoClass *klass;
53         MonoObject *o;
54
55         klass = mono_class_from_name (image, name_space, name);
56
57         o = mono_object_new (domain, klass);
58         g_assert (o != NULL);
59         
60         mono_runtime_object_init (o);
61
62         return (MonoException *)o;
63 }
64
65
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
70  *
71  * Creates an exception of the type given by @token.
72  *
73  * Returns: the initialized exception instance.
74  */
75 MonoException *
76 mono_exception_from_token (MonoImage *image, guint32 token)
77 {
78         MonoClass *klass;
79         MonoObject *o;
80
81         klass = mono_class_get (image, token);
82
83         o = mono_object_new (mono_domain_get (), klass);
84         g_assert (o != NULL);
85         
86         mono_runtime_object_init (o);
87
88         return (MonoException *)o;
89 }
90
91 static MonoException *
92 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
93 {
94         MonoDomain *domain = mono_domain_get ();
95         MonoMethod *method = NULL;
96         MonoObject *o;
97         int count = 1;
98         gpointer args [2];
99         gpointer iter;
100         MonoMethod *m;
101
102         if (a2 != NULL)
103                 count++;
104         
105         o = mono_object_new (domain, klass);
106
107         iter = NULL;
108         while ((m = mono_class_get_methods (klass, &iter))) {
109                 MonoMethodSignature *sig;
110                 
111                 if (strcmp (".ctor", mono_method_get_name (m)))
112                         continue;
113                 sig = mono_method_signature (m);
114                 if (sig->param_count != count)
115                         continue;
116
117                 if (sig->params [0]->type != MONO_TYPE_STRING)
118                         continue;
119                 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
120                         continue;
121                 method = m;
122                 break;
123         }
124
125         args [0] = a1;
126         args [1] = a2;
127         mono_runtime_invoke (method, o, args, NULL);
128         return (MonoException *) o;
129 }
130
131 /**
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
135  * @name: class name
136  * @a1: first string argument to pass
137  * @a2: second string argument to pass
138  *
139  * Creates an exception from a constructor that takes two string
140  * arguments.
141  *
142  * Returns: the initialized exception instance.
143  */
144 MonoException *
145 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
146                                       const char *name, MonoString *a1, MonoString *a2)
147 {
148         MonoClass *klass = mono_class_from_name (image, name_space, name);
149
150         return create_exception_two_strings (klass, a1, a2);
151 }
152
153 /**
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
157  * @name: class name
158  * @msg: the message to embed inside the exception
159  *
160  * Creates an exception and initializes its message field.
161  *
162  * Returns: the initialized exception instance.
163  */
164 MonoException *
165 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
166                               const char *name, const char *msg)
167 {
168         MonoException *ex;
169
170         ex = mono_exception_from_name (image, name_space, name);
171
172         if (msg)
173                 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
174
175         return ex;
176 }
177
178 /**
179  * mono_exception_from_token_two_strings:
180  *
181  *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
182  * IMAGE and TOKEN.
183  */
184 MonoException *
185 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
186                                                                            MonoString *a1, MonoString *a2)
187 {
188         MonoClass *klass = mono_class_get (image, token);
189
190         return create_exception_two_strings (klass, a1, a2);
191 }
192
193 /**
194  * mono_get_exception_divide_by_zero:
195  *
196  * Returns: a new instance of the System.DivideByZeroException
197  */
198 MonoException *
199 mono_get_exception_divide_by_zero ()
200 {
201         return mono_exception_from_name (mono_get_corlib (), "System",
202                                          "DivideByZeroException");
203 }
204
205 /**
206  * mono_get_exception_security:
207  *
208  * Returns: a new instance of the System.Security.SecurityException
209  */
210 MonoException *
211 mono_get_exception_security ()
212 {
213         return mono_exception_from_name (mono_get_corlib (), "System.Security",
214                                          "SecurityException");
215 }
216
217 /**
218  * mono_get_exception_thread_abort:
219  *
220  * Returns: a new instance of the System.Threading.ThreadAbortException.
221  */
222 MonoException *
223 mono_get_exception_thread_abort ()
224 {
225         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
226                                          "ThreadAbortException");
227 }
228
229 /**
230  * mono_get_exception_thread_interrupted:
231  *
232  * Returns: a new instance of the System.Threading.ThreadInterruptedException.
233  */
234 MonoException *
235 mono_get_exception_thread_interrupted ()
236 {
237         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
238                                          "ThreadInterruptedException");
239 }
240
241 /**
242  * mono_get_exception_arithmetic:
243  *
244  * Returns: a new instance of the System.ArithmeticException.
245  */
246 MonoException *
247 mono_get_exception_arithmetic ()
248 {
249         return mono_exception_from_name (mono_get_corlib (), "System",
250                                          "ArithmeticException");
251 }
252
253 /**
254  * mono_get_exception_overflow:
255  *
256  * Returns: a new instance of the System.OverflowException
257  */
258 MonoException *
259 mono_get_exception_overflow ()
260 {
261         return mono_exception_from_name (mono_get_corlib (), "System",
262                                          "OverflowException");
263 }
264
265 /**
266  * mono_get_exception_null_reference:
267  *
268  * Returns: a new instance of the System.NullReferenceException
269  */
270 MonoException *
271 mono_get_exception_null_reference ()
272 {
273         return mono_exception_from_name (mono_get_corlib (), "System",
274                                          "NullReferenceException");
275 }
276
277 /**
278  * mono_get_exception_execution_engine:
279  * @msg: the message to pass to the user
280  *
281  * Returns: a new instance of the System.ExecutionEngineException
282  */
283 MonoException *
284 mono_get_exception_execution_engine (const char *msg)
285 {
286         return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
287 }
288
289 /**
290  * mono_get_exception_serialization:
291  * @msg: the message to pass to the user
292  *
293  * Returns: a new instance of the System.Runtime.Serialization.SerializationException
294  */
295 MonoException *
296 mono_get_exception_serialization (const char *msg)
297 {
298         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
299 }
300
301 /**
302  * mono_get_exception_invalid_cast:
303  *
304  * Returns: a new instance of the System.InvalidCastException
305  */
306 MonoException *
307 mono_get_exception_invalid_cast ()
308 {
309         return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
310 }
311
312 /**
313  * mono_get_exception_invalid_operation:
314  * @msg: the message to pass to the user
315  *
316  * Returns: a new instance of the System.InvalidOperationException
317  */
318 MonoException *
319 mono_get_exception_invalid_operation (const char *msg)
320 {
321         return mono_exception_from_name_msg (mono_get_corlib (), "System",
322                                         "InvalidOperationException", msg);
323 }
324
325 /**
326  * mono_get_exception_index_out_of_range:
327  *
328  * Returns: a new instance of the System.IndexOutOfRangeException
329  */
330 MonoException *
331 mono_get_exception_index_out_of_range ()
332 {
333         return mono_exception_from_name (mono_get_corlib (), "System",
334                                          "IndexOutOfRangeException");
335 }
336
337 /**
338  * mono_get_exception_array_type_mismatch:
339  *
340  * Returns: a new instance of the System.ArrayTypeMismatchException
341  */
342 MonoException *
343 mono_get_exception_array_type_mismatch ()
344 {
345         return mono_exception_from_name (mono_get_corlib (), "System",
346                                          "ArrayTypeMismatchException");
347 }
348
349 /**
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.
353  *
354  * Returns: a new instance of the System.TypeLoadException.
355  */
356 MonoException *
357 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
358 {
359         MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
360
361         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
362                                                      "TypeLoadException", class_name, s);
363 }
364
365 /**
366  * mono_get_exception_not_implemented:
367  * @msg: the message to pass to the user
368  *
369  * Returns: a new instance of the System.NotImplementedException
370  */
371 MonoException *
372 mono_get_exception_not_implemented (const char *msg)
373 {
374         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
375 }
376
377 /**
378  * mono_get_exception_not_supported:
379  * @msg: the message to pass to the user
380  *
381  * Returns: a new instance of the System.NotSupportedException
382  */
383 MonoException *
384 mono_get_exception_not_supported (const char *msg)
385 {
386         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
387 }
388
389 /**
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.
393  *
394  * Returns: a new instance of the System.MissingMethodException
395  */
396 MonoException *
397 mono_get_exception_missing_method (const char *class_name, const char *member_name)
398 {
399         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
400         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
401
402         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
403                                                      "MissingMethodException", s1, s2);
404 }
405
406 /**
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.
410  *
411  * Returns: a new instance of the System.MissingFieldException
412  */
413 MonoException *
414 mono_get_exception_missing_field (const char *class_name, const char *member_name)
415 {
416         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
417         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
418
419         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
420                                                      "MissingFieldException", s1, s2);
421 }
422
423 /**
424  * mono_get_exception_argument_null:
425  * @arg: the name of the argument that is null
426  *
427  * Returns: a new instance of the System.ArgumentNullException
428  */
429 MonoException*
430 mono_get_exception_argument_null (const char *arg)
431 {
432         MonoException *ex;
433
434         ex = mono_exception_from_name ( 
435                 mono_get_corlib (), "System", "ArgumentNullException");
436
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));
440         }
441         
442         return ex;
443 }
444
445 /**
446  * mono_get_exception_argument:
447  * @arg: the name of the invalid argument.
448  *
449  * Returns: a new instance of the System.ArgumentException
450  */
451 MonoException *
452 mono_get_exception_argument (const char *arg, const char *msg)
453 {
454         MonoException *ex;
455
456         ex = mono_exception_from_name_msg (
457                 mono_get_corlib (), "System", "ArgumentException", msg);
458
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));
462         }
463         
464         return ex;
465 }
466
467 /**
468  * mono_get_exception_argument_out_of_range:
469  * @arg: the name of the out of range argument.
470  *
471  * Returns: a new instance of the System.ArgumentOutOfRangeException
472  */
473 MonoException *
474 mono_get_exception_argument_out_of_range (const char *arg)
475 {
476         MonoException *ex;
477
478         ex = mono_exception_from_name (
479                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
480
481         if (arg) {
482                 MonoArgumentException *argex = (MonoArgumentException *)ex;
483                 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
484         }
485         
486         return ex;
487 }
488
489 /**
490  * mono_get_exception_thread_state:
491  * @msg: the message to present to the user
492  *
493  * Returns: a new instance of the System.Threading.ThreadStateException
494  */
495 MonoException *
496 mono_get_exception_thread_state (const char *msg)
497 {
498         return mono_exception_from_name_msg (
499                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
500 }
501
502 /**
503  * mono_get_exception_io:
504  * @msg: the message to present to the user
505  *
506  * Returns: a new instance of the System.IO.IOException
507  */
508 MonoException *
509 mono_get_exception_io (const char *msg)
510 {
511         return mono_exception_from_name_msg ( 
512                 mono_get_corlib (), "System.IO", "IOException", msg);
513 }
514
515 /**
516  * mono_get_exception_file_not_found:
517  * @fname: the name of the file not found.
518  *
519  * Returns: a new instance of the System.IO.FileNotFoundException
520  */
521 MonoException *
522 mono_get_exception_file_not_found (MonoString *fname)
523 {
524         return mono_exception_from_name_two_strings (
525                 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
526 }
527
528 /**
529  * mono_get_exception_file_not_found2:
530  * @msg: an informative message for the user.
531  * @fname: the name of the file not found.
532  *
533  * Returns: a new instance of the System.IO.FileNotFoundException
534  */
535 MonoException *
536 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
537 {
538         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
539
540         return mono_exception_from_name_two_strings (
541                 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
542 }
543
544 /**
545  * mono_get_exception_type_initialization:
546  * @type_name: the name of the type that failed to initialize.
547  * @inner: the inner exception.
548  *
549  * Returns: a new instance of the System.TypeInitializationException
550  */
551 MonoException *
552 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
553 {
554         MonoClass *klass;
555         gpointer args [2];
556         MonoObject *exc;
557         MonoMethod *method;
558         gpointer iter;
559
560         klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
561         g_assert (klass);
562
563         mono_class_init (klass);
564
565         /* TypeInitializationException only has 1 ctor with 2 args */
566         iter = NULL;
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)
569                         break;
570                 method = NULL;
571         }
572
573         g_assert (method);
574
575         args [0] = mono_string_new (mono_domain_get (), type_name);
576         args [1] = inner;
577
578         exc = mono_object_new (mono_domain_get (), klass);
579         mono_runtime_invoke (method, exc, args, NULL);
580
581         return (MonoException *) exc;
582 }
583
584 /**
585  * mono_get_exception_synchronization_lock:
586  * @inner: the inner exception.
587  *
588  * Returns: a new instance of the System.TypeInitializationException
589  */
590 MonoException *
591 mono_get_exception_synchronization_lock (const char *msg)
592 {
593         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
594 }
595
596 /**
597  * mono_get_exception_cannot_unload_appdomain:
598  * @inner: the inner exception.
599  *
600  * Returns: a new instance of the System.CannotUnloadAppDomainException
601  */
602 MonoException *
603 mono_get_exception_cannot_unload_appdomain (const char *msg)
604 {
605         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
606 }
607
608 /**
609  * mono_get_exception_appdomain_unloaded
610  *
611  * Returns: a new instance of the System.AppDomainUnloadedException
612  */
613 MonoException *
614 mono_get_exception_appdomain_unloaded (void)
615 {
616         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
617 }
618
619 /**
620  * mono_get_exception_bad_image_format:
621  * @msg: an informative message for the user.
622  *
623  * Returns: a new instance of the System.BadImageFormatException
624  */
625 MonoException *
626 mono_get_exception_bad_image_format (const char *msg)
627 {
628         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
629 }       
630
631 /**
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.
635  *
636  * Returns: a new instance of the System.BadImageFormatException
637  */
638 MonoException *
639 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
640 {
641         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
642
643         return mono_exception_from_name_two_strings (
644                 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
645 }
646
647 /**
648  * mono_get_exception_stack_overflow:
649  *
650  * Returns: a new instance of the System.StackOverflowException
651  */
652 MonoException *
653 mono_get_exception_stack_overflow (void)
654 {
655         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
656 }
657
658 /**
659  * mono_get_exception_out_of_memory:
660  *
661  * Returns: a new instance of the System.OutOfMemoryException
662  */
663 MonoException *
664 mono_get_exception_out_of_memory (void)
665 {
666         return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
667 }
668
669 /**
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.
673  *
674  * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
675  */
676 MonoException *
677 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
678 {
679         MonoClass *klass;
680         gpointer args [2];
681         MonoObject *exc;
682         MonoMethod *method;
683
684         klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
685         g_assert (klass);
686         mono_class_init (klass);
687
688         method = mono_class_get_method_from_name (klass, ".ctor", 2);
689         g_assert (method);
690
691         args [0] = types;
692         args [1] = exceptions;
693
694         exc = mono_object_new (mono_domain_get (), klass);
695         mono_runtime_invoke (method, exc, args, NULL);
696
697         return (MonoException *) exc;
698 }