I need a code that gives the error AccessViolationException

2

My teacher has passed a series of exercises and in them I need to create a code to give the error AccessViolationException . But I am not able to create a code that from this error (I do not want to do the error handling).

I need something to insert inside try .

try{
    //código que retorna o erro 
}catch(AccessViolationException ex){
    Console.WriteLine(ex);
}
    
asked by anonymous 19.09.2015 / 20:03

2 answers

0

The code below consistently generates an exception of type AccessViolationException :

var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);

That said, @bigown's answer is correct. There is no aspect of throw new AccessViolationException() that behaves differently.

Source: How to test handling of AccessViolationException

    
24.09.2015 / 15:22
4
É simples:

try {
    throw new AccessViolationException();
} catch (AccessViolationException ex){
    Console.WriteLine(ex);
}

See running on dotNetFiddle .

I was not in the question that I could not use throw . These fictional rules are no good. The only utility I see to simulate the error is to do tests and this can be done with throw .

There is a question in the SO that deals with this with the answer of someone who understands well, but I do not know if it works.

    
19.09.2015 / 20:30