Alex Rivera | Logout

C# how to generate a proper error quickly for testing purposes

Asked 2010-03-04T08:06:58.313
21

I have some code in my error handler I need to test against a realistic error. How can I generate an error that has a full stack trace and is just as realistic as a runtime error.

I am using a general catch on an exception. So do not need a specific type of exception in this particular case.

Thanks

c#
Edit
Report

3 Answers

11

You can throw any kind of .Net exception by calling

throw new OutOfMemoryException();
throw new IndexOutOfRangeException();
throw new ArgumentNullException();

The exception names can be any dotnet exceptions. you can even create and throw your own specific exceptions.

To get a list of the CLR exceptions, in VS click on the debug menu and then the exceptions menu.

answered 2010-03-04T08:22:16.687
7

One DivideByZeroException coming up!:

int i = 0;
int j = 1 / i;
answered 2010-03-04T08:09:17.513
-2

how about the good old division by zero?

5 / 0

answered 2010-03-04T13:39:20.517

Your Answer