Exception Handling

Mono's exception handling contains methods to create `MonoException*` objects that can be raised.

Alternatively, you can obtain an exception that you can raise from some of the most common exceptions in the .NET Runtime.

Raising and Catching exceptions

If you plan on running your code in Mono's Cooperative mode for the Garbage Collector (for example, if you are using pure Bitcode code generation), it you should avoid raising an exception from any method that is not the entry point to the internal call.

It is recommended that if you need to raise an error condition from nested parts of your code, surface this error to the topmost method that is surfaced as an internal call and raise the exception there.

mono_raise_exception
Syntax
void mono_raise_exception (MonoException *ex)

Parameters
ex exception object
Description

Signal the runtime that the exception ex has been raised in unmanaged code.

mono_unhandled_exception
Syntax
void mono_unhandled_exception (MonoObject *exc)

Parameters
exc exception thrown
Description

This is a VM internal routine.

We call this function when we detect an unhandled exception in the default domain.

It invokes the * UnhandledException event in AppDomain or prints a warning to the console

mono_print_unhandled_exception
Syntax
void mono_print_unhandled_exception (MonoObject *exc)

Parameters
exc The exception
Description

Prints the unhandled exception.

Exception Types: General API

mono_exception_from_name_domain
Syntax
MonoException* mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, const char* name_space, const char *name)

Parameters
domain Domain where the return object will be created.
image the Mono image where to look for the class
name_space the namespace for the class
name class name
Return value
the initialized exception instance.
Description

Creates an exception object of the given namespace/name class on the given domain.

mono_exception_from_name
Syntax
MonoException* mono_exception_from_name (MonoImage *image, const char *name_space, const char *name)

Parameters
image the Mono image where to look for the class
name_space the namespace for the class
name class name
Return value
the initialized exception instance.
Description

Creates an exception of the given namespace/name class in the current domain.

mono_exception_from_name_msg
Syntax
MonoException* mono_exception_from_name_msg (MonoImage *image, const char *name_space, const char *name, const char *msg)

Parameters
image the Mono image where to look for the class
name_space the namespace for the class
name class name
msg the message to embed inside the exception
Return value
the initialized exception instance.
Description

Creates an exception and initializes its message field.

mono_exception_from_name_two_strings
Syntax
MonoException* mono_exception_from_name_two_strings (MonoImage *image, const char *name_space, const char *name, MonoString *a1, MonoString *a2)

Parameters
image the Mono image where to look for the class
name_space the namespace for the class
name class name
a1 first string argument to pass
a2 second string argument to pass
Return value
the initialized exception instance.
Description

Creates an exception from a constructor that takes two string arguments.

Obtaining Common Exceptions

There are a number of common exceptions that are used by the runtime, use the routines in this section to get a copy of those exceptions.

mono_get_exception_appdomain_unloaded
Syntax
MonoException* mono_get_exception_appdomain_unloaded (void)

Return value
a new instance of the System.AppDomainUnloadedException
Description

mono_get_exception_argument
Syntax
MonoException* mono_get_exception_argument (const char *arg, const char *msg)

Parameters
arg the name of the invalid argument.
Return value
a new instance of the System.ArgumentException
Description

mono_get_exception_argument_null
Syntax
MonoException* mono_get_exception_argument_null (const char *arg)

Parameters
arg the name of the argument that is null
Return value
a new instance of the System.ArgumentNullException
Description

mono_get_exception_argument_out_of_range
Syntax
MonoException* mono_get_exception_argument_out_of_range (const char *arg)

Parameters
arg the name of the out of range argument.
Return value
a new instance of the System.ArgumentOutOfRangeException
Description

mono_get_exception_arithmetic
Syntax
MonoException* mono_get_exception_arithmetic ()

Return value
a new instance of the System.ArithmeticException
Description

mono_get_exception_array_type_mismatch
Syntax
MonoException* mono_get_exception_array_type_mismatch ()

Return value
a new instance of the System.ArrayTypeMismatchException
Description

mono_get_exception_bad_image_format
Syntax
MonoException* mono_get_exception_bad_image_format (const char *msg)

Parameters
msg an informative message for the user.
Return value
a new instance of the System.BadImageFormatException
Description

mono_get_exception_cannot_unload_appdomain
Syntax
MonoException* mono_get_exception_cannot_unload_appdomain (const char *msg)

Parameters
inner the inner exception.
Return value
a new instance of the System.CannotUnloadAppDomainException
Description

mono_get_exception_class
Syntax
MonoClass* mono_get_exception_class (void)

Return value
The MonoClass* for the `` type.
Description

Use this function to get the MonoClass* that the runtime is using for System.Exception.

mono_get_exception_divide_by_zero
Syntax
MonoException* mono_get_exception_divide_by_zero ()

Return value
a new instance of the System.DivideByZeroException
Description

mono_get_exception_execution_engine
Syntax
MonoException* mono_get_exception_execution_engine (const char *msg)

Parameters
msg the message to pass to the user
Return value
a new instance of the System.ExecutionEngineException
Description

mono_get_exception_file_not_found2
Syntax
MonoException* mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)

Parameters
msg an informative message for the user.
fname the name of the file not found.
Return value
a new instance of the System.IO.FileNotFoundException
Description

