2004-09-03 Zoltan Varga <vargaz@freemail.hu>
[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.
25  *
26  * Returns: the initialized exception instance.
27  */
28 MonoException *
29 mono_exception_from_name (MonoImage *image, const char *name_space,
30                           const char *name)
31 {
32         return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
33 }
34
35 MonoException *
36 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
37                                  const char* name_space, const char *name)
38 {
39         MonoClass *klass;
40         MonoObject *o;
41
42         klass = mono_class_from_name (image, name_space, name);
43
44         o = mono_object_new (domain, klass);
45         g_assert (o != NULL);
46         
47         mono_runtime_object_init (o);
48
49         return (MonoException *)o;
50 }
51
52 /**
53  * mono_exception_from_name_two_strings:
54  * @image: the Mono image where to look for the class
55  * @name_space: the namespace for the class
56  * @name: class name
57  * @a1: first string argument to pass
58  * @a2: second string argument to pass
59  *
60  * Creates an exception from a constructor that takes two string
61  * arguments.
62  *
63  * Returns: the initialized exception instance.
64  */
65 MonoException *
66 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
67                                       const char *name, MonoString *a1, MonoString *a2)
68 {
69         MonoDomain *domain = mono_domain_get ();
70         MonoClass *klass;
71         MonoMethod *method = NULL;
72         MonoObject *o;
73         int count = 1;
74         gpointer args [2];
75         gpointer iter;
76         MonoMethod *m;
77
78         if (a2 != NULL)
79                 count++;
80         
81         klass = mono_class_from_name (image, name_space, name);
82         o = mono_object_new (domain, klass);
83
84         iter = NULL;
85         while ((m = mono_class_get_methods (klass, &iter))) {
86                 MonoMethodSignature *sig;
87                 
88                 if (strcmp (".ctor", mono_method_get_name (m)))
89                         continue;
90                 sig = mono_method_signature (m);
91                 if (sig->param_count != count)
92                         continue;
93
94                 if (sig->params [0]->type != MONO_TYPE_STRING)
95                         continue;
96                 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
97                         continue;
98                 method = m;
99         }
100
101         args [0] = a1;
102         args [1] = a2;
103         mono_runtime_invoke (method, o, args, NULL);
104         return (MonoException *) o;
105 }
106
107 /**
108  * mono_exception_from_name_msg:
109  * @image: the Mono image where to look for the class
110  * @name_space: the namespace for the class
111  * @name: class name
112  * @msg: the message to embed inside the exception
113  *
114  * Creates an exception and initializes its message field.
115  *
116  * Returns: the initialized exception instance.
117  */
118 MonoException *
119 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
120                               const char *name, const guchar *msg)
121 {
122         MonoException *ex;
123
124         ex = mono_exception_from_name (image, name_space, name);
125
126         if (msg)
127                 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
128
129         return ex;
130 }
131
132 MonoException *
133 mono_get_exception_divide_by_zero ()
134 {
135         return mono_exception_from_name (mono_get_corlib (), "System",
136                                          "DivideByZeroException");
137 }
138
139 MonoException *
140 mono_get_exception_security ()
141 {
142         return mono_exception_from_name (mono_get_corlib (), "System.Security",
143                                          "SecurityException");
144 }
145
146 MonoException *
147 mono_get_exception_thread_abort ()
148 {
149         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
150                                          "ThreadAbortException");
151 }
152
153 MonoException *
154 mono_get_exception_arithmetic ()
155 {
156         return mono_exception_from_name (mono_get_corlib (), "System",
157                                          "ArithmeticException");
158 }
159
160 MonoException *
161 mono_get_exception_overflow ()
162 {
163         return mono_exception_from_name (mono_get_corlib (), "System",
164                                          "OverflowException");
165 }
166
167 MonoException *
168 mono_get_exception_null_reference ()
169 {
170         return mono_exception_from_name (mono_get_corlib (), "System",
171                                          "NullReferenceException");
172 }
173
174 MonoException *
175 mono_get_exception_execution_engine (const guchar *msg)
176 {
177         return mono_exception_from_name_msg (mono_get_corlib (), "System",
178                                                                                  "ExecutionEngineException", msg);
179 }
180
181 MonoException *
182 mono_get_exception_serialization (const guchar *msg)
183 {
184         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization",
185                                                                                  "SerializationException", msg);
186 }
187
188 MonoException *
189 mono_get_exception_invalid_cast ()
190 {
191         return mono_exception_from_name (mono_get_corlib (), "System",
192                                          "InvalidCastException");
193 }
194
195 MonoException *
196 mono_get_exception_index_out_of_range ()
197 {
198         return mono_exception_from_name (mono_get_corlib (), "System",
199                                          "IndexOutOfRangeException");
200 }
201
202 MonoException *
203 mono_get_exception_array_type_mismatch ()
204 {
205         return mono_exception_from_name (mono_get_corlib (), "System",
206                                          "ArrayTypeMismatchException");
207 }
208
209 MonoException *
210 mono_get_exception_type_load (MonoString *type_name)
211 {
212         MonoTypeLoadException *exc;
213         
214         exc = (MonoTypeLoadException *) mono_exception_from_name (mono_get_corlib (),
215                                         "System",
216                                         "TypeLoadException");
217
218         exc->type_name = type_name;
219         return (MonoException *) exc;
220 }
221
222 MonoException *
223 mono_get_exception_not_implemented (const guchar *msg)
224 {
225         MonoException *ex;
226         
227         ex = mono_exception_from_name (mono_get_corlib (), "System",
228                                        "NotImplementedException");
229
230         if (msg)
231                 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
232
233         return ex;
234 }
235
236 MonoException *
237 mono_get_exception_missing_method ()
238 {
239         return mono_exception_from_name (mono_get_corlib (), "System",
240                                          "MissingMethodException");
241 }
242
243 MonoException*
244 mono_get_exception_argument_null (const guchar *arg)
245 {
246         MonoException *ex;
247
248         ex = mono_exception_from_name ( 
249                 mono_get_corlib (), "System", "ArgumentNullException");
250
251         if (arg)
252                 ((MonoArgumentException *)ex)->param_name =
253                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
254         
255         return ex;
256 }
257
258 MonoException *
259 mono_get_exception_argument (const guchar *arg, const guchar *msg)
260 {
261         MonoException *ex;
262
263         ex = mono_exception_from_name_msg (
264                 mono_get_corlib (), "System", "ArgumentException", msg);
265
266         if (arg)
267                 ((MonoArgumentException *)ex)->param_name =
268                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
269         
270         return ex;
271 }
272
273 MonoException *
274 mono_get_exception_argument_out_of_range (const guchar *arg)
275 {
276         MonoException *ex;
277
278         ex = mono_exception_from_name (
279                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
280
281         if (arg)
282                 ((MonoArgumentException *)ex)->param_name =
283                         mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
284         
285         return ex;
286 }
287
288 MonoException *
289 mono_get_exception_thread_state (const guchar *msg)
290 {
291         return mono_exception_from_name_msg (
292                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
293 }
294
295 MonoException *
296 mono_get_exception_io (const guchar *msg)
297 {
298         return mono_exception_from_name_msg ( 
299                 mono_get_corlib (), "System.IO", "IOException", msg);
300 }
301
302 MonoException *
303 mono_get_exception_file_not_found (MonoString *fname)
304 {
305         return mono_exception_from_name_two_strings (
306                 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
307 }
308
309 MonoException *
310 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
311 {
312         MonoClass *klass;
313         gpointer args [2];
314         MonoObject *exc;
315         MonoMethod *method;
316         gpointer iter;
317
318         klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
319         g_assert (klass);
320
321         mono_class_init (klass);
322
323         /* TypeInitializationException only has 1 ctor with 2 args */
324         iter = NULL;
325         while ((method = mono_class_get_methods (klass, &iter))) {
326                 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
327                         break;
328                 method = NULL;
329         }
330
331         g_assert (method);
332
333         args [0] = mono_string_new (mono_domain_get (), type_name);
334         args [1] = inner;
335
336         exc = mono_object_new (mono_domain_get (), klass);
337         mono_runtime_invoke (method, exc, args, NULL);
338
339         return (MonoException *) exc;
340 }
341
342 MonoException *
343 mono_get_exception_synchronization_lock (const guchar *msg)
344 {
345         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
346 }
347
348 MonoException *
349 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
350 {
351         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
352 }
353
354 MonoException *
355 mono_get_exception_appdomain_unloaded (void)
356 {
357         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
358 }
359
360 MonoException *
361 mono_get_exception_bad_image_format (const guchar *msg)
362 {
363         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
364 }       
365
366 MonoException *
367 mono_get_exception_stack_overflow (void)
368 {
369         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
370 }