Merge pull request #2698 from esdrubal/iosxmlarray
[mono.git] / mono / metadata / exception.c
index d5744d6af33c8222d2d4f1a270a18096112508ac..e2c45aca38bbc615c7547112df6253a7475560bb 100644 (file)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
+#include <glib.h>
 #include <mono/metadata/exception.h>
+#include <mono/metadata/exception-internals.h>
+
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/appdomain.h>
@@ -57,18 +60,21 @@ MonoException *
 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
                                 const char* name_space, const char *name)
 {
+       MonoError error;
        MonoClass *klass;
        MonoObject *o;
        MonoDomain *caller_domain = mono_domain_get ();
 
-       klass = mono_class_from_name (image, name_space, name);
+       klass = mono_class_load_from_name (image, name_space, name);
 
-       o = mono_object_new (domain, klass);
-       g_assert (o != NULL);
+       o = mono_object_new_checked (domain, klass, &error);
+       mono_error_assert_ok (&error);
 
        if (domain != caller_domain)
                mono_domain_set_internal (domain);
-       mono_runtime_object_init (o);
+       mono_runtime_object_init_checked (o, &error);
+       mono_error_assert_ok (&error);
+
        if (domain != caller_domain)
                mono_domain_set_internal (caller_domain);
 
@@ -93,18 +99,19 @@ mono_exception_from_token (MonoImage *image, guint32 token)
        MonoObject *o;
 
        klass = mono_class_get_checked (image, token, &error);
-       g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
+       mono_error_assert_ok (&error);
 
-       o = mono_object_new (mono_domain_get (), klass);
-       g_assert (o != NULL);
+       o = mono_object_new_checked (mono_domain_get (), klass, &error);
+       mono_error_assert_ok (&error);
        
-       mono_runtime_object_init (o);
+       mono_runtime_object_init_checked (o, &error);
+       mono_error_assert_ok (&error);
 
        return (MonoException *)o;
 }
 
 static MonoException *
-create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
+create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoMethod *method = NULL;
@@ -117,7 +124,8 @@ create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
        if (a2 != NULL)
                count++;
        
-       o = mono_object_new (domain, klass);
+       o = mono_object_new_checked (domain, klass, error);
+       mono_error_assert_ok (error);
 
        iter = NULL;
        while ((m = mono_class_get_methods (klass, &iter))) {
@@ -139,7 +147,10 @@ create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
 
        args [0] = a1;
        args [1] = a2;
-       mono_runtime_invoke (method, o, args, NULL);
+
+       mono_runtime_invoke_checked (method, o, args, error);
+       return_val_if_nok (error, NULL);
+
        return (MonoException *) o;
 }
 
