How to intentionally crash an application with an AccessViolationException in c#?
I have a console application that use an unmanaged DLL that eventually crashes because of access violation exceptions. Because of that, I need to intentionally throw an AccessViolationException
to test how it behaves under certain circumstances.
Besides that, it must be specifically an AccessViolationException
because this exception is not handled by the catch blocks.
Surprisingly, this does not work:
public static void Crash()
{
throw new AccessViolationException();
}
Neither this:
public static unsafe void Crash()
{
for (int i = 1; true; i++)
{
var ptr = (int*)i;
int value = *ptr;
}
}
try/catch
blocks (and other exception-handling clauses) are not invoked for them. If you are absolutely sure that you want to maintain your handling of these exceptions, you must apply theHandleProcessCorruptedStateExceptionsAttribute
attribute to the method whose exception-handling clauses you want to execute – Almazan*(int*)0x10000 = 0;
to generate an AVE instead of a NRE, addresses below 0x10000 are assumed to be caused by null pointers. – Parahydrogen