mono_get_exception_file_not_found
Syntax
MonoException* mono_get_exception_file_not_found (MonoString *fname)

Parameters
fname the name of the file not found.
Return value
a new instance of the System.IO.FileNotFoundException
Description

mono_get_exception_index_out_of_range
Syntax
MonoException* mono_get_exception_index_out_of_range ()

Return value
a new instance of the System.IndexOutOfRangeException
Description

mono_get_exception_invalid_cast
Syntax
MonoException* mono_get_exception_invalid_cast ()

Return value
a new instance of the System.InvalidCastException
Description

mono_get_exception_io
Syntax
MonoException* mono_get_exception_io (const char *msg)

Parameters
msg the message to present to the user
Return value
a new instance of the System.IO.IOException
Description

mono_get_exception_missing_method
Syntax
MonoException* mono_get_exception_missing_method (const char *class_name, const char *member_name)

Parameters
class_name the class where the lookup was performed.
member_name the name of the missing method.
Return value
a new instance of the System.MissingMethodException
Description

mono_get_exception_not_implemented
Syntax
MonoException* mono_get_exception_not_implemented (const char *msg)

Parameters
msg the message to pass to the user
Return value
a new instance of the System.NotImplementedException
Description

mono_get_exception_null_reference
Syntax
MonoException* mono_get_exception_null_reference ()

Return value
a new instance of the System.NullReferenceException
Description

mono_get_exception_overflow
Syntax
MonoException* mono_get_exception_overflow ()

Return value
a new instance of the System.OverflowException
Description

mono_get_exception_security
Syntax
MonoException* mono_get_exception_security ()

Return value
a new instance of the System.Security.SecurityException
Description

mono_get_exception_serialization
Syntax
MonoException* mono_get_exception_serialization (const char *msg)

Parameters
msg the message to pass to the user
Return value
a new instance of the System.Runtime.Serialization.SerializationException
Description

mono_get_exception_stack_overflow
Syntax
MonoException* mono_get_exception_stack_overflow (void)

Return value
a new instance of the System.StackOverflowException
Description

mono_get_exception_synchronization_lock
Syntax
MonoException* mono_get_exception_synchronization_lock (const char *msg)

Parameters
inner the inner exception.
Return value
a new instance of the System.SynchronizationLockException
Description

mono_get_exception_thread_abort
Syntax
MonoException* mono_get_exception_thread_abort ()

Return value
a new instance of the System.Threading.ThreadAbortException
Description

mono_get_exception_thread_state
Syntax
MonoException* mono_get_exception_thread_state (const char *msg)

Parameters
msg the message to present to the user
Return value
a new instance of the System.Threading.ThreadStateException
Description

mono_get_exception_type_initialization
Syntax
MonoException* mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)

Parameters
type_name the name of the type that failed to initialize.
inner the inner exception.
Return value
a new instance of the System.TypeInitializationException
Description

mono_get_exception_type_load
Syntax
MonoException* mono_get_exception_type_load (MonoString *class_name, char *assembly_name)

Parameters
class_name the name of the class that could not be loaded
assembly_name the assembly where the class was looked up.
Return value
a new instance of the System.TypeLoadException
Description

mono_get_exception_invalid_operation
Syntax
MonoException* mono_get_exception_invalid_operation (const char *msg)

Parameters
msg the message to pass to the user
Return value
a new instance of the System.InvalidOperationException
Description

mono_get_exception_missing_field
Syntax
MonoException* mono_get_exception_missing_field (const char *class_name, const char *member_name)

Parameters
class_name the class where the lookup was performed
member_name the name of the missing method.
Return value
a new instance of the System.MissingFieldException
Description

mono_get_exception_not_supported
Syntax
MonoException* mono_get_exception_not_supported (const char *msg)

Parameters
msg the message to pass to the user
Return value
a new instance of the System.NotSupportedException
Description

mono_get_exception_reflection_type_load
Syntax
MonoException* mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)

Parameters
types an array of types that were defined in the moduled loaded.
exceptions an array of exceptions that were thrown during the type loading.
Return value
a new instance of the System.Reflection.ReflectionTypeLoadException
Description

mono_exception_from_token_two_strings
Syntax
MonoException* mono_exception_from_token_two_strings (MonoImage *image, guint32 token, MonoString *a1, MonoString *a2)

Description

Same as mono_exception_from_name_two_strings, but lookup the exception class using IMAGE and TOKEN.

mono_get_exception_bad_image_format2
Syntax
MonoException* mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)

Parameters
msg an informative message for the user.
fname The full name of the file with the invalid image.
Return value
a new instance of the System.BadImageFormatException
Description

mono_get_exception_field_access
Syntax
MonoException* mono_get_exception_field_access (void)

Return value
a new instance of the System.FieldAccessException
Description

mono_get_exception_method_access
Syntax
MonoException* mono_get_exception_method_access (void)

Return value
a new instance of the System.MethodAccessException
Description

mono_get_exception_out_of_memory
Syntax
MonoException* mono_get_exception_out_of_memory (void)

Return value
a new instance of the System.OutOfMemoryException
Description

mono_get_exception_runtime_wrapped
Syntax
mono_get_exception_runtime_wrapped