[runtime] Move some mono_error_raise_exception () calls to icalls from utility functi...
[mono.git] / mono / metadata / icall.c
index bd8254462b007c11c46c53120ac4237f0737284c..82d2c58996f37486b8f9cc672dfb4240ad32197a 100644 (file)
@@ -108,8 +108,16 @@ extern MonoString* ves_icall_System_Environment_GetOSVersionString (void);
 
 ICALL_EXPORT MonoReflectionAssembly* ves_icall_System_Reflection_Assembly_GetCallingAssembly (void);
 
+/* Lazy class loading functions */
+static GENERATE_GET_CLASS_WITH_CACHE (system_version, System, Version)
+static GENERATE_GET_CLASS_WITH_CACHE (assembly_name, System.Reflection, AssemblyName)
+static GENERATE_GET_CLASS_WITH_CACHE (constructor_info, System.Reflection, ConstructorInfo)
+static GENERATE_GET_CLASS_WITH_CACHE (property_info, System.Reflection, PropertyInfo)
+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)
@@ -118,10 +126,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 *
@@ -540,7 +550,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 */
@@ -590,7 +601,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 */
@@ -885,6 +897,7 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObj
 ICALL_EXPORT void
 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle)
 {
+       MonoError error;
        MonoClass *klass;
        MonoVTable *vtable;
 
@@ -896,10 +909,15 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (Mo
        if (klass->generic_container)
                return;
 
-       vtable = mono_class_vtable_full (mono_domain_get (), klass, TRUE);
+       vtable = mono_class_vtable_full (mono_domain_get (), klass, &error);
+       if (!is_ok (&error)) {
+               mono_error_set_pending_exception (&error);
+               return;
+       }
 
        /* This will call the type constructor */
-       mono_runtime_class_init (vtable);
+       if (!mono_runtime_class_init_full (vtable, &error))
+               mono_error_set_pending_exception (&error);
 }
 
 ICALL_EXPORT void
@@ -915,7 +933,13 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (M
                        return;
                }
                /*It's fine to raise the exception here*/
-               mono_runtime_class_init (mono_class_vtable_full (mono_domain_get (), module_klass, TRUE));
+               MonoVTable * vtable = mono_class_vtable_full (mono_domain_get (), module_klass, &error);
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return;
+               }
+               if (!mono_runtime_class_init_full (vtable, &error))
+                       mono_error_set_pending_exception (&error);
        }
 }
 
