MonoExceptionHandle mono_exception_new_serialization (const char *msg, MonoError *error) { return mono_exception_new_by_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", "Could not serialize unhandled exception.", error); }
static MonoExceptionHandle mono_exception_new_argument_internal (const char *type, const char *arg, const char *msg, MonoError *error) { MonoExceptionHandle ex = mono_exception_new_by_name_msg (mono_get_corlib (), "System", type, msg, error); if (arg && !MONO_HANDLE_IS_NULL (ex)) { MonoArgumentExceptionHandle argex = MONO_HANDLE_CAST (MonoArgumentException, ex); MonoStringHandle arg_str = mono_string_new_handle (MONO_HANDLE_DOMAIN (ex), arg, error); MONO_HANDLE_SET (argex, param_name, arg_str); } return ex; }
/** * mono_exception_from_name_msg: * \param image the Mono image where to look for the class * \param name_space the namespace for the class * \param name class name * \param msg the message to embed inside the exception * * Creates an exception and initializes its message field. * * \returns the initialized exception instance. */ MonoException * mono_exception_from_name_msg (MonoImage *image, const char *name_space, const char *name, const char *msg) { HANDLE_FUNCTION_ENTER (); MonoExceptionHandle ex; MONO_ENTER_GC_UNSAFE; ERROR_DECL (error); ex = mono_exception_new_by_name_msg (image, name_space, name, msg, error); mono_error_cleanup (error); MONO_EXIT_GC_UNSAFE; HANDLE_FUNCTION_RETURN_OBJ (ex); }
static MonoExceptionHandle mono_exception_new_argument_internal (const char *type, const char *arg, const char *msg, MonoError *error) { #ifdef ENABLE_NETCORE MonoStringHandle arg_str = arg ? mono_string_new_handle (mono_domain_get (), arg, error) : NULL_HANDLE_STRING; MonoStringHandle msg_str = msg ? mono_string_new_handle (mono_domain_get (), msg, error) : NULL_HANDLE_STRING; if (!strcmp (type, "ArgumentException")) return mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", type, msg_str, arg_str, error); else return mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", type, arg_str, msg_str, error); #else MonoExceptionHandle ex = mono_exception_new_by_name_msg (mono_get_corlib (), "System", type, msg, error); if (arg && !MONO_HANDLE_IS_NULL (ex)) { MonoArgumentExceptionHandle argex = MONO_HANDLE_CAST (MonoArgumentException, ex); MonoStringHandle arg_str = mono_string_new_handle (MONO_HANDLE_DOMAIN (ex), arg, error); MONO_HANDLE_SET (argex, param_name, arg_str); } return ex; #endif }
/*Can fail with out-of-memory*/ MonoException* mono_error_prepare_exception (MonoError *oerror, MonoError *error_out) { HANDLE_FUNCTION_ENTER (); MonoErrorInternal *error = (MonoErrorInternal*)oerror; MonoExceptionHandle exception = MONO_HANDLE_CAST (MonoException, mono_new_null ()); MonoDomain *domain = mono_domain_get (); char *type_name = NULL; char *message = NULL; error_init (error_out); const guint16 error_code = error->error_code; g_assert (error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL); switch (error_code) { case MONO_ERROR_NONE: goto exit; case MONO_ERROR_MISSING_METHOD: exception = mono_corlib_exception_new_with_args ("System", "MissingMethodException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_BAD_IMAGE: exception = mono_corlib_exception_new_with_args ("System", "BadImageFormatException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_FILE_NOT_FOUND: exception = mono_corlib_exception_new_with_args ("System.IO", "FileNotFoundException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_MISSING_FIELD: exception = mono_corlib_exception_new_with_args ("System", "MissingFieldException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_MEMBER_ACCESS: exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "MemberAccessException", error->full_message, error_out); break; case MONO_ERROR_TYPE_LOAD: { MonoStringHandle assembly_name; MonoStringHandle type_name; if ((error->type_name && error->assembly_name) || error->exn.klass) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; if (error->assembly_name) { assembly_name = string_new_cleanup (domain, error->assembly_name); if (MONO_HANDLE_IS_NULL (assembly_name)) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } else { assembly_name = mono_string_empty_handle (domain); } exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out); if (!MONO_HANDLE_IS_NULL (exception)) { const char *full_message = error->full_message; if (full_message && full_message [0]) { MonoStringHandle msg = string_new_cleanup (mono_domain_get (), full_message); if (!MONO_HANDLE_IS_NULL (msg)) MONO_HANDLE_SET (exception, message, msg); else mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); } } } else { exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message, error_out); } } break; case MONO_ERROR_OUT_OF_MEMORY: if (domain) exception = MONO_HANDLE_NEW (MonoException, domain->out_of_memory_ex); if (MONO_HANDLE_IS_NULL (exception)) exception = mono_get_exception_out_of_memory_handle (); break; case MONO_ERROR_ARGUMENT: exception = mono_exception_new_argument (error->first_argument, error->full_message, error_out); break; case MONO_ERROR_ARGUMENT_NULL: exception = mono_exception_new_argument_null (error->first_argument, error_out); break; case MONO_ERROR_ARGUMENT_OUT_OF_RANGE: exception = mono_exception_new_argument_out_of_range(error->first_argument, error->full_message, error_out); break; case MONO_ERROR_NOT_VERIFIABLE: if (error->exn.klass) { type_name = mono_type_get_full_name (error->exn.klass); if (!type_name) goto out_of_memory; } message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message); if (!message) goto out_of_memory; exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message, error_out); break; case MONO_ERROR_GENERIC: if (!error->exception_name_space || !error->exception_name) mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied"); else exception = mono_exception_new_by_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message, error_out); break; case MONO_ERROR_EXCEPTION_INSTANCE: exception = MONO_HANDLE_CAST (MonoException, mono_gchandle_get_target_handle (error->exn.instance_handle)); break; case MONO_ERROR_CLEANUP_CALLED_SENTINEL: mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup"); break; case MONO_ERROR_INVALID_PROGRAM: exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", (error->flags & MONO_ERROR_INCOMPLETE) ? "" : error->full_message, error_out); break; default: mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code); } if (!mono_error_ok (error_out)) goto return_null; if (MONO_HANDLE_IS_NULL (exception)) mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); goto exit; out_of_memory: mono_error_set_out_of_memory (error_out, "Could not allocate message"); goto exit; return_null: exception = MONO_HANDLE_CAST (MonoException, mono_new_null ()); exit: g_free (message); g_free (type_name); HANDLE_FUNCTION_RETURN_OBJ (exception); }
MonoExceptionHandle mono_exception_new_invalid_operation (const char *msg, MonoError *error) { return mono_exception_new_by_name_msg (mono_get_corlib (), "System", "InvalidOperationException", msg, error); }