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,
38 const char* name_space,
44 klass = mono_class_from_name (image, name_space, name);
46 o = mono_object_new (domain, klass);
49 mono_runtime_object_init (o);
51 return (MonoException *)o;
55 * mono_exception_from_name_two_strings:
56 * @image: the Mono image where to look for the class
57 * @name_space: the namespace for the class
59 * @a1: first string argument to pass
60 * @a2: second string argument to pass
62 * Creates an exception from a constructor that takes two string
65 * Returns: the initialized exception instance.
67 static MonoException *
68 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
69 const char *name, MonoString *a1, MonoString *a2)
71 MonoDomain *domain = mono_domain_get ();
73 MonoMethod *method = NULL;
78 klass = mono_class_from_name (image, name_space, name);
79 o = mono_object_new (domain, klass);
81 for (i = 0; i < klass->method.count; ++i) {
82 MonoMethodSignature *sig;
84 if (strcmp (".ctor", klass->methods [i]->name))
86 sig = klass->methods [i]->signature;
87 if (sig->param_count != 2)
90 if (sig->params [0]->type != MONO_TYPE_STRING ||
91 sig->params [1]->type != MONO_TYPE_STRING)
93 method = klass->methods [i];
98 mono_runtime_invoke (method, o, args, NULL);
99 return (MonoException *) o;
103 * mono_exception_from_name_msg:
104 * @image: the Mono image where to look for the class
105 * @name_space: the namespace for the class
107 * @msg: the message to embed inside the exception
109 * Creates an exception and initializes its message field.
111 * Returns: the initialized exception instance.
114 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
115 const char *name, const guchar *msg)
120 ex = mono_exception_from_name (image, name_space, name);
122 domain = ((MonoObject *)ex)->vtable->domain;
125 ex->message = mono_string_new (domain, msg);
131 mono_get_exception_divide_by_zero ()
133 return mono_exception_from_name (mono_defaults.corlib, "System",
134 "DivideByZeroException");
138 mono_get_exception_security ()
140 return mono_exception_from_name (mono_defaults.corlib, "System",
141 "SecurityException");
145 mono_get_exception_thread_abort ()
147 return mono_exception_from_name (mono_defaults.corlib, "System.Threading",
148 "ThreadAbortException");
152 mono_get_exception_arithmetic ()
154 return mono_exception_from_name (mono_defaults.corlib, "System",
155 "ArithmeticException");
159 mono_get_exception_overflow ()
161 return mono_exception_from_name (mono_defaults.corlib, "System",
162 "OverflowException");
166 mono_get_exception_null_reference ()
168 return mono_exception_from_name (mono_defaults.corlib, "System",
169 "NullReferenceException");
173 mono_get_exception_execution_engine (const guchar *msg)
175 return mono_exception_from_name_msg (mono_defaults.corlib, "System",
176 "ExecutionEngineException", msg);
180 mono_get_exception_serialization (const guchar *msg)
182 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Runtime.Serialization",
183 "SerializationException", msg);
187 mono_get_exception_invalid_cast ()
189 return mono_exception_from_name (mono_defaults.corlib, "System",
190 "InvalidCastException");
194 mono_get_exception_index_out_of_range ()
196 return mono_exception_from_name (mono_defaults.corlib, "System",
197 "IndexOutOfRangeException");
201 mono_get_exception_array_type_mismatch ()
203 return mono_exception_from_name (mono_defaults.corlib, "System",
204 "ArrayTypeMismatchException");
208 mono_get_exception_type_load (MonoString *type_name)
210 MonoTypeLoadException *exc;
212 exc = (MonoTypeLoadException *) mono_exception_from_name (mono_defaults.corlib,
214 "TypeLoadException");
216 exc->type_name = type_name;
217 return (MonoException *) exc;
221 mono_get_exception_not_implemented (const guchar *msg)
226 ex = mono_exception_from_name (mono_defaults.corlib, "System",
227 "NotImplementedException");
228 domain = ((MonoObject *)ex)->vtable->domain;
231 ex->message = mono_string_new (domain, msg);
237 mono_get_exception_missing_method ()
239 return mono_exception_from_name (mono_defaults.corlib, "System",
240 "MissingMethodException");
244 mono_get_exception_argument_null (const guchar *arg)
249 ex = mono_exception_from_name (
250 mono_defaults.corlib, "System", "ArgumentNullException");
252 domain = ((MonoObject *)ex)->vtable->domain;
255 ((MonoArgumentException *)ex)->param_name =
256 mono_string_new (domain, arg);
262 mono_get_exception_argument (const guchar *arg, const guchar *msg)
267 ex = mono_exception_from_name_msg (
268 mono_defaults.corlib, "System", "ArgumentException", msg);
270 domain = ((MonoObject *)ex)->vtable->domain;
273 ((MonoArgumentException *)ex)->param_name =
274 mono_string_new (domain, arg);
280 mono_get_exception_argument_out_of_range (const guchar *arg)
285 ex = mono_exception_from_name (
286 mono_defaults.corlib, "System", "ArgumentOutOfRangeException");
288 domain = ((MonoObject *)ex)->vtable->domain;
291 ((MonoArgumentException *)ex)->param_name =
292 mono_string_new (domain, arg);
298 mono_get_exception_thread_state (const guchar *msg)
300 return mono_exception_from_name_msg (mono_defaults.corlib,
301 "System.Threading", "ThreadStateException",
306 mono_get_exception_io (const guchar *msg)
308 return mono_exception_from_name_msg (
309 mono_defaults.corlib, "System.IO", "IOException", msg);
313 mono_get_exception_file_not_found (MonoString *fname)
315 return mono_exception_from_name_two_strings (
316 mono_defaults.corlib, "System.IO", "FileNotFoundException", fname, fname);
320 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
328 klass = mono_class_from_name (mono_defaults.corlib, "System", "TypeInitializationException");
331 mono_class_init (klass);
333 /* TypeInitializationException only has 1 ctor with 2 args */
334 for (i = 0; i < klass->method.count; ++i) {
335 method = klass->methods [i];
336 if (!strcmp (".ctor", method->name) && method->signature->param_count == 2)
343 args [0] = mono_string_new (mono_domain_get (), type_name);
346 exc = mono_object_new (mono_domain_get (), klass);
347 mono_runtime_invoke (method, exc, args, NULL);
349 return (MonoException *) exc;
353 mono_get_exception_synchronization_lock (const guchar *msg)
355 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Threading", "SynchronizationLockException", msg);
359 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
361 return mono_exception_from_name_msg (mono_defaults.corlib, "System", "CannotUnloadAppDomainException", msg);
365 mono_get_exception_appdomain_unloaded (void)
367 return mono_exception_from_name (mono_defaults.corlib, "System", "AppDomainUnloadedException");
371 mono_get_exception_bad_image_format (const guchar *msg)
373 return mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", msg);