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 MonoDomain *domain = mono_domain_get ();
36 klass = mono_class_from_name (image, name_space, name);
38 o = mono_object_new (domain, klass);
41 mono_runtime_object_init (o);
43 return (MonoException *)o;
47 * mono_exception_from_name_two_strings:
48 * @image: the Mono image where to look for the class
49 * @name_space: the namespace for the class
51 * @a1: first string argument to pass
52 * @a2: second string argument to pass
54 * Creates an exception from a constructor that takes two string
57 * Returns: the initialized exception instance.
59 static MonoException *
60 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
61 const char *name, MonoString *a1, MonoString *a2)
63 MonoDomain *domain = mono_domain_get ();
65 MonoMethod *method = NULL;
70 klass = mono_class_from_name (image, name_space, name);
71 o = mono_object_new (domain, klass);
73 for (i = 0; i < klass->method.count; ++i) {
74 MonoMethodSignature *sig;
76 if (strcmp (".ctor", klass->methods [i]->name))
78 sig = klass->methods [i]->signature;
79 if (sig->param_count != 2)
82 if (sig->params [0]->type != MONO_TYPE_STRING ||
83 sig->params [1]->type != MONO_TYPE_STRING)
85 method = klass->methods [i];
90 mono_runtime_invoke (method, o, args, NULL);
91 return (MonoException *) o;
95 * mono_exception_from_name_msg:
96 * @image: the Mono image where to look for the class
97 * @name_space: the namespace for the class
99 * @msg: the message to embed inside the exception
101 * Creates an exception and initializes its message field.
103 * Returns: the initialized exception instance.
105 static MonoException *
106 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
107 const char *name, const guchar *msg)
112 ex = mono_exception_from_name (image, name_space, name);
114 domain = ((MonoObject *)ex)->vtable->domain;
117 ex->message = mono_string_new (domain, msg);
123 mono_get_exception_divide_by_zero ()
125 return mono_exception_from_name (mono_defaults.corlib, "System",
126 "DivideByZeroException");
130 mono_get_exception_security ()
132 return mono_exception_from_name (mono_defaults.corlib, "System",
133 "SecurityException");
137 mono_get_exception_thread_abort ()
139 return mono_exception_from_name (mono_defaults.corlib, "System.Threading",
140 "ThreadAbortException");
144 mono_get_exception_arithmetic ()
146 return mono_exception_from_name (mono_defaults.corlib, "System",
147 "ArithmeticException");
151 mono_get_exception_overflow ()
153 return mono_exception_from_name (mono_defaults.corlib, "System",
154 "OverflowException");
158 mono_get_exception_null_reference ()
160 return mono_exception_from_name (mono_defaults.corlib, "System",
161 "NullReferenceException");
165 mono_get_exception_execution_engine (const guchar *msg)
167 return mono_exception_from_name_msg (mono_defaults.corlib, "System",
168 "ExecutionEngineException", msg);
172 mono_get_exception_serialization (const guchar *msg)
174 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Runtime.Serialization",
175 "SerializationException", msg);
179 mono_get_exception_invalid_cast ()
181 return mono_exception_from_name (mono_defaults.corlib, "System",
182 "InvalidCastException");
186 mono_get_exception_index_out_of_range ()
188 return mono_exception_from_name (mono_defaults.corlib, "System",
189 "IndexOutOfRangeException");
193 mono_get_exception_array_type_mismatch ()
195 return mono_exception_from_name (mono_defaults.corlib, "System",
196 "ArrayTypeMismatchException");
200 mono_get_exception_type_load (MonoString *type_name)
202 MonoTypeLoadException *exc;
204 exc = (MonoTypeLoadException *) mono_exception_from_name (mono_defaults.corlib,
206 "TypeLoadException");
208 exc->type_name = type_name;
209 return (MonoException *) exc;
213 mono_get_exception_not_implemented ()
215 return mono_exception_from_name (mono_defaults.corlib, "System",
216 "NotImplementedException");
220 mono_get_exception_missing_method ()
222 return mono_exception_from_name (mono_defaults.corlib, "System",
223 "MissingMethodException");
227 mono_get_exception_argument_null (const guchar *arg)
232 ex = mono_exception_from_name (
233 mono_defaults.corlib, "System", "ArgumentNullException");
235 domain = ((MonoObject *)ex)->vtable->domain;
238 ((MonoArgumentException *)ex)->param_name =
239 mono_string_new (domain, arg);
245 mono_get_exception_argument (const guchar *arg, const guchar *msg)
250 ex = mono_exception_from_name_msg (
251 mono_defaults.corlib, "System", "ArgumentException", msg);
253 domain = ((MonoObject *)ex)->vtable->domain;
256 ((MonoArgumentException *)ex)->param_name =
257 mono_string_new (domain, arg);
263 mono_get_exception_argument_out_of_range (const guchar *arg)
268 ex = mono_exception_from_name (
269 mono_defaults.corlib, "System", "ArgumentOutOfRangeException");
271 domain = ((MonoObject *)ex)->vtable->domain;
274 ((MonoArgumentException *)ex)->param_name =
275 mono_string_new (domain, arg);
281 mono_get_exception_thread_state (const guchar *msg)
283 return mono_exception_from_name_msg (mono_defaults.corlib,
284 "System.Threading", "ThreadStateException",
289 mono_get_exception_io (const guchar *msg)
291 return mono_exception_from_name_msg (
292 mono_defaults.corlib, "System.IO", "IOException", msg);
296 mono_get_exception_file_not_found (MonoString *fname)
298 return mono_exception_from_name_two_strings (
299 mono_defaults.corlib, "System.IO", "FileNotFoundException", fname, fname);
303 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
311 klass = mono_class_from_name (mono_defaults.corlib, "System", "TypeInitializationException");
314 mono_class_init (klass);
316 /* TypeInitializationException only has 1 ctor with 2 args */
317 for (i = 0; i < klass->method.count; ++i) {
318 method = klass->methods [i];
319 if (!strcmp (".ctor", method->name) && method->signature->param_count == 2)
326 args [0] = mono_string_new (mono_domain_get (), type_name);
329 exc = mono_object_new (mono_domain_get (), klass);
330 mono_runtime_invoke (method, exc, args, NULL);
332 return (MonoException *) exc;
336 mono_get_exception_synchronization_lock (const guchar *msg)
338 return mono_exception_from_name_msg (mono_defaults.corlib, "System.Threading", "SynchronizationLockException", msg);
342 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
344 return mono_exception_from_name_msg (mono_defaults.corlib, "System", "CannotUnloadAppDomainException", msg);
348 mono_get_exception_appdomain_unloaded (void)
350 return mono_exception_from_name (mono_defaults.corlib, "System", "AppDomainUnloadedException");