@@ -160,9 +171,16 @@ MonoException *
 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
                                      const char *name, MonoString *a1, MonoString *a2)
 {
-       MonoClass *klass = mono_class_from_name (image, name_space, name);
+       MonoError error;
+       MonoClass *klass;
+       MonoException *ret;
+
+       klass = mono_class_load_from_name (image, name_space, name);
 
-       return create_exception_two_strings (klass, a1, a2);
+       ret = create_exception_two_strings (klass, a1, a2, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+       return ret;
 }
 
 /**
@@ -201,16 +219,22 @@ mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
                                                                           MonoString *a1, MonoString *a2)
 {
        MonoError error;
-       MonoClass *klass = mono_class_get_checked (image, token, &error);
-       g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
+       MonoClass *klass;
+       MonoException *ret;
 
-       return create_exception_two_strings (klass, a1, a2);
+       klass = mono_class_get_checked (image, token, &error);
+       mono_error_assert_ok (&error); /* FIXME handle the error. */
+
+       ret = create_exception_two_strings (klass, a1, a2, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+       return ret;
 }
 
 /**
  * mono_get_exception_divide_by_zero:
  *
- * Returns: a new instance of the System.DivideByZeroException
+ * Returns: a new instance of the `System.DivideByZeroException`
  */
 MonoException *
 mono_get_exception_divide_by_zero ()
@@ -222,7 +246,7 @@ mono_get_exception_divide_by_zero ()
 /**
  * mono_get_exception_security:
  *
- * Returns: a new instance of the System.Security.SecurityException
+ * Returns: a new instance of the `System.Security.SecurityException`
  */
 MonoException *
 mono_get_exception_security ()
@@ -234,7 +258,7 @@ mono_get_exception_security ()
 /**
  * mono_get_exception_thread_abort:
  *
- * Returns: a new instance of the System.Threading.ThreadAbortException.
+ * Returns: a new instance of the `System.Threading.ThreadAbortException`
  */
 MonoException *
 mono_get_exception_thread_abort ()
@@ -246,7 +270,7 @@ mono_get_exception_thread_abort ()
 /**
  * mono_get_exception_thread_interrupted:
  *
- * Returns: a new instance of the System.Threading.ThreadInterruptedException.
+ * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
  */
 MonoException *
 mono_get_exception_thread_interrupted ()
@@ -258,7 +282,7 @@ mono_get_exception_thread_interrupted ()
 /**
  * mono_get_exception_arithmetic:
  *
- * Returns: a new instance of the System.ArithmeticException.
+ * Returns: a new instance of the `System.ArithmeticException`
  */
 MonoException *
 mono_get_exception_arithmetic ()
@@ -270,7 +294,7 @@ mono_get_exception_arithmetic ()
 /**
  * mono_get_exception_overflow:
  *
- * Returns: a new instance of the System.OverflowException
+ * Returns: a new instance of the `System.OverflowException`
  */
 MonoException *
 mono_get_exception_overflow ()
@@ -282,7 +306,7 @@ mono_get_exception_overflow ()
 /**
  * mono_get_exception_null_reference:
  *
- * Returns: a new instance of the System.NullReferenceException
+ * Returns: a new instance of the `System.NullReferenceException`
  */
 MonoException *
 mono_get_exception_null_reference ()
@@ -295,7 +319,7 @@ mono_get_exception_null_reference ()
  * mono_get_exception_execution_engine:
  * @msg: the message to pass to the user
  *
- * Returns: a new instance of the System.ExecutionEngineException
+ * Returns: a new instance of the `System.ExecutionEngineException`
  */
 MonoException *
 mono_get_exception_execution_engine (const char *msg)
@@ -307,7 +331,7 @@ mono_get_exception_execution_engine (const char *msg)
  * mono_get_exception_serialization:
  * @msg: the message to pass to the user
  *
- * Returns: a new instance of the System.Runtime.Serialization.SerializationException
+ * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
  */
 MonoException *
 mono_get_exception_serialization (const char *msg)
@@ -318,7 +342,7 @@ mono_get_exception_serialization (const char *msg)
 /**
  * mono_get_exception_invalid_cast:
  *
- * Returns: a new instance of the System.InvalidCastException
+ * Returns: a new instance of the `System.InvalidCastException`
  */
 MonoException *
 mono_get_exception_invalid_cast ()
@@ -330,7 +354,7 @@ mono_get_exception_invalid_cast ()
  * mono_get_exception_invalid_operation:
  * @msg: the message to pass to the user
  *
- * Returns: a new instance of the System.InvalidOperationException
+ * Returns: a new instance of the `System.InvalidOperationException`
  */
 MonoException *
 mono_get_exception_invalid_operation (const char *msg)
@@ -342,7 +366,7 @@ mono_get_exception_invalid_operation (const char *msg)
 /**
  * mono_get_exception_index_out_of_range:
  *
- * Returns: a new instance of the System.IndexOutOfRangeException
+ * Returns: a new instance of the `System.IndexOutOfRangeException`
  */
 MonoException *
 mono_get_exception_index_out_of_range ()
@@ -354,7 +378,7 @@ mono_get_exception_index_out_of_range ()
 /**
  * mono_get_exception_array_type_mismatch:
  *
- * Returns: a new instance of the System.ArrayTypeMismatchException
+ * Returns: a new instance of the `System.ArrayTypeMismatchException`
  */
 MonoException *
 mono_get_exception_array_type_mismatch ()
@@ -368,7 +392,7 @@ mono_get_exception_array_type_mismatch ()
  * @class_name: the name of the class that could not be loaded
  * @assembly_name: the assembly where the class was looked up.
  *
- * Returns: a new instance of the System.TypeLoadException.
+ * Returns: a new instance of the `System.TypeLoadException`
  */
 MonoException *
 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
@@ -383,7 +407,7 @@ mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
  * mono_get_exception_not_implemented:
  * @msg: the message to pass to the user
  *
- * Returns: a new instance of the System.NotImplementedException
+ * Returns: a new instance of the `System.NotImplementedException`
  */
 MonoException *
 mono_get_exception_not_implemented (const char *msg)
@@ -395,7 +419,7 @@ mono_get_exception_not_implemented (const char *msg)
  * mono_get_exception_not_supported:
  * @msg: the message to pass to the user
  *
- * Returns: a new instance of the System.NotSupportedException
+ * Returns: a new instance of the `System.NotSupportedException`
  */
 MonoException *
 mono_get_exception_not_supported (const char *msg)
@@ -408,7 +432,7 @@ mono_get_exception_not_supported (const char *msg)
  * @class_name: the class where the lookup was performed.
  * @member_name: the name of the missing method.
  *
- * Returns: a new instance of the System.MissingMethodException
+ * Returns: a new instance of the `System.MissingMethodException`
  */
 MonoException *
 mono_get_exception_missing_method (const char *class_name, const char *member_name)
@@ -425,7 +449,7 @@ mono_get_exception_missing_method (const char *class_name, const char *member_na
  * @class_name: the class where the lookup was performed
  * @member_name: the name of the missing method.
  *
- * Returns: a new instance of the System.MissingFieldException
+ * Returns: a new instance of the `System.MissingFieldException`
  */
 MonoException *
 mono_get_exception_missing_field (const char *class_name, const char *member_name)
@@ -441,7 +465,7 @@ mono_get_exception_missing_field (const char *class_name, const char *member_nam
  * mono_get_exception_argument_null:
  * @arg: the name of the argument that is null
  *
- * Returns: a new instance of the System.ArgumentNullException
+ * Returns: a new instance of the `System.ArgumentNullException`
  */
 MonoException*
 mono_get_exception_argument_null (const char *arg)
@@ -463,7 +487,7 @@ mono_get_exception_argument_null (const char *arg)
  * mono_get_exception_argument:
  * @arg: the name of the invalid argument.
  *
- * Returns: a new instance of the System.ArgumentException
+ * Returns: a new instance of the `System.ArgumentException`
  */
 MonoException *
 mono_get_exception_argument (const char *arg, const char *msg)
@@ -485,7 +509,7 @@ mono_get_exception_argument (const char *arg, const char *msg)
  * mono_get_exception_argument_out_of_range:
  * @arg: the name of the out of range argument.
  *
- * Returns: a new instance of the System.ArgumentOutOfRangeException
+ * Returns: a new instance of the `System.ArgumentOutOfRangeException`
  */
 MonoException *
 mono_get_exception_argument_out_of_range (const char *arg)
@@ -507,7 +531,7 @@ mono_get_exception_argument_out_of_range (const char *arg)
  * mono_get_exception_thread_state:
  * @msg: the message to present to the user
  *
- * Returns: a new instance of the System.Threading.ThreadStateException
+ * Returns: a new instance of the `System.Threading.ThreadStateException`
  */
 MonoException *
 mono_get_exception_thread_state (const char *msg)
@@ -520,7 +544,7 @@ mono_get_exception_thread_state (const char *msg)
  * mono_get_exception_io:
  * @msg: the message to present to the user
  *
- * Returns: a new instance of the System.IO.IOException
+ * Returns: a new instance of the `System.IO.IOException`
  */
 MonoException *
 mono_get_exception_io (const char *msg)
@@ -533,7 +557,7 @@ mono_get_exception_io (const char *msg)
  * mono_get_exception_file_not_found:
  * @fname: the name of the file not found.
  *
- * Returns: a new instance of the System.IO.FileNotFoundException
+ * Returns: a new instance of the `System.IO.FileNotFoundException`
  */
 MonoException *
 mono_get_exception_file_not_found (MonoString *fname)
@@ -547,7 +571,7 @@ mono_get_exception_file_not_found (MonoString *fname)
  * @msg: an informative message for the user.
  * @fname: the name of the file not found.
  *
- * Returns: a new instance of the System.IO.FileNotFoundException
+ * Returns: a new instance of the `System.IO.FileNotFoundException`
  */
 MonoException *
 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
@@ -563,10 +587,23 @@ mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
  * @type_name: the name of the type that failed to initialize.
  * @inner: the inner exception.
  *
- * Returns: a new instance of the System.TypeInitializationException
+ * Returns: a new instance of the `System.TypeInitializationException`
  */
 MonoException *
 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
+{
+       MonoError error;
+       MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
+       if (!is_ok (&error)) {
+               mono_error_cleanup (&error);
+               return NULL;
+       }
+
+       return ret;
+}
+
+MonoException *
+mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
 {
        MonoClass *klass;
        gpointer args [2];
@@ -574,8 +611,7 @@ mono_get_exception_type_initialization (const gchar *type_name, MonoException *i
        MonoMethod *method;
        gpointer iter;
 
-       klass = mono_class_from_name (mono_get_corlib (), "System", "TypeInitializationException");
-       g_assert (klass);
+       klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
 
        mono_class_init (klass);
 
@@ -594,8 +630,11 @@ mono_get_exception_type_initialization (const gchar *type_name, MonoException *i
        args [0] = mono_string_new (mono_domain_get (), type_name);
        args [1] = inner;
 
-       exc = mono_object_new (mono_domain_get (), klass);
-       mono_runtime_invoke (method, exc, args, NULL);
+       exc = mono_object_new_checked (mono_domain_get (), klass, error);
+       mono_error_assert_ok (error);
+
+       mono_runtime_invoke_checked (method, exc, args, error);
+       return_val_if_nok (error, NULL);
 
        return (MonoException *) exc;
 }
@@ -604,7 +643,7 @@ mono_get_exception_type_initialization (const gchar *type_name, MonoException *i
  * mono_get_exception_synchronization_lock:
  * @inner: the inner exception.
  *
- * Returns: a new instance of the System.SynchronizationLockException
+ * Returns: a new instance of the `System.SynchronizationLockException`
  */
 MonoException *
 mono_get_exception_synchronization_lock (const char *msg)
@@ -616,7 +655,7 @@ mono_get_exception_synchronization_lock (const char *msg)
  * mono_get_exception_cannot_unload_appdomain:
  * @inner: the inner exception.
  *
- * Returns: a new instance of the System.CannotUnloadAppDomainException
+ * Returns: a new instance of the `System.CannotUnloadAppDomainException`
  */
 MonoException *
 mono_get_exception_cannot_unload_appdomain (const char *msg)
@@ -627,7 +666,7 @@ mono_get_exception_cannot_unload_appdomain (const char *msg)
 /**
  * mono_get_exception_appdomain_unloaded
  *
- * Returns: a new instance of the System.AppDomainUnloadedException
+ * Returns: a new instance of the `System.AppDomainUnloadedException`
  */
 MonoException *
 mono_get_exception_appdomain_unloaded (void)
@@ -639,7 +678,7 @@ mono_get_exception_appdomain_unloaded (void)
  * mono_get_exception_bad_image_format:
  * @msg: an informative message for the user.
  *
- * Returns: a new instance of the System.BadImageFormatException
+ * Returns: a new instance of the `System.BadImageFormatException`
  */
 MonoException *
 mono_get_exception_bad_image_format (const char *msg)
@@ -652,7 +691,7 @@ mono_get_exception_bad_image_format (const char *msg)
  * @msg: an informative message for the user.
  * @fname: The full name of the file with the invalid image.
  *
- * Returns: a new instance of the System.BadImageFormatException
+ * Returns: a new instance of the `System.BadImageFormatException`
  */
 MonoException *
 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
@@ -666,7 +705,7 @@ mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
 /**
  * mono_get_exception_stack_overflow:
  *
- * Returns: a new instance of the System.StackOverflowException
+ * Returns: a new instance of the `System.StackOverflowException`
  */
 MonoException *
 mono_get_exception_stack_overflow (void)
@@ -677,7 +716,7 @@ mono_get_exception_stack_overflow (void)
 /**
  * mono_get_exception_out_of_memory:
  *
- * Returns: a new instance of the System.OutOfMemoryException
+ * Returns: a new instance of the `System.OutOfMemoryException`
  */
 MonoException *
 mono_get_exception_out_of_memory (void)
@@ -688,7 +727,7 @@ mono_get_exception_out_of_memory (void)
 /**
  * mono_get_exception_field_access:
  *
- * Returns: a new instance of the System.FieldAccessException
+ * Returns: a new instance of the `System.FieldAccessException`
  */
 MonoException *
 mono_get_exception_field_access (void)
@@ -700,7 +739,7 @@ mono_get_exception_field_access (void)
  * mono_get_exception_field_access2:
  * @msg: an informative message for the user.
  *
- * Returns: a new instance of the System.FieldAccessException
+ * Returns: a new instance of the `System.FieldAccessException`
  */
 MonoException *
 mono_get_exception_field_access_msg (const char *msg)
@@ -711,7 +750,7 @@ mono_get_exception_field_access_msg (const char *msg)
 /**
  * mono_get_exception_method_access:
  *
- * Returns: a new instance of the System.MethodAccessException
+ * Returns: a new instance of the `System.MethodAccessException`
  */
 MonoException *
 mono_get_exception_method_access (void)
@@ -723,7 +762,7 @@ mono_get_exception_method_access (void)
  * mono_get_exception_method_access2:
  * @msg: an informative message for the user.
  *
- * Returns: a new instance of the System.MethodAccessException
+ * Returns: a new instance of the `System.MethodAccessException`
  */
 MonoException *
 mono_get_exception_method_access_msg (const char *msg)
@@ -736,10 +775,23 @@ mono_get_exception_method_access_msg (const char *msg)
  * @types: an array of types that were defined in the moduled loaded.
  * @exceptions: an array of exceptions that were thrown during the type loading.
  *
- * Returns: a new instance of the System.Reflection.ReflectionTypeLoadException
+ * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
  */
 MonoException *
 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
+{
+       MonoError error;
+       MonoException *ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
+       if (is_ok (&error)) {
+               mono_error_cleanup (&error);
+               return NULL;
+       }
+
+       return ret;
+}
+
+MonoException *
+mono_get_exception_reflection_type_load_checked (MonoArray *types, MonoArray *exceptions, MonoError *error)
 {
        MonoClass *klass;
        gpointer args [2];
@@ -747,8 +799,8 @@ mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions
        MonoMethod *method;
        gpointer iter;
 
-       klass = mono_class_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
-       g_assert (klass);
+       klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
+
        mono_class_init (klass);
 
        /* Find the Type[], Exception[] ctor */
@@ -767,14 +819,30 @@ mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions
        args [0] = types;
        args [1] = exceptions;
 
-       exc = mono_object_new (mono_domain_get (), klass);
-       mono_runtime_invoke (method, exc, args, NULL);
+       exc = mono_object_new_checked (mono_domain_get (), klass, error);
+       mono_error_assert_ok (error);
+
+       mono_runtime_invoke_checked (method, exc, args, error);
+       return_val_if_nok (error, NULL);
 
        return (MonoException *) exc;
 }
 
 MonoException *
 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
+{
+       MonoError error;
+       MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
+       if (!is_ok (&error)) {
+               mono_error_cleanup (&error);
+               return NULL;
+       }
+
+       return ret;
+}
+
+MonoException *
+mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
 {
        MonoClass *klass;
        MonoObject *o;
@@ -782,17 +850,19 @@ mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
        MonoDomain *domain = mono_domain_get ();
        gpointer params [16];
 
-       klass = mono_class_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
-       g_assert (klass);
+       klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
 
-       o = mono_object_new (domain, klass);
+       o = mono_object_new_checked (domain, klass, error);
+       mono_error_assert_ok (error);
        g_assert (o != NULL);
 
        method = mono_class_get_method_from_name (klass, ".ctor", 1);
        g_assert (method);
 
        params [0] = wrapped_exception;
-       mono_runtime_invoke (method, o, params, NULL);
+
+       mono_runtime_invoke_checked (method, o, params, error);
+       return_val_if_nok (error, NULL);
 
        return (MonoException *)o;
 }      
@@ -847,7 +917,7 @@ mono_exception_get_native_backtrace (MonoException *exc)
 
        for (i = 0; i < len; ++i) {
                gpointer ip = mono_array_get (arr, gpointer, i);
-               MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
+               MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
                if (ji) {
                        char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
                        g_string_append_printf (text, "%s\n", msg);
@@ -896,10 +966,27 @@ mono_error_raise_exception (MonoError *target_error)
                mono_raise_exception (ex);
 }
 
-void
+/**
+ * mono_error_set_pending_exception:
+ * @error: The error
+ *
+ *
+ * If @error is set, convert it to an exception and set the pending exception for the current icall.
+ * Returns TRUE if @error was set, or FALSE otherwise, so that you can write:
+ *    if (mono_error_set_pending_exception (error)) {
+ *      { ... cleanup code ... }
+ *      return;
+ *    }
+ */
+gboolean
 mono_error_set_pending_exception (MonoError *error)
 {
        MonoException *ex = mono_error_convert_to_exception (error);
-       if (ex)
+       if (ex) {
                mono_set_pending_exception (ex);
+               return TRUE;
+       } else {
+               return FALSE;
+       }
 }
+