2 * exception.c: Exception handling
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/class.h>
15 #include <mono/metadata/appdomain.h>
19 * mono_exception_from_name:
20 * @image: the Mono image where to look for the class
21 * @name_space: the namespace for the class
24 * Creates an exception of the given namespace/name class.
26 * Returns: the initialized exception instance.
29 mono_exception_from_name (MonoImage *image, const char *name_space,
32 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
36 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
37 const char* name_space, const char *name)
42 klass = mono_class_from_name (image, name_space, name);
44 o = mono_object_new (domain, klass);
47 mono_runtime_object_init (o);
49 return (MonoException *)o;
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
57 * @a1: first string argument to pass
58 * @a2: second string argument to pass
60 * Creates an exception from a constructor that takes two string
63 * Returns: the initialized exception instance.
66 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
67 const char *name, MonoString *a1, MonoString *a2)
69 MonoDomain *domain = mono_domain_get ();
71 MonoMethod *method = NULL;
79 klass = mono_class_from_name (image, name_space, name);
80 o = mono_object_new (domain, klass);
82 for (i = 0; i < klass->method.count; ++i) {
83 MonoMethodSignature *sig;
85 if (strcmp (".ctor", klass->methods [i]->name))
87 sig = klass->methods [i]->signature;
88 if (sig->param_count != count)
91 if (sig->params [0]->type != MONO_TYPE_STRING)
93 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
95 method = klass->methods [i];
100 mono_runtime_invoke (method, o, args, NULL);
101 return (MonoException *) o;
105 * mono_exception_from_name_msg:
106 * @image: the Mono image where to look for the class
107 * @name_space: the namespace for the class
109 * @msg: the message to embed inside the exception
111 * Creates an exception and initializes its message field.
113 * Returns: the initialized exception instance.
116 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
117 const char *name, const guchar *msg)
122 ex = mono_exception_from_name (image, name_space, name);
124 domain = ((MonoObject *)ex)->vtable->domain;
127 ex->message = mono_string_new (domain, msg);
133 mono_get_exception_divide_by_zero ()
135 return mono_exception_from_name (mono_defaults.corlib, "System",
136 "DivideByZeroException");
140 mono_get_exception_security ()
142 return mono_exception_from_name (mono_defaults.corlib, "System",
143 "SecurityException");
147 mono_get_exception_thread_abort ()
149 return mono_exception_from_name (mono_defaults.corlib, "System.Threading",
150 "ThreadAbortException");
154 mono_get_exception_arithmetic ()
156 return mono_exception_from_name (mono_defaults.corlib, "System",
157 "ArithmeticException");
161 mono_get_exception_overflow ()
163 return mono_exception_from_name (mono_defaults.corlib, "System",
164 "OverflowException");
168 mono_get_exception_null_reference ()
170 return mono_exception_from_name (mono_defaults.corlib, "System",
171 "NullReferenceException");
175 mono_get_exception_execution_engine (const guchar *msg)
177 return mono_exception_from_name_msg (mono_defaults.corlib, "System",
178 "ExecutionEngineException", msg);
182 mono_get_exception_serialization (const guchar *msg)
184 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Runtime.Serialization",
185 "SerializationException", msg);
189 mono_get_exception_invalid_cast ()
191 return mono_exception_from_name (mono_defaults.corlib, "System",
192 "InvalidCastException");
196 mono_get_exception_index_out_of_range ()
198 return mono_exception_from_name (mono_defaults.corlib, "System",
199 "IndexOutOfRangeException");
203 mono_get_exception_array_type_mismatch ()
205 return mono_exception_from_name (mono_defaults.corlib, "System",
206 "ArrayTypeMismatchException");
210 mono_get_exception_type_load (MonoString *type_name)
212 MonoTypeLoadException *exc;
214 exc = (MonoTypeLoadException *) mono_exception_from_name (mono_defaults.corlib,
216 "TypeLoadException");
218 exc->type_name = type_name;
219 return (MonoException *) exc;
223 mono_get_exception_not_implemented (const guchar *msg)
228 ex = mono_exception_from_name (mono_defaults.corlib, "System",
229 "NotImplementedException");
230 domain = ((MonoObject *)ex)->vtable->domain;
233 ex->message = mono_string_new (domain, msg);
239 mono_get_exception_missing_method ()
241 return mono_exception_from_name (mono_defaults.corlib, "System",
242 "MissingMethodException");
246 mono_get_exception_argument_null (const guchar *arg)
251 ex = mono_exception_from_name (
252 mono_defaults.corlib, "System", "ArgumentNullException");
254 domain = ((MonoObject *)ex)->vtable->domain;
257 ((MonoArgumentException *)ex)->param_name =
258 mono_string_new (domain, arg);
264 mono_get_exception_argument (const guchar *arg, const guchar *msg)
269 ex = mono_exception_from_name_msg (
270 mono_defaults.corlib, "System", "ArgumentException", msg);
272 domain = ((MonoObject *)ex)->vtable->domain;
275 ((MonoArgumentException *)ex)->param_name =
276 mono_string_new (domain, arg);
282 mono_get_exception_argument_out_of_range (const guchar *arg)
287 ex = mono_exception_from_name (
288 mono_defaults.corlib, "System", "ArgumentOutOfRangeException");
290 domain = ((MonoObject *)ex)->vtable->domain;
293 ((MonoArgumentException *)ex)->param_name =
294 mono_string_new (domain, arg);
300 mono_get_exception_thread_state (const guchar *msg)
302 return mono_exception_from_name_msg (
303 mono_defaults.corlib, "System.Threading", "ThreadStateException", msg);
307 mono_get_exception_io (const guchar *msg)
309 return mono_exception_from_name_msg (
310 mono_defaults.corlib, "System.IO", "IOException", msg);
314 mono_get_exception_file_not_found (MonoString *fname)
316 return mono_exception_from_name_two_strings (
317 mono_defaults.corlib, "System.IO", "FileNotFoundException", fname, fname);
321 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
329 klass = mono_class_from_name (mono_defaults.corlib, "System", "TypeInitializationException");
332 mono_class_init (klass);
334 /* TypeInitializationException only has 1 ctor with 2 args */
335 for (i = 0; i < klass->method.count; ++i) {
336 method = klass->methods [i];
337 if (!strcmp (".ctor", method->name) && method->signature->param_count == 2)
344 args [0] = mono_string_new (mono_domain_get (), type_name);
347 exc = mono_object_new (mono_domain_get (), klass);
348 mono_runtime_invoke (method, exc, args, NULL);
350 return (MonoException *) exc;
354 mono_get_exception_synchronization_lock (const guchar *msg)
356 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Threading", "SynchronizationLockException", msg);
360 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
362 return mono_exception_from_name_msg (mono_defaults.corlib, "System", "CannotUnloadAppDomainException", msg);
366 mono_get_exception_appdomain_unloaded (void)
368 return mono_exception_from_name (mono_defaults.corlib, "System", "AppDomainUnloadedException");
372 mono_get_exception_bad_image_format (const guchar *msg)
374 return mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", msg);
378 mono_get_exception_stack_overflow (void)
380 return mono_exception_from_name (mono_defaults.corlib, "System", "StackOverflowException");