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/object-internals.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;
81 klass = mono_class_from_name (image, name_space, name);
82 o = mono_object_new (domain, klass);
85 while ((m = mono_class_get_methods (klass, &iter))) {
86 MonoMethodSignature *sig;
88 if (strcmp (".ctor", mono_method_get_name (m)))
90 sig = mono_method_signature (m);
91 if (sig->param_count != count)
94 if (sig->params [0]->type != MONO_TYPE_STRING)
96 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
103 mono_runtime_invoke (method, o, args, NULL);
104 return (MonoException *) o;
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
112 * @msg: the message to embed inside the exception
114 * Creates an exception and initializes its message field.
116 * Returns: the initialized exception instance.
119 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
120 const char *name, const guchar *msg)
124 ex = mono_exception_from_name (image, name_space, name);
127 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
133 mono_get_exception_divide_by_zero ()
135 return mono_exception_from_name (mono_get_corlib (), "System",
136 "DivideByZeroException");
140 mono_get_exception_security ()
142 return mono_exception_from_name (mono_get_corlib (), "System.Security",
143 "SecurityException");
147 mono_get_exception_thread_abort ()
149 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
150 "ThreadAbortException");
154 mono_get_exception_arithmetic ()
156 return mono_exception_from_name (mono_get_corlib (), "System",
157 "ArithmeticException");
161 mono_get_exception_overflow ()
163 return mono_exception_from_name (mono_get_corlib (), "System",
164 "OverflowException");
168 mono_get_exception_null_reference ()
170 return mono_exception_from_name (mono_get_corlib (), "System",
171 "NullReferenceException");
175 mono_get_exception_execution_engine (const guchar *msg)
177 return mono_exception_from_name_msg (mono_get_corlib (), "System",
178 "ExecutionEngineException", msg);
182 mono_get_exception_serialization (const guchar *msg)
184 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization",
185 "SerializationException", msg);
189 mono_get_exception_invalid_cast ()
191 return mono_exception_from_name (mono_get_corlib (), "System",
192 "InvalidCastException");
196 mono_get_exception_index_out_of_range ()
198 return mono_exception_from_name (mono_get_corlib (), "System",
199 "IndexOutOfRangeException");
203 mono_get_exception_array_type_mismatch ()
205 return mono_exception_from_name (mono_get_corlib (), "System",
206 "ArrayTypeMismatchException");
210 mono_get_exception_type_load (MonoString *type_name)
212 MonoTypeLoadException *exc;
214 exc = (MonoTypeLoadException *) mono_exception_from_name (mono_get_corlib (),
216 "TypeLoadException");
218 exc->type_name = type_name;
219 return (MonoException *) exc;
223 mono_get_exception_not_implemented (const guchar *msg)
227 ex = mono_exception_from_name (mono_get_corlib (), "System",
228 "NotImplementedException");
231 ex->message = mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg);
237 mono_get_exception_missing_method ()
239 return mono_exception_from_name (mono_get_corlib (), "System",
240 "MissingMethodException");
244 mono_get_exception_argument_null (const guchar *arg)
248 ex = mono_exception_from_name (
249 mono_get_corlib (), "System", "ArgumentNullException");
252 ((MonoArgumentException *)ex)->param_name =
253 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
259 mono_get_exception_argument (const guchar *arg, const guchar *msg)
263 ex = mono_exception_from_name_msg (
264 mono_get_corlib (), "System", "ArgumentException", msg);
267 ((MonoArgumentException *)ex)->param_name =
268 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
274 mono_get_exception_argument_out_of_range (const guchar *arg)
278 ex = mono_exception_from_name (
279 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
282 ((MonoArgumentException *)ex)->param_name =
283 mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg);
289 mono_get_exception_thread_state (const guchar *msg)
291 return mono_exception_from_name_msg (
292 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
296 mono_get_exception_io (const guchar *msg)
298 return mono_exception_from_name_msg (
299 mono_get_corlib (), "System.IO", "IOException", msg);
303 mono_get_exception_file_not_found (MonoString *fname)
305 return mono_exception_from_name_two_strings (
306 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
310 mono_get_exception_file_not_found2 (const guchar *msg, MonoString *fname)
312 MonoString *s = mono_string_new (mono_domain_get (), msg);
314 return mono_exception_from_name_two_strings (
315 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
319 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
327 klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
330 mono_class_init (klass);
332 /* TypeInitializationException only has 1 ctor with 2 args */
334 while ((method = mono_class_get_methods (klass, &iter))) {
335 if (!strcmp (".ctor", mono_method_get_name (method)) && mono_method_signature (method)->param_count == 2)
342 args [0] = mono_string_new (mono_domain_get (), type_name);
345 exc = mono_object_new (mono_domain_get (), klass);
346 mono_runtime_invoke (method, exc, args, NULL);
348 return (MonoException *) exc;
352 mono_get_exception_synchronization_lock (const guchar *msg)
354 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
358 mono_get_exception_cannot_unload_appdomain (const guchar *msg)
360 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
364 mono_get_exception_appdomain_unloaded (void)
366 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
370 mono_get_exception_bad_image_format (const guchar *msg)
372 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
376 mono_get_exception_stack_overflow (void)
378 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");