Merge pull request #2708 from lambdageek/dev/monoerror-assorted-get_type
[mono.git] / mono / metadata / icall.c
index c68df0247e35be0c3fa7ee3f24c9b1e4327af06e..60de5cb9f4a16f202e5f10d7cb730f5d157bde56 100644 (file)
@@ -45,6 +45,7 @@
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/exception.h>
+#include <mono/metadata/exception-internals.h>
 #include <mono/metadata/file-io.h>
 #include <mono/metadata/console-io.h>
 #include <mono/metadata/mono-route.h>
@@ -117,7 +118,7 @@ static GENERATE_GET_CLASS_WITH_CACHE (event_info, System.Reflection, EventInfo)
 static GENERATE_GET_CLASS_WITH_CACHE (module, System.Reflection, Module)
 
 static MonoArray*
-type_array_from_modifiers (MonoImage *image, MonoType *type, int optional);
+type_array_from_modifiers (MonoImage *image, MonoType *type, int optional, MonoError *error);
 
 static inline MonoBoolean
 is_generic_parameter (MonoType *type)
@@ -126,10 +127,12 @@ is_generic_parameter (MonoType *type)
 }
 
 static void
-mono_class_init_or_throw (MonoClass *klass)
+mono_class_init_checked (MonoClass *klass, MonoError *error)
 {
+       mono_error_init (error);
+
        if (!mono_class_init (klass))
-               mono_raise_exception (mono_class_get_exception_for_failure (klass));
+               mono_error_set_exception_instance (error, mono_class_get_exception_for_failure (klass));
 }
 
 ICALL_EXPORT MonoObject *
@@ -548,7 +551,8 @@ ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *
        }
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (bounds && (mono_array_length (bounds) == 1) && (mono_array_get (bounds, gint32, 0) != 0))
                /* vectors are not the same as one dimensional arrays with no-zero bounds */
@@ -598,7 +602,8 @@ ves_icall_System_Array_CreateInstanceImpl64 (MonoReflectionType *type, MonoArray
        }
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (bounds && (mono_array_length (bounds) == 1) && (mono_array_get (bounds, gint64, 0) != 0))
                /* vectors are not the same as one dimensional arrays with no-zero bounds */
@@ -1163,7 +1168,7 @@ ves_icall_System_Object_GetType (MonoObject *obj)
 }
 
 ICALL_EXPORT void
-mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
+ves_icall_MonoType_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
 {
        mtype->type = &obj->vtable->klass->byval_arg;
        g_assert (mtype->type->type);
@@ -1354,9 +1359,9 @@ type_from_parsed_name (MonoTypeNameParse *info, MonoBoolean ignoreCase, MonoErro
 }
 
 ICALL_EXPORT MonoReflectionType*
-ves_icall_type_from_name (MonoString *name,
-                         MonoBoolean throwOnError,
-                         MonoBoolean ignoreCase)
+ves_icall_System_Type_internal_from_name (MonoString *name,
+                                                                                 MonoBoolean throwOnError,
+                                                                                 MonoBoolean ignoreCase)
 {
        MonoError error;
        char *str = mono_string_to_utf8 (name);
@@ -1407,7 +1412,7 @@ ves_icall_type_from_name (MonoString *name,
 
 
 ICALL_EXPORT MonoReflectionType*
-ves_icall_type_from_handle (MonoType *handle)
+ves_icall_System_Type_internal_from_handle (MonoType *handle)
 {
        MonoError error;
        MonoReflectionType *ret;
@@ -1541,7 +1546,7 @@ mono_type_get_underlying_type_ignore_byref (MonoType *type)
 }
 
 ICALL_EXPORT guint32
-ves_icall_type_is_assignable_from (MonoReflectionType *type, MonoReflectionType *c)
+ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionType *type, MonoReflectionType *c)
 {
        MonoClass *klass;
        MonoClass *klassc;
@@ -1580,15 +1585,17 @@ ves_icall_type_is_assignable_from (MonoReflectionType *type, MonoReflectionType
 }
 
 ICALL_EXPORT guint32
-ves_icall_type_IsInstanceOfType (MonoReflectionType *type, MonoObject *obj)
+ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionType *type, MonoObject *obj)
 {
+       MonoError error;
        MonoClass *klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
        return mono_object_isinst (obj, klass) != NULL;
 }
 
 ICALL_EXPORT guint32
-ves_icall_get_attributes (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetAttributes (MonoReflectionType *type)
 {
        MonoClass *klass = mono_class_from_mono_type (type->type);
        return klass->flags;
@@ -1668,12 +1675,16 @@ ves_icall_System_Reflection_FieldInfo_GetTypeModifiers (MonoReflectionField *fie
 {
        MonoError error;
        MonoType *type = mono_field_get_type_checked (field->field, &error);
+       MonoArray *res;
+
        if (!mono_error_ok (&error)) {
                mono_error_set_pending_exception (&error);
                return NULL;
        }
 
-       return type_array_from_modifiers (field->field->parent->image, type, optional);
+       res = type_array_from_modifiers (field->field->parent->image, type, optional, &error);
+       mono_error_raise_exception (&error);
+       return res;
 }
 
 ICALL_EXPORT int
@@ -2060,7 +2071,7 @@ typedef enum {
 } PInfo;
 
 ICALL_EXPORT void
-ves_icall_get_property_info (const MonoReflectionProperty *property, MonoPropertyInfo *info, PInfo req_info)
+ves_icall_MonoPropertyInfo_get_property_info (const MonoReflectionProperty *property, MonoPropertyInfo *info, PInfo req_info)
 {
        MonoError error;
        MonoReflectionType *rt;
@@ -2118,7 +2129,7 @@ ves_icall_get_property_info (const MonoReflectionProperty *property, MonoPropert
 }
 
 ICALL_EXPORT void
-ves_icall_get_event_info (MonoReflectionMonoEvent *event, MonoEventInfo *info)
+ves_icall_MonoEventInfo_get_event_info (MonoReflectionMonoEvent *event, MonoEventInfo *info)
 {
        MonoError error;
        MonoReflectionType *rt;
@@ -2307,8 +2318,10 @@ ves_icall_Type_GetInterfaceMapData (MonoReflectionType *type, MonoReflectionType
        MonoDomain *domain;
        MonoError error;
 
-       mono_class_init_or_throw (klass);
-       mono_class_init_or_throw (iclass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
+       mono_class_init_checked (iclass, &error);
+       mono_error_raise_exception (&error);
 
        mono_class_setup_vtable (klass);
 
@@ -2336,8 +2349,11 @@ ves_icall_Type_GetInterfaceMapData (MonoReflectionType *type, MonoReflectionType
 ICALL_EXPORT void
 ves_icall_Type_GetPacking (MonoReflectionType *type, guint32 *packing, guint32 *size)
 {
+       MonoError error;
        MonoClass *klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (image_is_dynamic (klass->image)) {
                MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder*)type;
@@ -2349,7 +2365,7 @@ ves_icall_Type_GetPacking (MonoReflectionType *type, guint32 *packing, guint32 *
 }
 
 ICALL_EXPORT MonoReflectionType*
-ves_icall_MonoType_GetElementType (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetElementType (MonoReflectionType *type)
 {
        MonoError error;
        MonoReflectionType *ret;
@@ -2363,7 +2379,8 @@ ves_icall_MonoType_GetElementType (MonoReflectionType *type)
        }
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        // GetElementType should only return a type for:
        // Array Pointer PassedByRef
@@ -2382,7 +2399,7 @@ ves_icall_MonoType_GetElementType (MonoReflectionType *type)
 }
 
 ICALL_EXPORT MonoReflectionType*
-ves_icall_get_type_parent (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetBaseType (MonoReflectionType *type)
 {
        MonoError error;
        MonoReflectionType *ret;
@@ -2401,34 +2418,42 @@ ves_icall_get_type_parent (MonoReflectionType *type)
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_type_ispointer (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsPointer (MonoReflectionType *type)
 {
        return type->type->type == MONO_TYPE_PTR;
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_type_isprimitive (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsPrimitive (MonoReflectionType *type)
 {
        return (!type->type->byref && (((type->type->type >= MONO_TYPE_BOOLEAN) && (type->type->type <= MONO_TYPE_R8)) || (type->type->type == MONO_TYPE_I) || (type->type->type == MONO_TYPE_U)));
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_type_isbyref (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsByRef (MonoReflectionType *type)
 {
        return type->type->byref;
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_type_iscomobject (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsComObject (MonoReflectionType *type)
 {
+       MonoError error;
        MonoClass *klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        return mono_class_is_com_object (klass);
 }
 
+ICALL_EXPORT guint32
+ves_icall_RuntimeTypeHandle_GetMetadataToken (MonoReflectionType *obj)
+{
+       return mono_reflection_get_token ((MonoObject*)obj);
+}
+
 ICALL_EXPORT MonoReflectionModule*
-ves_icall_MonoType_get_Module (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetModule (MonoReflectionType *type)
 {
        MonoError error;
        MonoReflectionModule *result = NULL;
@@ -2440,7 +2465,7 @@ ves_icall_MonoType_get_Module (MonoReflectionType *type)
 }
 
 ICALL_EXPORT MonoReflectionAssembly*
-ves_icall_MonoType_get_Assembly (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionType *type)
 {
        MonoError error;
        MonoDomain *domain = mono_domain_get (); 
@@ -2514,7 +2539,7 @@ ves_icall_MonoType_get_Namespace (MonoReflectionType *type)
 }
 
 ICALL_EXPORT gint32
-ves_icall_MonoType_GetArrayRank (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetArrayRank (MonoReflectionType *type)
 {
        MonoClass *klass;
 
@@ -2575,7 +2600,7 @@ ves_icall_MonoType_GetGenericArguments (MonoReflectionType *type, MonoBoolean ru
 }
 
 ICALL_EXPORT gboolean
-ves_icall_Type_get_IsGenericTypeDefinition (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsGenericTypeDefinition (MonoReflectionType *type)
 {
        MonoClass *klass;
 
@@ -2590,7 +2615,7 @@ ves_icall_Type_get_IsGenericTypeDefinition (MonoReflectionType *type)
 }
 
 ICALL_EXPORT MonoReflectionType*
-ves_icall_Type_GetGenericTypeDefinition_impl (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetGenericTypeDefinition_impl (MonoReflectionType *type)
 {
        MonoError error;
        MonoReflectionType *ret;
@@ -2632,7 +2657,8 @@ ves_icall_Type_MakeGenericType (MonoReflectionType *type, MonoArray *type_array)
        int i, count;
 
        g_assert (IS_MONOTYPE (type));
-       mono_class_init_or_throw (mono_class_from_mono_type (type->type));
+       mono_class_init_checked (mono_class_from_mono_type (type->type), &error);
+       mono_error_raise_exception (&error);
 
        count = mono_array_length (type_array);
        types = g_new0 (MonoType *, count);
@@ -2662,7 +2688,7 @@ ves_icall_Type_MakeGenericType (MonoReflectionType *type, MonoArray *type_array)
 }
 
 ICALL_EXPORT gboolean
-ves_icall_Type_get_IsGenericType (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_HasInstantiation (MonoReflectionType *type)
 {
        MonoClass *klass;
 
@@ -2726,7 +2752,7 @@ ves_icall_Type_GetGenericParameterConstraints (MonoReflectionType *type)
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_MonoType_get_IsGenericParameter (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_IsGenericVariable (MonoReflectionType *type)
 {
        return is_generic_parameter (type->type);
 }
@@ -2758,7 +2784,8 @@ ves_icall_MonoType_GetCorrespondingInflatedMethod (MonoReflectionType *type,
        domain = ((MonoObject *)type)->vtable->domain;
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        iter = NULL;
        while ((method = mono_class_get_methods (klass, &iter))) {
@@ -3300,7 +3327,8 @@ ves_icall_System_Enum_ToObject (MonoReflectionType *enumType, guint64 value)
        domain = mono_object_domain (enumType); 
        enumc = mono_class_from_mono_type (enumType->type);
 
-       mono_class_init_or_throw (enumc);
+       mono_class_init_checked (enumc, &error);
+       mono_error_raise_exception (&error);
 
        etype = mono_class_enum_basetype (enumc);
 
@@ -3359,7 +3387,8 @@ ves_icall_System_Enum_get_underlying_type (MonoReflectionType *type)
        MonoClass *klass;
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        etype = mono_class_enum_basetype (klass);
        if (!etype) {
@@ -3461,6 +3490,7 @@ ves_icall_System_Enum_get_hashcode (MonoObject *eobj)
 ICALL_EXPORT MonoBoolean
 ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionType *type, MonoArray **values, MonoArray **names)
 {
+       MonoError error;
        MonoDomain *domain = mono_object_domain (type); 
        MonoClass *enumc = mono_class_from_mono_type (type->type);
        guint j = 0, nvalues;
@@ -3470,7 +3500,8 @@ ves_icall_System_Enum_GetEnumValuesAndNames (MonoReflectionType *type, MonoArray
        guint64 field_value, previous_value = 0;
        gboolean sorted = TRUE;
 
-       mono_class_init_or_throw (enumc);
+       mono_class_init_checked (enumc, &error);
+       mono_error_raise_exception (&error);
 
        if (!enumc->enumtype) {
                mono_set_pending_exception (mono_get_exception_argument ("enumType", "Type provided must be an Enum."));
@@ -5139,8 +5170,11 @@ ves_icall_System_MonoType_getFullName (MonoReflectionType *object, gboolean full
 ICALL_EXPORT int
 vell_icall_MonoType_get_core_clr_security_level (MonoReflectionType *rfield)
 {
+       MonoError error;
        MonoClass *klass = mono_class_from_mono_type (rfield->type);
-       mono_class_init_or_throw (klass);
+
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
        return mono_security_core_clr_class_level (klass);
 }
 
@@ -5543,7 +5577,11 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
                g_list_free (list);
                list = NULL;
 
-               exc = mono_get_exception_reflection_type_load (res, exl);
+               exc = mono_get_exception_reflection_type_load_checked (res, exl, &error);
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               }
                mono_loader_clear_error ();
                mono_set_pending_exception (exc);
                return NULL;
@@ -6063,7 +6101,7 @@ ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, Mon
 }
 
 ICALL_EXPORT MonoBoolean
-ves_icall_Type_IsArrayImpl (MonoReflectionType *t)
+ves_icall_RuntimeTypeHandle_IsArray (MonoReflectionType *t)
 {
        MonoType *type;
        MonoBoolean res;
@@ -6075,17 +6113,20 @@ ves_icall_Type_IsArrayImpl (MonoReflectionType *t)
 }
 
 static void
-check_for_invalid_type (MonoClass *klass)
+check_for_invalid_type (MonoClass *klass, MonoError *error)
 {
        char *name;
        MonoString *str;
+
+       mono_error_init (error);
+
        if (klass->byval_arg.type != MONO_TYPE_TYPEDBYREF)
                return;
 
        name = mono_type_get_full_name (klass);
        str =  mono_string_new (mono_domain_get (), name);
        g_free (name);
-       mono_raise_exception ((MonoException*)mono_get_exception_type_load (str, NULL));
+       mono_error_set_exception_instance (error, mono_get_exception_type_load (str, NULL));
 
 }
 ICALL_EXPORT MonoReflectionType *
@@ -6096,7 +6137,8 @@ ves_icall_Type_make_array_type (MonoReflectionType *type, int rank)
        MonoClass *klass, *aklass;
 
        klass = mono_class_from_mono_type (type->type);
-       check_for_invalid_type (klass);
+       check_for_invalid_type (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (rank == 0) //single dimentional array
                aklass = mono_array_class_get (klass, 1);
@@ -6117,8 +6159,10 @@ ves_icall_Type_make_byref_type (MonoReflectionType *type)
        MonoClass *klass;
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
-       check_for_invalid_type (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
+       check_for_invalid_type (klass, &error);
+       mono_error_raise_exception (&error);
 
        ret = mono_type_get_object_checked (mono_object_domain (type), &klass->this_arg, &error);
        mono_error_raise_exception (&error);
@@ -6134,8 +6178,10 @@ ves_icall_Type_MakePointerType (MonoReflectionType *type)
        MonoClass *klass, *pklass;
 
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
-       check_for_invalid_type (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
+       check_for_invalid_type (klass, &error);
+       mono_error_raise_exception (&error);
 
        pklass = mono_ptr_class_get (type->type);
 
@@ -6155,7 +6201,8 @@ ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, Mon
        gpointer func;
        MonoMethod *method = info->method;
 
-       mono_class_init_or_throw (delegate_class);
+       mono_class_init_checked (delegate_class, &error);
+       mono_error_raise_exception (&error);
 
        mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
 
@@ -6899,7 +6946,8 @@ ves_icall_Remoting_RemotingServices_GetVirtualMethod (
 
        method = rmethod->method;
        klass = mono_class_from_mono_type (rtype->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (MONO_CLASS_IS_INTERFACE (klass))
                return NULL;
@@ -6976,7 +7024,8 @@ ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClas
        
        domain = mono_object_domain (type);
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (MONO_CLASS_IS_INTERFACE (klass) || (klass->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
                mono_set_pending_exception (mono_get_exception_argument ("type", "Type cannot be instantiated"));
@@ -7043,7 +7092,7 @@ ves_icall_System_IO_DriveInfo_GetDriveType (MonoString *root_path_name)
 #endif
 
 ICALL_EXPORT gpointer
-ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
+ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method)
 {
        return mono_compile_method (method);
 }
@@ -7219,7 +7268,8 @@ ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionType *type)
        
        domain = mono_object_domain (type);
        klass = mono_class_from_mono_type (type->type);
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
        if (mono_class_is_nullable (klass))
                /* No arguments -> null */
@@ -7530,15 +7580,18 @@ mono_TypedReference_MakeTypedReferenceInternal (MonoObject *target, MonoArray *f
 }
 
 static void
-prelink_method (MonoMethod *method)
+prelink_method (MonoMethod *method, MonoError *error)
 {
        const char *exc_class, *exc_arg;
+
+       mono_error_init (error);
        if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                return;
        mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
        if (exc_class) {
-               mono_raise_exception( 
-                       mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg ) );
+               mono_error_set_exception_instance (error,
+                       mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
+               return;
        }
        /* create the wrapper, too? */
 }
@@ -7546,20 +7599,27 @@ prelink_method (MonoMethod *method)
 ICALL_EXPORT void
 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethod *method)
 {
-       prelink_method (method->method);
+       MonoError error;
+
+       prelink_method (method->method, &error);
+       mono_error_raise_exception (&error);
 }
 
 ICALL_EXPORT void
 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionType *type)
 {
+       MonoError error;
        MonoClass *klass = mono_class_from_mono_type (type->type);
        MonoMethod* m;
        gpointer iter = NULL;
 
-       mono_class_init_or_throw (klass);
+       mono_class_init_checked (klass, &error);
+       mono_error_raise_exception (&error);
 
-       while ((m = mono_class_get_methods (klass, &iter)))
-               prelink_method (m);
+       while ((m = mono_class_get_methods (klass, &iter))) {
+               prelink_method (m, &error);
+               mono_error_raise_exception (&error);
+       }
 }
 
 /* These parameters are "readonly" in corlib/System/NumberFormatter.cs */
@@ -7582,16 +7642,15 @@ ves_icall_System_NumberFormatter_GetFormatterTables (guint64 const **mantissas,
 /*
  * We return NULL for no modifiers so the corlib code can return Type.EmptyTypes
  * and avoid useless allocations.
- * 
- * MAY THROW
  */
 static MonoArray*
-type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
+type_array_from_modifiers (MonoImage *image, MonoType *type, int optional, MonoError *error)
 {
-       MonoError error;
        MonoReflectionType *rt;
        MonoArray *res;
        int i, count = 0;
+
+       mono_error_init (error);
        for (i = 0; i < type->num_mods; ++i) {
                if ((optional && !type->modifiers [i].required) || (!optional && type->modifiers [i].required))
                        count++;
@@ -7602,11 +7661,11 @@ type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
        count = 0;
        for (i = 0; i < type->num_mods; ++i) {
                if ((optional && !type->modifiers [i].required) || (!optional && type->modifiers [i].required)) {
-                       MonoClass *klass = mono_class_get_checked (image, type->modifiers [i].token, &error);
-                       mono_error_raise_exception (&error); /* this is safe, no cleanup needed on callers */ 
+                       MonoClass *klass = mono_class_get_checked (image, type->modifiers [i].token, error);
+                       return_val_if_nok (error, NULL);
 
-                       rt = mono_type_get_object_checked (mono_domain_get (), &klass->byval_arg, &error);
-                       mono_error_raise_exception (&error);
+                       rt = mono_type_get_object_checked (mono_domain_get (), &klass->byval_arg, error);
+                       return_val_if_nok (error, NULL);
 
                        mono_array_setref (res, count, rt);
                        count++;
@@ -7616,14 +7675,16 @@ type_array_from_modifiers (MonoImage *image, MonoType *type, int optional)
 }
 
 ICALL_EXPORT MonoArray*
-param_info_get_type_modifiers (MonoReflectionParameter *param, MonoBoolean optional)
+ves_icall_ParameterInfo_GetTypeModifiers (MonoReflectionParameter *param, MonoBoolean optional)
 {
+       MonoError error;
        MonoType *type = param->ClassImpl->type;
        MonoClass *member_class = mono_object_class (param->MemberImpl);
        MonoMethod *method = NULL;
        MonoImage *image;
        int pos;
        MonoMethodSignature *sig;
+       MonoArray *res;
 
        if (mono_class_is_reflection_method_or_constructor (member_class)) {
                MonoReflectionMethod *rmethod = (MonoReflectionMethod*)param->MemberImpl;
@@ -7651,7 +7712,9 @@ param_info_get_type_modifiers (MonoReflectionParameter *param, MonoBoolean optio
        else
                type = sig->params [pos];
 
-       return type_array_from_modifiers (image, type, optional);
+       res = type_array_from_modifiers (image, type, optional, &error);
+       mono_error_raise_exception (&error);
+       return res;
 }
 
 static MonoType*
@@ -7669,14 +7732,18 @@ get_property_type (MonoProperty *prop)
 }
 
 ICALL_EXPORT MonoArray*
-property_info_get_type_modifiers (MonoReflectionProperty *property, MonoBoolean optional)
+ves_icall_MonoPropertyInfo_GetTypeModifiers (MonoReflectionProperty *property, MonoBoolean optional)
 {
+       MonoError error;
        MonoType *type = get_property_type (property->property);
        MonoImage *image = property->klass->image;
+       MonoArray *res;
 
        if (!type)
                return NULL;
-       return type_array_from_modifiers (image, type, optional);
+       res = type_array_from_modifiers (image, type, optional, &error);
+       mono_error_raise_exception (&error);
+       return res;
 }
 
 /*
@@ -7704,6 +7771,7 @@ mono_type_from_blob_type (MonoType *type, MonoTypeEnum blob_type, MonoType *real
 ICALL_EXPORT MonoObject*
 property_info_get_default_value (MonoReflectionProperty *property)
 {
+       MonoError error;
        MonoType blob_type;
        MonoProperty *prop = property->property;
        MonoType *type = get_property_type (prop);
@@ -7722,8 +7790,9 @@ property_info_get_default_value (MonoReflectionProperty *property)
        def_value = mono_class_get_property_default_value (prop, &def_type);
 
        mono_type_from_blob_type (&blob_type, def_type, type);
-       o = mono_get_object_from_blob (domain, &blob_type, def_value);
+       o = mono_get_object_from_blob (domain, &blob_type, def_value, &error);
 
+       mono_error_set_pending_exception (&error);
        return o;
 }
 
@@ -7735,7 +7804,8 @@ custom_attrs_defined_internal (MonoObject *obj, MonoReflectionType *attr_type)
        MonoCustomAttrInfo *cinfo;
        gboolean found;
 
-       mono_class_init_or_throw (attr_class);
+       mono_class_init_checked (attr_class, &error);
+       mono_error_raise_exception (&error);
 
        cinfo = mono_reflection_get_custom_attrs_info_checked (obj, &error);
        if (!is_ok (&error)) {
@@ -7757,8 +7827,10 @@ custom_attrs_get_by_type (MonoObject *obj, MonoReflectionType *attr_type)
        MonoArray *res;
        MonoError error;
 
-       if (attr_class)
-               mono_class_init_or_throw (attr_class);
+       if (attr_class) {
+               mono_class_init_checked (attr_class, &error);
+               mono_error_raise_exception (&error);
+       }
 
        res = mono_reflection_get_custom_attrs_by_type (obj, attr_class, &error);
        if (!mono_error_ok (&error)) {