Merge pull request #2847 from vargaz/get-type-reflection
[mono.git] / mono / metadata / exception.c
index f84219678aa08cece25b0768cfea5ac1e62aa445..e2e162f702a1efeb69e0226a5a4ab8eedb4d52d4 100644 (file)
@@ -9,6 +9,7 @@
  *
  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <glib.h>
@@ -68,11 +69,13 @@ mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
        klass = mono_class_load_from_name (image, name_space, name);
 
        o = mono_object_new_checked (domain, klass, &error);
-       g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */
+       mono_error_assert_ok (&error);
 
        if (domain != caller_domain)
                mono_domain_set_internal (domain);
-       mono_runtime_object_init (o);
+       mono_runtime_object_init_checked (o, &error);
+       mono_error_assert_ok (&error);
+
        if (domain != caller_domain)
                mono_domain_set_internal (caller_domain);
 
@@ -97,12 +100,13 @@ mono_exception_from_token (MonoImage *image, guint32 token)
        MonoObject *o;
 
        klass = mono_class_get_checked (image, token, &error);
-       g_assert (mono_error_ok (&error)); /* FIXME handle the error. */
+       mono_error_assert_ok (&error);
 
        o = mono_object_new_checked (mono_domain_get (), klass, &error);
-       g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */
+       mono_error_assert_ok (&error);
        
-       mono_runtime_object_init (o);
+       mono_runtime_object_init_checked (o, &error);
+       mono_error_assert_ok (&error);
 
        return (MonoException *)o;
 }
@@ -936,8 +940,10 @@ ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
 {
        char *trace;
        MonoString *res;
-       if (!exc)
-               mono_raise_exception (mono_get_exception_argument_null ("exception"));
+       if (!exc) {
+               mono_set_pending_exception (mono_get_exception_argument_null ("exception"));
+               return NULL;
+       }
 
        trace = mono_exception_get_native_backtrace (exc);
        res = mono_string_new (mono_domain_get (), trace);
@@ -963,11 +969,27 @@ mono_error_raise_exception (MonoError *target_error)
                mono_raise_exception (ex);
 }
 
-void
+/**
+ * mono_error_set_pending_exception:
+ * @error: The error
+ *
+ *
+ * If @error is set, convert it to an exception and set the pending exception for the current icall.
+ * Returns TRUE if @error was set, or FALSE otherwise, so that you can write:
+ *    if (mono_error_set_pending_exception (error)) {
+ *      { ... cleanup code ... }
+ *      return;
+ *    }
+ */
+gboolean
 mono_error_set_pending_exception (MonoError *error)
 {
        MonoException *ex = mono_error_convert_to_exception (error);
-       if (ex)
+       if (ex) {
                mono_set_pending_exception (ex);
+               return TRUE;
+       } else {
+               return FALSE;
+       }
 }