@@ -1143,7 +1167,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);
@@ -1154,7 +1178,10 @@ ves_icall_ModuleBuilder_getToken (MonoReflectionModuleBuilder *mb, MonoObject *o
 {
        MONO_CHECK_ARG_NULL (obj, 0);
        
-       return mono_image_create_token (mb->dynamic_image, obj, create_open_instance, TRUE);
+       MonoError error;
+       gint32 result = mono_image_create_token (mb->dynamic_image, obj, create_open_instance, TRUE, &error);
+       mono_error_raise_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT gint32
@@ -1164,20 +1191,27 @@ ves_icall_ModuleBuilder_getMethodToken (MonoReflectionModuleBuilder *mb,
 {
        MONO_CHECK_ARG_NULL (method, 0);
        
-       return mono_image_create_method_token (
-               mb->dynamic_image, (MonoObject *) method, opt_param_types);
+       MonoError error;
+       gint32 result = mono_image_create_method_token (
+               mb->dynamic_image, (MonoObject *) method, opt_param_types, &error);
+       mono_error_raise_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT void
 ves_icall_ModuleBuilder_WriteToFile (MonoReflectionModuleBuilder *mb, HANDLE file)
 {
-       mono_image_create_pefile (mb, file);
+       MonoError error;
+       mono_image_create_pefile (mb, file, &error);
+       mono_error_raise_exception (&error);
 }
 
 ICALL_EXPORT void
 ves_icall_ModuleBuilder_build_metadata (MonoReflectionModuleBuilder *mb)
 {
-       mono_image_build_metadata (mb);
+       MonoError error;
+       if (!mono_image_build_metadata (mb, &error))
+               mono_error_raise_exception (&error);
 }
 
 ICALL_EXPORT void
@@ -1263,16 +1297,16 @@ get_caller_no_reflection (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed
 }
 
 static MonoReflectionType *
-type_from_parsed_name (MonoTypeNameParse *info, MonoBoolean ignoreCase)
+type_from_parsed_name (MonoTypeNameParse *info, MonoBoolean ignoreCase, MonoError *error)
 {
-       MonoError error;
-       MonoReflectionType *ret;
        MonoMethod *m, *dest;
 
        MonoType *type = NULL;
        MonoAssembly *assembly = NULL;
        gboolean type_resolve = FALSE;
 
+       mono_error_init (error);
+
        /*
         * We must compute the calling assembly as type loading must happen under a metadata context.
         * For example. The main assembly is a.exe and Type.GetType is called from dir/b.dll. Without
@@ -1320,42 +1354,15 @@ type_from_parsed_name (MonoTypeNameParse *info, MonoBoolean ignoreCase)
        if (!type) 
                return NULL;
 
-       ret = mono_type_get_object_checked (mono_domain_get (), type, &error);
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
-
-       return ret;
-}
-
-#ifdef UNUSED
-MonoReflectionType *
-mono_type_get (const char *str)
-{
-       char *copy = g_strdup (str);
-       MonoTypeNameParse info;
-       MonoReflectionType *type;
-       gboolean parsedOk;
-
-       parsedOk = mono_reflection_parse_type(copy, &info);
-       if (!parsedOk) {
-               mono_reflection_free_type_info (&info);
-               g_free(copy);
-               return NULL;
-       }
-
-       type = type_from_parsed_name (&info, FALSE);
-
-       mono_reflection_free_type_info (&info);
-       g_free(copy);
-
-       return type;
+       return mono_type_get_object_checked (mono_domain_get (), type, error);
 }
-#endif
 
 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);
        MonoTypeNameParse info;
        MonoReflectionType *type;
@@ -1373,11 +1380,19 @@ ves_icall_type_from_name (MonoString *name,
                return NULL;
        }
 
-       type = type_from_parsed_name (&info, ignoreCase);
+       type = type_from_parsed_name (&info, ignoreCase, &error);
 
        mono_reflection_free_type_info (&info);
        g_free (str);
 
+       if (!mono_error_ok (&error)) {
+               if (throwOnError)
+                       mono_error_set_pending_exception (&error);
+               else
+                       mono_error_cleanup (&error);
+               return NULL;
+       }
+
        if (type == NULL){
                MonoException *e = NULL;
                
@@ -1396,7 +1411,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;
@@ -1530,7 +1545,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;
@@ -1569,15 +1584,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;
@@ -1586,6 +1603,7 @@ ves_icall_get_attributes (MonoReflectionType *type)
 ICALL_EXPORT MonoReflectionMarshalAsAttribute*
 ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionField *field)
 {
+       MonoError error;
        MonoClass *klass = field->field->parent;
        MonoMarshalType *info;
        MonoType *ftype;
@@ -1605,8 +1623,13 @@ ves_icall_System_Reflection_FieldInfo_get_marshal_info (MonoReflectionField *fie
                if (info->fields [i].field == field->field) {
                        if (!info->fields [i].mspec)
                                return NULL;
-                       else
-                               return mono_reflection_marshal_as_attribute_from_marshal_spec (field->object.vtable->domain, klass, info->fields [i].mspec);
+                       else {
+                               MonoReflectionMarshalAsAttribute* obj;
+                               obj = mono_reflection_marshal_as_attribute_from_marshal_spec (field->object.vtable->domain, klass, info->fields [i].mspec, &error);
+                               if (!mono_error_ok (&error))
+                                       mono_error_set_pending_exception (&error);
+                               return obj;
+                       }
                }
        }
 
@@ -1651,12 +1674,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
@@ -1719,6 +1746,7 @@ ves_icall_get_parameter_info (MonoMethod *method, MonoReflectionMethod *member)
 ICALL_EXPORT MonoReflectionMarshalAsAttribute*
 ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method)
 {
+       MonoError error;
        MonoDomain *domain = mono_domain_get (); 
        MonoReflectionMarshalAsAttribute* res = NULL;
        MonoMarshalSpec **mspecs;
@@ -1727,8 +1755,13 @@ ves_icall_System_MonoMethodInfo_get_retval_marshal (MonoMethod *method)
        mspecs = g_new (MonoMarshalSpec*, mono_method_signature (method)->param_count + 1);
        mono_method_get_marshal_info (method, mspecs);
 
-       if (mspecs [0])
-               res = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [0]);
+       if (mspecs [0]) {
+               res = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [0], &error);
+               if (!mono_error_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               }
+       }
                
        for (i = mono_method_signature (method)->param_count; i >= 0; i--)
                if (mspecs [i])
@@ -1872,9 +1905,17 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
        }
 
        if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
-               MonoVTable *vtable = mono_class_vtable_full (mono_object_domain (field), cf->parent, TRUE);
-               if (!vtable->initialized)
-                       mono_runtime_class_init (vtable);
+               MonoVTable *vtable = mono_class_vtable_full (mono_object_domain (field), cf->parent, &error);
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return;
+               }
+               if (!vtable->initialized) {
+                       if (!mono_runtime_class_init_full (vtable, &error)) {
+                               mono_error_set_pending_exception (&error);
+                               return;
+                       }
+               }
                mono_field_static_set_value (vtable, cf, v);
        } else {
                mono_field_set_value (obj, cf, v);
@@ -2026,7 +2067,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;
@@ -2084,7 +2125,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;
@@ -2273,8 +2314,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);
 
@@ -2302,8 +2345,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;
@@ -2315,7 +2361,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;
@@ -2329,7 +2375,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
@@ -2348,7 +2395,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;
@@ -2367,45 +2414,62 @@ 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;
        MonoClass *klass = mono_class_from_mono_type (type->type);
-       return mono_module_get_object (mono_object_domain (type), klass->image);
+       result = mono_module_get_object_checked (mono_object_domain (type), klass->image, &error);
+       if (!mono_error_ok (&error))
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoReflectionAssembly*
-ves_icall_MonoType_get_Assembly (MonoReflectionType *type)
+ves_icall_RuntimeTypeHandle_GetAssembly (MonoReflectionType *type)
 {
+       MonoError error;
        MonoDomain *domain = mono_domain_get (); 
        MonoClass *klass = mono_class_from_mono_type (type->type);
-       return mono_assembly_get_object (domain, klass->image->assembly);
+       MonoReflectionAssembly *result = mono_assembly_get_object_checked (domain, klass->image->assembly, &error);
+       if (!result)
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoReflectionType*
@@ -2471,7 +2535,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;
 
@@ -2532,7 +2596,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;
 
@@ -2547,7 +2611,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;
@@ -2589,7 +2653,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);
@@ -2619,7 +2684,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;
 
@@ -2683,7 +2748,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);
 }
@@ -2715,7 +2780,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))) {
@@ -2942,7 +3008,8 @@ ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this_arg, Mo
                mono_security_core_clr_ensure_reflection_access_method (m);
 
        if (!(m->flags & METHOD_ATTRIBUTE_STATIC)) {
-               if (!mono_class_vtable_full (mono_object_domain (method), m->klass, FALSE)) {
+               if (!mono_class_vtable_full (mono_object_domain (method), m->klass, &error)) {
+                       mono_error_cleanup (&error); /* FIXME does this make sense? */
                        mono_gc_wbarrier_generic_store (exc, (MonoObject*) mono_class_get_exception_for_failure (m->klass));
                        return NULL;
                }
@@ -3256,7 +3323,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);
 
@@ -3315,7 +3383,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) {
@@ -3417,6 +3486,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;
@@ -3426,7 +3496,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."));
@@ -3512,7 +3583,7 @@ ves_icall_Type_GetFields_internal (MonoReflectionType *type, MonoString *name, g
        mono_ptr_array_init (tmp_array, 2, MONO_ROOT_SOURCE_REFLECTION, "temporary reflection fields list");
        
 handle_parent: 
-       if (klass->exception_type != MONO_EXCEPTION_NONE) {
+       if (mono_class_has_failure (klass)) {
                mono_ptr_array_destroy (tmp_array);
                mono_set_pending_exception (mono_class_get_exception_for_failure (klass));
                return NULL;
@@ -3629,7 +3700,7 @@ mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bfla
 
        mono_class_setup_methods (klass);
        mono_class_setup_vtable (klass);
-       if (klass->exception_type != MONO_EXCEPTION_NONE || mono_loader_get_last_error ())
+       if (mono_class_has_failure (klass) || mono_loader_get_last_error ())
                goto loader_error;
 
        if (is_generic_parameter (&klass->byval_arg))
@@ -3645,7 +3716,7 @@ mono_class_get_methods_by_name (MonoClass *klass, const char *name, guint32 bfla
 handle_parent:
        mono_class_setup_methods (klass);
        mono_class_setup_vtable (klass);
-       if (klass->exception_type != MONO_EXCEPTION_NONE || mono_loader_get_last_error ())
+       if (mono_class_has_failure (klass) || mono_loader_get_last_error ())
                goto loader_error;              
 
        iter = NULL;
@@ -3702,7 +3773,7 @@ loader_error:
                g_free (method_slots);
        g_ptr_array_free (array, TRUE);
 
-       if (klass->exception_type != MONO_EXCEPTION_NONE) {
+       if (mono_class_has_failure (klass)) {
                *ex = mono_class_get_exception_for_failure (klass);
        } else {
                *ex = mono_loader_error_prepare_exception (mono_loader_get_last_error ());
@@ -3736,7 +3807,11 @@ ves_icall_Type_GetMethodsByName (MonoReflectionType *type, MonoString *name, gui
        klass = mono_class_from_mono_type (type->type);
        refklass = mono_class_from_mono_type (reftype->type);
        domain = ((MonoObject *)type)->vtable->domain;
-       array_vtable = mono_class_vtable_full (domain, MethodInfo_array, TRUE);
+       array_vtable = mono_class_vtable_full (domain, MethodInfo_array, &error);
+       if (!is_ok (&error)) {
+               mono_error_set_pending_exception (&error);
+               return NULL;
+       }
        if (type->type->byref) {
                res = mono_array_new_specific_checked (array_vtable, 0, &error);
                mono_error_set_pending_exception (&error);
@@ -3779,7 +3854,6 @@ ICALL_EXPORT MonoArray*
 ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflags, MonoReflectionType *reftype)
 {
        MonoDomain *domain; 
-       static MonoClass *System_Reflection_ConstructorInfo;
        MonoClass *startklass, *klass, *refklass;
        MonoArray *res;
        MonoMethod *method;
@@ -3797,12 +3871,8 @@ ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflag
        klass = startklass = mono_class_from_mono_type (type->type);
        refklass = mono_class_from_mono_type (reftype->type);
 
-       if (!System_Reflection_ConstructorInfo)
-               System_Reflection_ConstructorInfo = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
-
        mono_class_setup_methods (klass);
-       if (klass->exception_type != MONO_EXCEPTION_NONE) {
+       if (mono_class_has_failure (klass)) {
                mono_set_pending_exception (mono_class_get_exception_for_failure (klass));
                return NULL;
        }
@@ -3842,7 +3912,7 @@ ves_icall_Type_GetConstructors_internal (MonoReflectionType *type, guint32 bflag
                mono_ptr_array_append (tmp_array, member);
        }
 
-       res = mono_array_new_cached (domain, System_Reflection_ConstructorInfo, mono_ptr_array_size (tmp_array));
+       res = mono_array_new_cached (domain, mono_class_get_constructor_info_class (), mono_ptr_array_size (tmp_array));
 
        for (i = 0; i < mono_ptr_array_size (tmp_array); ++i)
                mono_array_setref (res, i, mono_ptr_array_get (tmp_array, i));
@@ -3913,9 +3983,8 @@ property_accessor_nonpublic (MonoMethod* accessor, gboolean start_klass)
 ICALL_EXPORT MonoArray*
 ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoBoolean ignore_case, MonoReflectionType *reftype)
 {
-       MonoException *ex;
+       MonoError error;
        MonoDomain *domain; 
-       static MonoClass *System_Reflection_PropertyInfo;
        MonoClass *startklass, *klass;
        MonoArray *res;
        MonoMethod *method;
@@ -3928,15 +3997,13 @@ ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name,
        GHashTable *properties = NULL;
        MonoPtrArray tmp_array;
 
+       mono_error_init (&error);
+       
        mono_ptr_array_init (tmp_array, 8, MONO_ROOT_SOURCE_REFLECTION, "temporary reflection properties list"); /*This the average for ASP.NET types*/
 
-       if (!System_Reflection_PropertyInfo)
-               System_Reflection_PropertyInfo = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "PropertyInfo");
-
        domain = ((MonoObject *)type)->vtable->domain;
        if (type->type->byref)
-               return mono_array_new_cached (domain, System_Reflection_PropertyInfo, 0);
+               return mono_array_new_cached (domain, mono_class_get_property_info_class (), 0);
        klass = startklass = mono_class_from_mono_type (type->type);
 
        if (name != NULL) {
@@ -3948,7 +4015,7 @@ ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name,
 handle_parent:
        mono_class_setup_methods (klass);
        mono_class_setup_vtable (klass);
-       if (klass->exception_type != MONO_EXCEPTION_NONE || mono_loader_get_last_error ())
+       if (mono_class_has_failure (klass) || mono_loader_get_last_error ())
                goto loader_error;
 
        iter = NULL;
@@ -3995,7 +4062,10 @@ handle_parent:
                if (g_hash_table_lookup (properties, prop))
                        continue;
 
-               mono_ptr_array_append (tmp_array, mono_property_get_object (domain, startklass, prop));
+               MonoReflectionProperty *pr = mono_property_get_object_checked (domain, startklass, prop, &error);
+               if (!pr)
+                       goto failure;
+               mono_ptr_array_append (tmp_array, pr);
                
                g_hash_table_insert (properties, prop, prop);
        }
@@ -4005,7 +4075,7 @@ handle_parent:
        g_hash_table_destroy (properties);
        g_free (propname);
 
-       res = mono_array_new_cached (domain, System_Reflection_PropertyInfo, mono_ptr_array_size (tmp_array));
+       res = mono_array_new_cached (domain, mono_class_get_property_info_class (), mono_ptr_array_size (tmp_array));
        for (i = 0; i < mono_ptr_array_size (tmp_array); ++i)
                mono_array_setref (res, i, mono_ptr_array_get (tmp_array, i));
 
@@ -4013,20 +4083,25 @@ handle_parent:
 
        return res;
 
+
+
 loader_error:
+       if (mono_class_has_failure (klass)) {
+               mono_error_set_exception_instance (&error, mono_class_get_exception_for_failure (klass));
+       } else {
+               mono_error_set_from_loader_error (&error);
+               mono_loader_clear_error ();
+       }
+
+failure:
        if (properties)
                g_hash_table_destroy (properties);
        if (name)
                g_free (propname);
        mono_ptr_array_destroy (tmp_array);
 
-       if (klass->exception_type != MONO_EXCEPTION_NONE) {
-               ex = mono_class_get_exception_for_failure (klass);
-       } else {
-               ex = mono_loader_error_prepare_exception (mono_loader_get_last_error ());
-               mono_loader_clear_error ();
-       }
-       mono_set_pending_exception (ex);
+       mono_error_set_pending_exception (&error);
+
        return NULL;
 }
 
@@ -4048,9 +4123,8 @@ event_equal (MonoEvent *event1, MonoEvent *event2)
 ICALL_EXPORT MonoArray*
 ves_icall_Type_GetEvents_internal (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoReflectionType *reftype)
 {
-       MonoException *ex;
+       MonoError error;
        MonoDomain *domain; 
-       static MonoClass *System_Reflection_EventInfo;
        MonoClass *startklass, *klass;
        MonoArray *res;
        MonoMethod *method;
@@ -4062,22 +4136,20 @@ ves_icall_Type_GetEvents_internal (MonoReflectionType *type, MonoString *name, g
        GHashTable *events = NULL;
        MonoPtrArray tmp_array;
 
+       mono_error_init (&error);
+       
        mono_ptr_array_init (tmp_array, 4, MONO_ROOT_SOURCE_REFLECTION, "temporary reflection events list");
 
-       if (!System_Reflection_EventInfo)
-               System_Reflection_EventInfo = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "EventInfo");
-
        domain = mono_object_domain (type);
        if (type->type->byref)
-               return mono_array_new_cached (domain, System_Reflection_EventInfo, 0);
+               return mono_array_new_cached (domain, mono_class_get_event_info_class (), 0);
        klass = startklass = mono_class_from_mono_type (type->type);
 
        events = g_hash_table_new (event_hash, (GEqualFunc)event_equal);
 handle_parent:
        mono_class_setup_methods (klass);
        mono_class_setup_vtable (klass);
-       if (klass->exception_type != MONO_EXCEPTION_NONE || mono_loader_get_last_error ())
+       if (mono_class_has_failure (klass) || mono_loader_get_last_error ())
                goto loader_error;
 
        iter = NULL;
@@ -4132,7 +4204,11 @@ handle_parent:
                if (g_hash_table_lookup (events, event))
                        continue;
 
-               mono_ptr_array_append (tmp_array, mono_event_get_object (domain, startklass, event));
+               MonoReflectionEvent *ev_obj;
+               ev_obj = mono_event_get_object_checked (domain, startklass, event, &error);
+               if (!ev_obj)
+                       goto failure;
+               mono_ptr_array_append (tmp_array, ev_obj);
 
                g_hash_table_insert (events, event, event);
        }
@@ -4141,7 +4217,7 @@ handle_parent:
 
        g_hash_table_destroy (events);
 
-       res = mono_array_new_cached (domain, System_Reflection_EventInfo, mono_ptr_array_size (tmp_array));
+       res = mono_array_new_cached (domain, mono_class_get_event_info_class (), mono_ptr_array_size (tmp_array));
 
        for (i = 0; i < mono_ptr_array_size (tmp_array); ++i)
                mono_array_setref (res, i, mono_ptr_array_get (tmp_array, i));
@@ -4154,14 +4230,23 @@ handle_parent:
        return res;
 
 loader_error:
-       mono_ptr_array_destroy (tmp_array);
-       if (klass->exception_type != MONO_EXCEPTION_NONE) {
-               ex = mono_class_get_exception_for_failure (klass);
+       if (mono_class_has_failure (klass)) {
+               mono_error_set_exception_instance (&error, mono_class_get_exception_for_failure (klass));
        } else {
-               ex = mono_loader_error_prepare_exception (mono_loader_get_last_error ());
+               mono_error_set_from_loader_error (&error);
                mono_loader_clear_error ();
        }
-       mono_set_pending_exception (ex);
+
+failure:
+       
+       if (events != NULL)
+               g_hash_table_destroy (events);
+       if (utf8_name != NULL)
+               g_free (utf8_name);
+
+       mono_ptr_array_destroy (tmp_array);
+
+       mono_error_set_pending_exception (&error);
        return NULL;
 }
 
@@ -4339,7 +4424,7 @@ ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssembly *as
                MonoClass *klass = mono_type_get_class (type);
 
                /* need to report exceptions ? */
-               if (throwOnError && klass->exception_type) {
+               if (throwOnError && mono_class_has_failure (klass)) {
                        /* report SecurityException (or others) that occured when loading the assembly */
                        MonoException *exc = mono_class_get_exception_for_failure (klass);
                        mono_loader_clear_error ();
@@ -4445,9 +4530,11 @@ ves_icall_System_Reflection_Assembly_get_global_assembly_cache (MonoReflectionAs
 ICALL_EXPORT MonoReflectionAssembly*
 ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoString *mname, MonoObject *evidence)
 {
+       MonoError error;
        gchar *name;
        MonoAssembly *res;
        MonoImageOpenStatus status;
+       MonoReflectionAssembly* result = NULL;
        
        name = mono_string_to_utf8 (mname);
        res = mono_assembly_load_with_partial_name (name, &status);
@@ -4456,7 +4543,10 @@ ves_icall_System_Reflection_Assembly_load_with_partial_name (MonoString *mname,
 
        if (res == NULL)
                return NULL;
-       return mono_assembly_get_object (mono_domain_get (), res);
+       result = mono_assembly_get_object_checked (mono_domain_get (), res, &error);
+       if (!result)
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoString *
@@ -4510,7 +4600,12 @@ leave:
 ICALL_EXPORT MonoReflectionModule*
 ves_icall_System_Reflection_Assembly_GetManifestModuleInternal (MonoReflectionAssembly *assembly) 
 {
-       return mono_module_get_object (mono_object_domain (assembly), assembly->assembly->image);
+       MonoError error;
+       MonoReflectionModule *result = NULL;
+       result = mono_module_get_object_checked (mono_object_domain (assembly), assembly->assembly->image, &error);
+       if (!mono_error_ok (&error))
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoArray*
@@ -4529,22 +4624,18 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAss
 }
 
 static MonoObject*
-create_version (MonoDomain *domain, guint32 major, guint32 minor, guint32 build, guint32 revision)
+create_version (MonoDomain *domain, guint32 major, guint32 minor, guint32 build, guint32 revision, MonoError *error)
 {
-       static MonoClass *System_Version = NULL;
        static MonoMethod *create_version = NULL;
-       MonoError error;
        MonoObject *result;
        gpointer args [4];
+
+       mono_error_init (error);
        
-       if (!System_Version) {
-               System_Version = mono_class_from_name (mono_defaults.corlib, "System", "Version");
-               g_assert (System_Version);
-       }
 
        if (!create_version) {
                MonoMethodDesc *desc = mono_method_desc_new (":.ctor(int,int,int,int)", FALSE);
-               create_version = mono_method_desc_search_in_class (desc, System_Version);
+               create_version = mono_method_desc_search_in_class (desc, mono_class_get_system_version_class ());
                g_assert (create_version);
                mono_method_desc_free (desc);
        }
@@ -4553,9 +4644,11 @@ create_version (MonoDomain *domain, guint32 major, guint32 minor, guint32 build,
        args [1] = &minor;
        args [2] = &build;
        args [3] = &revision;
-       result = mono_object_new_checked (domain, System_Version, &error);
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
-       mono_runtime_invoke (create_version, result, args, NULL);
+       result = mono_object_new_checked (domain, mono_class_get_system_version_class (), error);
+       return_val_if_nok (error, NULL);
+
+       mono_runtime_invoke_checked (create_version, result, args, error);
+       return_val_if_nok (error, NULL);
 
        return result;
 }
@@ -4563,7 +4656,6 @@ create_version (MonoDomain *domain, guint32 major, guint32 minor, guint32 build,
 ICALL_EXPORT MonoArray*
 ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAssembly *assembly) 
 {
-       static MonoClass *System_Reflection_AssemblyName;
        MonoError error;
        MonoArray *result;
        MonoDomain *domain = mono_object_domain (assembly);
@@ -4571,15 +4663,12 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
        static MonoMethod *create_culture = NULL;
        MonoImage *image = assembly->assembly->image;
        MonoTableInfo *t;
-
-       if (!System_Reflection_AssemblyName)
-               System_Reflection_AssemblyName = mono_class_from_name (
-                       mono_defaults.corlib, "System.Reflection", "AssemblyName");
+       MonoObject *o;
 
        t = &assembly->assembly->image->tables [MONO_TABLE_ASSEMBLYREF];
        count = t->rows;
 
-       result = mono_array_new (domain, System_Reflection_AssemblyName, count);
+       result = mono_array_new (domain, mono_class_get_assembly_name_class (), count);
 
        if (count > 0 && !create_culture) {
                MonoMethodDesc *desc = mono_method_desc_new (
@@ -4590,13 +4679,14 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
        }
 
        for (i = 0; i < count; i++) {
+               MonoObject *version;
                MonoReflectionAssemblyName *aname;
                guint32 cols [MONO_ASSEMBLYREF_SIZE];
 
                mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
 
                aname = (MonoReflectionAssemblyName *) mono_object_new_checked (
-                       domain, System_Reflection_AssemblyName, &error);
+                       domain, mono_class_get_assembly_name_class (), &error);
                mono_error_raise_exception (&error);
 
                MONO_OBJECT_SETREF (aname, name, mono_string_new (domain, mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME])));
@@ -4608,14 +4698,22 @@ ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAsse
                aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
                aname->versioncompat = 1; /* SameMachine (default) */
                aname->hashalg = ASSEMBLY_HASH_SHA1; /* SHA1 (default) */
-               MONO_OBJECT_SETREF (aname, version, create_version (domain, aname->major, aname->minor, aname->build, aname->revision));
+
+               version = create_version (domain, aname->major, aname->minor, aname->build, aname->revision, &error);
+               mono_error_raise_exception (&error);
+
+               MONO_OBJECT_SETREF (aname, version, version);
 
                if (create_culture) {
                        gpointer args [2];
                        MonoBoolean assembly_ref = 1;
                        args [0] = mono_string_new (domain, mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]));
                        args [1] = &assembly_ref;
-                       MONO_OBJECT_SETREF (aname, cultureInfo, mono_runtime_invoke (create_culture, NULL, args, NULL));
+
+                       o = mono_runtime_invoke_checked (create_culture, NULL, args, &error);
+                       mono_error_raise_exception (&error);
+
+                       MONO_OBJECT_SETREF (aname, cultureInfo, o);
                }
                
                if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
@@ -4663,6 +4761,7 @@ g_concat_dir_and_file (const char *dir, const char *file)
 ICALL_EXPORT void *
 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name, gint32 *size, MonoReflectionModule **ref_module) 
 {
+       MonoError error;
        char *n = mono_string_to_utf8 (name);
        MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
        guint32 i;
@@ -4697,7 +4796,10 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflection
        else
                module = assembly->assembly->image;
 
-       mono_gc_wbarrier_generic_store (ref_module, (MonoObject*) mono_module_get_object (mono_domain_get (), module));
+       
+       MonoReflectionModule *rm = mono_module_get_object_checked (mono_domain_get (), module, &error);
+       mono_error_raise_exception (&error);
+       mono_gc_wbarrier_generic_store (ref_module, (MonoObject*) rm);
 
        return (void*)mono_image_get_resource (module, cols [MONO_MANIFEST_OFFSET], (guint32*)size);
 }
@@ -4705,6 +4807,7 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflection
 ICALL_EXPORT gboolean
 ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflectionAssembly *assembly, MonoString *name, MonoManifestResourceInfo *info)
 {
+       MonoError error;
        MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
        int i;
        guint32 cols [MONO_MANIFEST_SIZE];
@@ -4750,7 +4853,13 @@ ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflec
                                mono_set_pending_exception (ex);
                                return FALSE;
                        }
-                       MONO_OBJECT_SETREF (info, assembly, mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]));
+                       MonoReflectionAssembly *assm_obj;
+                       assm_obj = mono_assembly_get_object_checked (mono_domain_get (), assembly->assembly->image->references [i - 1], &error);
+                       if (!assm_obj) {
+                               mono_error_set_pending_exception (&error);
+                               return FALSE;
+                       }
+                       MONO_OBJECT_SETREF (info, assembly, assm_obj);
 
                        /* Obtain info recursively */
                        ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (info->assembly, name, info);
@@ -4817,6 +4926,7 @@ ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *a
 ICALL_EXPORT MonoArray*
 ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly *assembly)
 {
+       MonoError error;
        MonoDomain *domain = mono_domain_get();
        MonoArray *res;
        MonoClass *klass;
@@ -4841,21 +4951,28 @@ ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly
                if (modules [i])
                        real_module_count ++;
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection", "Module");
+       klass = mono_class_get_module_class ();
        res = mono_array_new (domain, klass, 1 + real_module_count + file_count);
 
-       mono_array_setref (res, 0, mono_module_get_object (domain, image));
+       MonoReflectionModule *image_obj = mono_module_get_object_checked (domain, image, &error);
+       mono_error_raise_exception (&error);
+       mono_array_setref (res, 0, image_obj);
        j = 1;
        for (i = 0; i < module_count; ++i)
                if (modules [i]) {
-                       mono_array_setref (res, j, mono_module_get_object (domain, modules[i]));
+                       MonoReflectionModule *rm = mono_module_get_object_checked (domain, modules[i], &error);
+                       mono_error_raise_exception (&error);
+                       mono_array_setref (res, j, rm);
                        ++j;
                }
 
        for (i = 0; i < file_count; ++i, ++j) {
                mono_metadata_decode_row (table, i, cols, MONO_FILE_SIZE);
-               if (cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA)
-                       mono_array_setref (res, j, mono_module_file_get_object (domain, image, i));
+               if (cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA) {
+                       MonoReflectionModule *rm = mono_module_file_get_object_checked (domain, image, i, &error);
+                       mono_error_raise_exception (&error);
+                       mono_array_setref (res, j, rm);
+               }
                else {
                        MonoImage *m = mono_image_load_file_for_image (image, i + 1);
                        if (!m) {
@@ -4863,7 +4980,9 @@ ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly
                                mono_set_pending_exception (mono_get_exception_file_not_found2 (NULL, fname));
                                return NULL;
                        }
-                       mono_array_setref (res, j, mono_module_get_object (domain, m));
+                       MonoReflectionModule *rm = mono_module_get_object_checked (domain, m, &error);
+                       mono_error_raise_exception (&error);
+                       mono_array_setref (res, j, rm);
                }
        }
 
@@ -4914,7 +5033,7 @@ mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
        }
 
        mono_class_setup_methods (method->klass);
-       if (method->klass->exception_type)
+       if (mono_class_has_failure (method->klass))
                return NULL;
        for (i = 0; i < method->klass->method.count; ++i) {
                if (method->klass->methods [i] == method) {
@@ -4923,7 +5042,7 @@ mono_method_get_equivalent_method (MonoMethod *method, MonoClass *klass)
                }       
        }
        mono_class_setup_methods (klass);
-       if (klass->exception_type)
+       if (mono_class_has_failure (klass))
                return NULL;
        g_assert (offset >= 0 && offset < klass->method.count);
        return klass->methods [offset];
@@ -4960,30 +5079,42 @@ ves_icall_System_Reflection_MethodBase_GetMethodBodyInternal (MonoMethod *method
 ICALL_EXPORT MonoReflectionAssembly*
 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
 {
+       MonoError error;
+       MonoReflectionAssembly *result;
        MonoMethod *dest = NULL;
 
        mono_stack_walk_no_il (get_executing, &dest);
        g_assert (dest);
-       return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
+       result = mono_assembly_get_object_checked (mono_domain_get (), dest->klass->image->assembly, &error);
+       if (!result)
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 
 ICALL_EXPORT MonoReflectionAssembly*
 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
 {
+       MonoError error;
+       MonoReflectionAssembly *result;
        MonoDomain* domain = mono_domain_get ();
 
        if (!domain->entry_assembly)
                return NULL;
 
-       return mono_assembly_get_object (domain, domain->entry_assembly);
+       result = mono_assembly_get_object_checked (domain, domain->entry_assembly, &error);
+       if (!result)
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoReflectionAssembly*
 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
 {
+       MonoError error;
        MonoMethod *m;
        MonoMethod *dest;
+       MonoReflectionAssembly *result;
 
        dest = NULL;
        mono_stack_walk_no_il (get_executing, &dest);
@@ -4995,7 +5126,10 @@ ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
                mono_set_pending_exception (mono_get_exception_not_supported ("Stack walks are not supported on this platform."));
                return NULL;
        }
-       return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
+       result = mono_assembly_get_object_checked (mono_domain_get (), dest->klass->image->assembly, &error);
+       if (!result)
+               mono_error_set_pending_exception (&error);
+       return result;
 }
 
 ICALL_EXPORT MonoString *
@@ -5032,8 +5166,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);
 }
 
@@ -5052,15 +5189,18 @@ ves_icall_MonoMethod_get_core_clr_security_level (MonoReflectionMethod *rfield)
 }
 
 static void
-fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *aname, MonoAssemblyName *name, const char *absolute, gboolean by_default_version, gboolean default_publickey, gboolean default_token)
+fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *aname, MonoAssemblyName *name, const char *absolute, gboolean by_default_version, gboolean default_publickey, gboolean default_token, MonoError *error)
 {
        static MonoMethod *create_culture = NULL;
+       MonoObject *obj;
        gpointer args [2];
        guint32 pkey_len;
        const char *pkey_ptr;
        gchar *codebase;
        MonoBoolean assembly_ref = 0;
 
+       mono_error_init (error);
+
        MONO_OBJECT_SETREF (aname, name, mono_string_new (domain, name->name));
        aname->major = name->major;
        aname->minor = name->minor;
@@ -5071,8 +5211,14 @@ fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *a
        aname->versioncompat = 1; /* SameMachine (default) */
        aname->processor_architecture = name->arch;
 
-       if (by_default_version)
-               MONO_OBJECT_SETREF (aname, version, create_version (domain, name->major, name->minor, name->build, name->revision));
+       if (by_default_version) {
+               MonoObject *version;
+
+               version = create_version (domain, name->major, name->minor, name->build, name->revision, error);
+               return_if_nok (error);
+
+               MONO_OBJECT_SETREF (aname, version, version);
+       }
 
        codebase = NULL;
        if (absolute != NULL && *absolute != '\0') {
@@ -5115,7 +5261,11 @@ fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *a
        if (name->culture) {
                args [0] = mono_string_new (domain, name->culture);
                args [1] = &assembly_ref;
-               MONO_OBJECT_SETREF (aname, cultureInfo, mono_runtime_invoke (create_culture, NULL, args, NULL));
+
+               obj = mono_runtime_invoke_checked (create_culture, NULL, args, error);
+               return_if_nok (error);
+
+               MONO_OBJECT_SETREF (aname, cultureInfo, obj);
        }
 
        if (name->public_key) {
@@ -5166,20 +5316,19 @@ ves_icall_System_Reflection_Assembly_get_fullName (MonoReflectionAssembly *assem
 ICALL_EXPORT void
 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
 {
+       MonoError error;
        gchar *absolute;
        MonoAssembly *mass = assembly->assembly;
 
        if (g_path_is_absolute (mass->image->name)) {
-               fill_reflection_assembly_name (mono_object_domain (assembly),
-                       aname, &mass->aname, mass->image->name, TRUE,
-                       TRUE, TRUE);
+               fill_reflection_assembly_name (mono_object_domain (assembly), aname, &mass->aname, mass->image->name, TRUE, TRUE, TRUE, &error);
+               mono_error_set_pending_exception (&error);
                return;
        }
        absolute = g_build_filename (mass->basedir, mass->image->name, NULL);
 
-       fill_reflection_assembly_name (mono_object_domain (assembly),
-               aname, &mass->aname, absolute, TRUE, TRUE,
-               TRUE);
+       fill_reflection_assembly_name (mono_object_domain (assembly), aname, &mass->aname, absolute, TRUE, TRUE, TRUE, &error);
+       mono_error_set_pending_exception (&error);
 
        g_free (absolute);
 }
@@ -5187,6 +5336,7 @@ ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly,
 ICALL_EXPORT void
 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname, MonoReflectionAssemblyName *aname)
 {
+       MonoError error;
        char *filename;
        MonoImageOpenStatus status = MONO_IMAGE_OK;
        gboolean res;
@@ -5222,11 +5372,11 @@ ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname,
                return;
        }
 
-       fill_reflection_assembly_name (mono_domain_get (), aname, &name, filename,
-               TRUE, FALSE, TRUE);
+       fill_reflection_assembly_name (mono_domain_get (), aname, &name, filename, TRUE, FALSE, TRUE, &error);
+       mono_error_set_pending_exception (&error);
 
-       g_free (filename);
        mono_image_close (image);
+       g_free (filename);
 }
 
 ICALL_EXPORT MonoBoolean
@@ -5274,15 +5424,16 @@ mono_module_type_is_visible (MonoTableInfo *tdef, MonoImage *image, int type)
 }
 
 static MonoArray*
-mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **exceptions, MonoBoolean exportedOnly)
+mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **exceptions, MonoBoolean exportedOnly, MonoError *error)
 {
-       MonoError error;
        MonoReflectionType *rt;
        MonoArray *res;
        MonoClass *klass;
        MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
        int i, count;
 
+       mono_error_init (error);
+
        /* we start the count from 1 because we skip the special type <Module> */
        if (exportedOnly) {
                count = 0;
@@ -5298,17 +5449,17 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **excepti
        count = 0;
        for (i = 1; i < tdef->rows; ++i) {
                if (!exportedOnly || mono_module_type_is_visible (tdef, image, i + 1)) {
-                       klass = mono_class_get_checked (image, (i + 1) | MONO_TOKEN_TYPE_DEF, &error);
+                       klass = mono_class_get_checked (image, (i + 1) | MONO_TOKEN_TYPE_DEF, error);
                        mono_loader_assert_no_error (); /* Plug any leaks */
-                       g_assert (mono_error_ok (&error));
+                       mono_error_assert_ok (error);
                        
                        if (klass) {
-                               rt = mono_type_get_object_checked (domain, &klass->byval_arg, &error);
-                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                               rt = mono_type_get_object_checked (domain, &klass->byval_arg, error);
+                               return_val_if_nok (error, NULL);
 
                                mono_array_setref (res, count, rt);
                        } else {
-                               MonoException *ex = mono_error_convert_to_exception (&error);
+                               MonoException *ex = mono_error_convert_to_exception (error);
                                mono_array_setref (*exceptions, count, ex);
                        }
                        count++;
@@ -5321,6 +5472,7 @@ mono_module_get_types (MonoDomain *domain, MonoImage *image, MonoArray **excepti
 ICALL_EXPORT MonoArray*
 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
 {
+       MonoError error;
        MonoArray *res = NULL;
        MonoArray *exceptions = NULL;
        MonoImage *image = NULL;
@@ -5334,7 +5486,8 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
        g_assert (!assembly_is_dynamic (assembly->assembly));
        image = assembly->assembly->image;
        table = &image->tables [MONO_TABLE_FILE];
-       res = mono_module_get_types (domain, image, &exceptions, exportedOnly);
+       res = mono_module_get_types (domain, image, &exceptions, exportedOnly, &error);
+       mono_error_raise_exception (&error);
 
        /* Append data from all modules in the assembly */
        for (i = 0; i < table->rows; ++i) {
@@ -5342,7 +5495,11 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
                        MonoImage *loaded_image = mono_assembly_load_module (image->assembly, i + 1);
                        if (loaded_image) {
                                MonoArray *ex2;
-                               MonoArray *res2 = mono_module_get_types (domain, loaded_image, &ex2, exportedOnly);
+                               MonoArray *res2;
+
+                               res2 = mono_module_get_types (domain, loaded_image, &ex2, exportedOnly, &error);
+                               mono_error_raise_exception (&error);
+
                                /* Append the new types to the end of the array */
                                if (mono_array_length (res2) > 0) {
                                        guint32 len1, len2;
@@ -5379,7 +5536,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
 
                if (t) {
                        klass = mono_type_get_class (t->type);
-                       if ((klass != NULL) && klass->exception_type) {
+                       if ((klass != NULL) && mono_class_has_failure (klass)) {
                                /* keep the class in the list */
                                list = g_list_append (list, klass);
                                /* and replace Type with NULL */
@@ -5428,6 +5585,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
 ICALL_EXPORT gboolean
 ves_icall_System_Reflection_AssemblyName_ParseName (MonoReflectionAssemblyName *name, MonoString *assname)
 {
+       MonoError error;
        MonoAssemblyName aname;
        MonoDomain *domain = mono_object_domain (name);
        char *val;
@@ -5442,8 +5600,8 @@ ves_icall_System_Reflection_AssemblyName_ParseName (MonoReflectionAssemblyName *
                return FALSE;
        }
        
-       fill_reflection_assembly_name (domain, name, &aname, "", is_version_defined,
-               FALSE, is_token_defined);
+       fill_reflection_assembly_name (domain, name, &aname, "", is_version_defined, FALSE, is_token_defined, &error);
+       mono_error_set_pending_exception (&error);
 
        mono_assembly_name_free (&aname);
        g_free ((guint8*) aname.public_key);
@@ -5531,13 +5689,18 @@ ves_icall_System_Reflection_Module_GetMDStreamVersion (MonoImage *image)
 ICALL_EXPORT MonoArray*
 ves_icall_System_Reflection_Module_InternalGetTypes (MonoReflectionModule *module)
 {
+       MonoError error;
        MonoArray *exceptions;
        int i;
 
        if (!module->image)
                return mono_array_new (mono_object_domain (module), mono_defaults.monotype_class, 0);
        else {
-               MonoArray *res = mono_module_get_types (mono_object_domain (module), module->image, &exceptions, FALSE);
+               MonoArray *res;
+
+               res = mono_module_get_types (mono_object_domain (module), module->image, &exceptions, FALSE, &error);
+               mono_error_raise_exception (&error);
+
                for (i = 0; i < mono_array_length (exceptions); ++i) {
                        MonoException *ex = mono_array_get (exceptions, MonoException *, i);
                        if (ex) {
@@ -5930,7 +6093,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;
@@ -5942,17 +6105,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 *
@@ -5963,7 +6129,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);
@@ -5984,8 +6151,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);
@@ -6001,8 +6170,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);
 
@@ -6022,7 +6193,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);
 
@@ -6189,7 +6361,7 @@ ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this_obj, MonoStri
 
        // mono_remote_class_vtable cannot handle errors well, so force any loading error to occur early
        mono_class_setup_vtable (klass);
-       if (klass->exception_type) {
+       if (mono_class_has_failure (klass)) {
                mono_set_pending_exception (mono_class_get_exception_for_failure (klass));
                return NULL;
        }
@@ -6766,7 +6938,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;
@@ -6809,11 +6982,16 @@ ves_icall_Remoting_RemotingServices_GetVirtualMethod (
 ICALL_EXPORT void
 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
 {
+       MonoError error;
        MonoClass *klass;
        MonoVTable* vtable;
 
        klass = mono_class_from_mono_type (type->type);
-       vtable = mono_class_vtable_full (mono_domain_get (), klass, TRUE);
+       vtable = mono_class_vtable_full (mono_domain_get (), klass, &error);
+       if (!is_ok (&error)) {
+               mono_error_set_pending_exception (&error);
+               return;
+       }
 
        mono_vtable_set_is_remote (vtable, enable);
 }
@@ -6838,7 +7016,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"));
@@ -6849,8 +7028,13 @@ ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClas
                g_assert (klass->rank == 1);
                return (MonoObject *) mono_array_new (domain, klass->element_class, 0);
        } else {
+               MonoVTable *vtable = mono_class_vtable_full (domain, klass, &error);
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               }
                /* Bypass remoting object creation check */
-               ret = mono_object_new_alloc_specific_checked (mono_class_vtable_full (domain, klass, TRUE), &error);
+               ret = mono_object_new_alloc_specific_checked (vtable, &error);
                mono_error_set_pending_exception (&error);
 
                return ret;
@@ -6900,7 +7084,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);
 }
@@ -7076,7 +7260,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 */
@@ -7387,15 +7572,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? */
 }
@@ -7403,20 +7591,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 */
@@ -7439,16 +7634,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++;
@@ -7459,11 +7653,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++;
@@ -7473,14 +7667,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;
@@ -7508,7 +7704,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*
@@ -7526,14 +7724,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;
 }
 
 /*
@@ -7587,13 +7789,19 @@ property_info_get_default_value (MonoReflectionProperty *property)
 ICALL_EXPORT MonoBoolean
 custom_attrs_defined_internal (MonoObject *obj, MonoReflectionType *attr_type)
 {
+       MonoError error;
        MonoClass *attr_class = mono_class_from_mono_type (attr_type->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 (obj);
+       cinfo = mono_reflection_get_custom_attrs_info_checked (obj, &error);
+       if (!is_ok (&error)) {
+               mono_error_set_pending_exception (&error);
+               return FALSE;
+       }
        if (!cinfo)
                return FALSE;
        found = mono_custom_attrs_has_attr (cinfo, attr_class);
@@ -7609,8 +7817,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)) {
@@ -7626,6 +7836,17 @@ custom_attrs_get_by_type (MonoObject *obj, MonoReflectionType *attr_type)
        }
 }
 
+ICALL_EXPORT MonoArray*
+ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObject *obj)
+{
+       MonoError error;
+       MonoArray *result;
+       result = mono_reflection_get_custom_attrs_data_checked (obj, &error);
+       mono_error_set_pending_exception (&error);
+       return result;
+}
+
+
 ICALL_EXPORT MonoString*
 ves_icall_Mono_Runtime_GetDisplayName (void)
 {
@@ -7673,6 +7894,76 @@ ves_icall_System_StackFrame_GetILOffsetFromFile (MonoString *path, guint32 metho
        return il_offset;
 }
 
+ICALL_EXPORT gpointer
+ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcess (void)
+{
+       return GetCurrentProcess ();
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_GetExitCodeProcess (gpointer handle, gint32 *exitcode)
+{
+       return GetExitCodeProcess (handle, (guint32*) exitcode);
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_CloseProcess (gpointer handle)
+{
+#if defined(TARGET_WIN32) || defined(HOST_WIN32)
+       return CloseHandle (handle);
+#else
+       return CloseProcess (handle);
+#endif
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_TerminateProcess (gpointer handle, gint32 exitcode)
+{
+       return TerminateProcess (handle, exitcode);
+}
+
+ICALL_EXPORT gint32
+ves_icall_Microsoft_Win32_NativeMethods_WaitForInputIdle (gpointer handle, gint32 milliseconds)
+{
+       return WaitForInputIdle (handle, milliseconds);
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_GetProcessWorkingSetSize (gpointer handle, gsize *min, gsize *max)
+{
+       return GetProcessWorkingSetSize (handle, min, max);
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_SetProcessWorkingSetSize (gpointer handle, gsize min, gsize max)
+{
+       return SetProcessWorkingSetSize (handle, min, max);
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_GetProcessTimes (gpointer handle, gint64 *creationtime, gint64 *exittime, gint64 *kerneltime, gint64 *usertime)
+{
+       return GetProcessTimes (handle, (LPFILETIME) creationtime, (LPFILETIME) exittime, (LPFILETIME) kerneltime, (LPFILETIME) usertime);
+}
+
+ICALL_EXPORT gint32
+ves_icall_Microsoft_Win32_NativeMethods_GetCurrentProcessId (void)
+{
+       return mono_process_current_pid ();
+}
+
+ICALL_EXPORT gint32
+ves_icall_Microsoft_Win32_NativeMethods_GetPriorityClass (gpointer handle)
+{
+       return GetPriorityClass (handle);
+}
+
+ICALL_EXPORT MonoBoolean
+ves_icall_Microsoft_Win32_NativeMethods_SetPriorityClass (gpointer handle, gint32 priorityClass)
+{
+       return SetPriorityClass (handle, priorityClass);
+}
+
 #ifndef DISABLE_ICALL_TABLES
 
 #define ICALL_TYPE(id,name,first)
@@ -7865,6 +8156,42 @@ mono_icall_cleanup (void)
        mono_os_mutex_destroy (&icall_mutex);
 }
 
+/**
+ * mono_add_internal_call:
+ * @name: method specification to surface to the managed world
+ * @method: pointer to a C method to invoke when the method is called
+ *
+ * This method surfaces the C function pointed by @method as a method
+ * that has been surfaced in managed code with the method specified in
+ * @name as an internal call.
+ *
+ * Internal calls are surfaced to all app domains loaded and they are
+ * accessibly by a type with the specified name.
+ *
+ * You must provide a fully qualified type name, that is namespaces
+ * and type name, followed by a colon and the method name, with an
+ * optional signature to bind.
+ *
+ * For example, the following are all valid declarations:
+ *
+ * "MyApp.Services.ScriptService:Accelerate"
+ * "MyApp.Services.ScriptService:Slowdown(int,bool)"
+ *
+ * You use method parameters in cases where there might be more than
+ * one surface method to managed code.  That way you can register different
+ * internal calls for different method overloads.
+ *
+ * The internal calls are invoked with no marshalling.   This means that .NET
+ * types like System.String are exposed as `MonoString *` parameters.   This is
+ * different than the way that strings are surfaced in P/Invoke.
+ *
+ * For more information on how the parameters are marshalled, see the
+ * <a href="http://www.mono-project.com/docs/advanced/embedding/">Mono Embedding</a>
+ * page.
+ *
+ * See the <a  href="mono-api-methods.html#method-desc">Method Description</a>
+ * reference for more information on the format of method descriptions.
+ */
 void
 mono_add_internal_call (const char *name, gconstpointer method)
 {