views:

269

answers:

5

Just curious as to how I can get this error the easiest way.

Once I was trying to create a file navigator and I was creating Image thumbnails; that turned out awful.

+2  A: 

Create a very, very large string. Probably:

string s = new string('a', int.MaxValue);

will be enough.

If not, you can concat it to build even bigger string.

string ss = string.Concat(s, s);
MainMa
+40  A: 
void Foo()
{
   throw new OutOfMemoryException();
}

:)))

Nagg
+1 - throwing it FTW
Russ Cam
obvious answer is obvioius.
Will
Haha you cheeky bastard. :D +1
Sergio Tapia
+1 technically correct - the *best kind* of correct
Wim Coenen
Why not throw it directly from `static void Main()`? Creating a whole new method is way too difficult and time consuming :)
Brian Gideon
+1  A: 

This does throw an OutOfMemoryException:

string s = " ";
while (true) s += s;
Guffa
A: 

Well, it is not the easiest way to get this error, but when dealing with images, it could cause this error, even if there is much memory free:

Confusingly, an OutOfMemoryException is thrown from the System.Drawing.Image.FromFile method when an image format or the pixel format of an image is not supported by the .net framework.

See: http://msdn.microsoft.com/en-gb/library/stf701f5.aspx

I don't know why there is no ImageFormatNotSupportedYetException ;(

terabaud
Interesting. If the right status code is returned from the GDI+ call then yes it could throw `OutOfMemoryException`, but its hard to tell what circumstances that would happen based on what I see in Reflector anyway.
Brian Gideon
A: 
for(var s=" ";;s+=s);
Mau