merge from trunk revisions 58933, 58935, 58936
[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 /**
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
98  *
99  * Creates an exception from a constructor that takes two string
100  * arguments.
101  *
102  * Returns: the initialized exception instance.
103  */
104 MonoException *
105 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
106                                       const char *name, MonoString *a1, MonoString *a2)
107 {
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;
116
117         if (a2 != NULL)
118                 count++;
119         
120         klass = mono_class_from_name (image, name_space, name);
121         o = mono_object_new (domain, klass);
122
123         iter = NULL;
124         while ((m = mono_class_get_methods (klass, &iter))) {
125                 MonoMethodSignature *sig;
126                 
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;
132
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;
138         }
139
140         args [0] = a1;
141         args [1] = a2;
142         mono_runtime_invoke (method, o, args, NULL);
143         return (MonoException *) o;
144 }
145
146 /**
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
152  *
153  * Creates an exception and initializes its message field.
154  *
155  * Returns: the initialized exception instance.
156  */
157 MonoException *
158 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
159                               const char *name, const char *msg)
160 {
161         MonoException *ex;
162
163         ex = mono_exception_from_name (image, name_space, name);
164
165         if (msg)
166                 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
167
168         return ex;
169 }
170
171 /**
172  * mono_get_exception_divide_by_zero:
173  *
174  * Returns: a new instance of the System.DivideByZeroException
175  */
176 MonoException *
177 mono_get_exception_divide_by_zero ()
178 {
179         return mono_exception_from_name (mono_get_corlib (), "System",
180                                          "DivideByZeroException");
181 }
182
183 /**
184  * mono_get_exception_security:
185  *
186  * Returns: a new instance of the System.Security.SecurityException
187  */
188 MonoException *
189 mono_get_exception_security ()
190 {
191         return mono_exception_from_name (mono_get_corlib (), "System.Security",
192                                          "SecurityException");
193 }
194
195 /**
196  * mono_get_exception_thread_abort:
197  *
198  * Returns: a new instance of the System.Threading.ThreadAbortException.
199  */
200 MonoException *
201 mono_get_exception_thread_abort ()
202 {
203         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
204                                          "ThreadAbortException");
205 }
206
207 /**
208  * mono_get_exception_arithmetic:
209  *
210  * Returns: a new instance of the System.ArithmeticException.
211  */
212 MonoException *
213 mono_get_exception_arithmetic ()
214 {
215         return mono_exception_from_name (mono_get_corlib (), "System",
216                                          "ArithmeticException");
217 }
218
219 /**
220  * mono_get_exception_overflow:
221  *
222  * Returns: a new instance of the System.OverflowException
223  */
224 MonoException *
225 mono_get_exception_overflow ()
226 {
227         return mono_exception_from_name (mono_get_corlib (), "System",
228                                          "OverflowException");
229 }
230
231 /**
232  * mono_get_exception_null_reference:
233  *
234  * Returns: a new instance of the System.NullReferenceException
235  */
236 MonoException *
237 mono_get_exception_null_reference ()
238 {
239         return mono_exception_from_name (mono_get_corlib (), "System",
240                                          "NullReferenceException");
241 }
242
243 /**
244  * mono_get_exception_execution_engine:
245  * @msg: the message to pass to the user
246  *
247  * Returns: a new instance of the System.ExecutionEngineException
248  */
249 MonoException *
250 mono_get_exception_execution_engine (const char *msg)
251 {
252         return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
253 }
254
255 /**
256  * mono_get_exception_serialization:
257  * @msg: the message to pass to the user
258  *
259  * Returns: a new instance of the System.Runtime.Serialization.SerializationException
260  */
261 MonoException *
262 mono_get_exception_serialization (const char *msg)
263 {
264         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
265 }
266
267 /**
268  * mono_get_exception_invalid_cast:
269  *
270  * Returns: a new instance of the System.InvalidCastException
271  */
272 MonoException *
273 mono_get_exception_invalid_cast ()
274 {
275         return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
276 }
277
278 /**
279  * mono_get_exception_invalid_operation:
280  * @msg: the message to pass to the user
281  *
282  * Returns: a new instance of the System.InvalidOperationException
283  */
284 MonoException *
285 mono_get_exception_invalid_operation (const char *msg)
286 {
287         return mono_exception_from_name_msg (mono_get_corlib (), "System",
288                                         "InvalidOperationException", msg);
289 }
290
291 /**
292  * mono_get_exception_index_out_of_range:
293  *
294  * Returns: a new instance of the System.IndexOutOfRangeException
295  */
296 MonoException *
297 mono_get_exception_index_out_of_range ()
298 {
299         return mono_exception_from_name (mono_get_corlib (), "System",
300                                          "IndexOutOfRangeException");
301 }
302
303 /**
304  * mono_get_exception_array_type_mismatch:
305  *
306  * Returns: a new instance of the System.ArrayTypeMismatchException
307  */
308 MonoException *
309 mono_get_exception_array_type_mismatch ()
310 {
311         return mono_exception_from_name (mono_get_corlib (), "System",
312                                          "ArrayTypeMismatchException");
313 }
314
315 /**
316  * mono_get_exception_type_load:
317  * @class_name: the name of the class that could not be loaded
318  * @assembly_name: the assembly where the class was looked up.
319  *
320  * Returns: a new instance of the System.TypeLoadException.
321  */
322 MonoException *
323 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
324 {
325         MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
326
327         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
328                                                      "TypeLoadException", class_name, s);
329 }
330
331 /**
332  * mono_get_exception_not_implemented:
333  * @msg: the message to pass to the user
334  *
335  * Returns: a new instance of the System.NotImplementedException
336  */
337 MonoException *
338 mono_get_exception_not_implemented (const char *msg)
339 {
340         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
341 }
342
343 /**
344  * mono_get_exception_not_supported:
345  * @msg: the message to pass to the user
346  *
347  * Returns: a new instance of the System.NotSupportedException
348  */
349 MonoException *
350 mono_get_exception_not_supported (const char *msg)
351 {
352         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
353 }
354
355 /**
356  * mono_get_exception_missing_method:
357  * @class_name: the class where the lookup was performed.
358  * @member_name: the name of the missing method.
359  *
360  * Returns: a new instance of the System.MissingMethodException
361  */
362 MonoException *
363 mono_get_exception_missing_method (const char *class_name, const char *member_name)
364 {
365         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
366         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
367
368         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
369                                                      "MissingMethodException", s1, s2);
370 }
371
372 /**
373  * mono_get_exception_missing_field:
374  * @class_name: the class where the lookup was performed
375  * @member_name: the name of the missing method.
376  *
377  * Returns: a new instance of the System.MissingFieldException
378  */
379 MonoException *
380 mono_get_exception_missing_field (const char *class_name, const char *member_name)
381 {
382         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
383         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
384
385         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
386                                                      "MissingFieldException", s1, s2);
387 }
388
389 /**
390  * mono_get_exception_argument_null:
391  * @arg: the name of the argument that is null
392  *
393  * Returns: a new instance of the System.ArgumentNullException
394  */
395 MonoException*
396 mono_get_exception_argument_null (const char *arg)
397 {
398         MonoException *ex;
399
400         ex = mono_exception_from_name ( 
401                 mono_get_corlib (), "System", "ArgumentNullException");
402
403         if (arg)
404                 ((MonoArgumentException *)ex)->param_name =
405                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
406         
407         return ex;
408 }
409
410 /**
411  * mono_get_exception_argument:
412  * @arg: the name of the invalid argument.
413  *
414  * Returns: a new instance of the System.ArgumentException
415  */
416 MonoException *
417 mono_get_exception_argument (const char *arg, const char *msg)
418 {
419         MonoException *ex;
420
421         ex = mono_exception_from_name_msg (
422                 mono_get_corlib (), "System", "ArgumentException", msg);
423
424         if (arg)
425                 ((MonoArgumentException *)ex)->param_name =
426                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
427         
428         return ex;
429 }
430
431 /**
432  * mono_get_exception_argument_out_of_range:
433  * @arg: the name of the out of range argument.
434  *
435  * Returns: a new instance of the System.ArgumentOutOfRangeException
436  */
437 MonoException *
438 mono_get_exception_argument_out_of_range (const char *arg)
439 {
440         MonoException *ex;
441
442         ex = mono_exception_from_name (
443                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
444
445         if (arg)
446                 ((MonoArgumentException *)ex)->param_name =
447                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
448         
449         return ex;
450 }
451
452 /**
453  * mono_get_exception_thread_state:
454  * @msg: the message to present to the user
455  *
456  * Returns: a new instance of the System.Threading.ThreadStateException
457  */
458 MonoException *
459 mono_get_exception_thread_state (const char *msg)
460 {
461         return mono_exception_from_name_msg (
462                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
463 }
464
465 /**
466  * mono_get_exception_io:
467  * @msg: the message to present to the user
468  *
469  * Returns: a new instance of the System.IO.IOException
470  */
471 MonoException *
472 mono_get_exception_io (const char *msg)
473 {
474         return mono_exception_from_name_msg ( 
475                 mono_get_corlib (), "System.IO", "IOException", msg);
476 }
477
478 /**
479  * mono_get_exception_file_not_found:
480  * @fname: the name of the file not found.
481  *
482  * Returns: a new instance of the System.IO.FileNotFoundException
483  */
484 MonoException *
485 mono_get_exception_file_not_found (MonoString *fname)
486 {
487         return mono_exception_from_name_two_strings (
488                 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
489 }
490
491 /**
492  * mono_get_exception_file_not_found2:
493  * @msg: an informative message for the user.
494  * @fname: the name of the file not found.
495  *
496  * Returns: a new instance of the System.IO.FileNotFoundException
497  */
498 MonoException *
499 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
500 {
501         MonoString *s = mono_string_new (mono_domain_get (), msg);
502
503         return mono_exception_from_name_two_strings (
504                 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
505 }
506
507 /**
508  * mono_get_exception_type_initialization:
509  * @type_name: the name of the type that failed to initialize.
510  * @inner: the inner exception.
511  *
512  * Returns: a new instance of the System.TypeInitializationException
513  */
514 MonoException *
515 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
516 {
517         MonoClass *klass;
518         gpointer args [2];
519         MonoObject *exc;
520         MonoMethod *method;
521         gpointer iter;
522
523         klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
524         g_assert (klass);
525
526         mono_class_init (klass);
527
528         /* TypeInitializationException only has 1 ctor with 2 args */
529         iter = NULL;
530         while ((method = mono_class_get_methods (klass, &iter))) {
531                 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
532                         break;
533                 method = NULL;
534         }
535
536         g_assert (method);
537
538         args [0] = mono_string_new (mono_domain_get (), type_name);
539         args [1] = inner;
540
541         exc = mono_object_new (mono_domain_get (), klass);
542         mono_runtime_invoke (method, exc, args, NULL);
543
544         return (MonoException *) exc;
545 }
546
547 /**
548  * mono_get_exception_synchronization_lock:
549  * @inner: the inner exception.
550  *
551  * Returns: a new instance of the System.TypeInitializationException
552  */
553 MonoException *
554 mono_get_exception_synchronization_lock (const char *msg)
555 {
556         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
557 }
558
559 /**
560  * mono_get_exception_cannot_unload_appdomain:
561  * @inner: the inner exception.
562  *
563  * Returns: a new instance of the System.CannotUnloadAppDomainException
564  */
565 MonoException *
566 mono_get_exception_cannot_unload_appdomain (const char *msg)
567 {
568         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
569 }
570
571 /**
572  * mono_get_exception_appdomain_unloaded
573  *
574  * Returns: a new instance of the System.AppDomainUnloadedException
575  */
576 MonoException *
577 mono_get_exception_appdomain_unloaded (void)
578 {
579         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
580 }
581
582 /**
583  * mono_get_exception_bad_image_format:
584  * @msg: an informative message for the user.
585  *
586  * Returns: a new instance of the System.BadImageFormatException
587  */
588 MonoException *
589 mono_get_exception_bad_image_format (const char *msg)
590 {
591         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
592 }       
593
594 /**
595  * mono_get_exception_stack_overflow:
596  *
597  * Returns: a new instance of the System.StackOverflowException
598  */
599 MonoException *
600 mono_get_exception_stack_overflow (void)
601 {
602         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
603 }
604
605 /**
606  * mono_get_exception_reflection_type_load:
607  * @types: an array of types that were defined in the moduled loaded.
608  * @exceptions: an array of exceptions that were thrown during the type loading.
609  *
610  * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
611  */
612 MonoException *
613 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
614 {
615         MonoClass *klass;
616         gpointer args [2];
617         MonoObject *exc;
618         MonoMethod *method;
619
620         klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
621         g_assert (klass);
622         mono_class_init (klass);
623
624         method = mono_class_get_method_from_name (klass, ".ctor", 2);
625         g_assert (method);
626
627         args [0] = types;
628         args [1] = exceptions;
629
630         exc = mono_object_new (mono_domain_get (), klass);
631         mono_runtime_invoke (method, exc, args, NULL);
632
633         return (MonoException *) exc;
634 }