Geeks With Blogs
Sean's Blog My Development Blog
I was recently working on an application that was known for throwing random exceptions at various times for no apparent reason.  Frustrated, I wrote this snippet of code to ease the tension.  I just thought I'd share it with you.

using System; using System.Collections.Generic; namespace RandomConsoleTests { class RandomConsoleTests { static void Main(string[] args) { ThrowRandomException(); } public static void ThrowRandomException() { throw ExceptionFactory.CreateRandomException(); } } }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Configuration.Provider; using System.Data; using System.IO.IsolatedStorage; using System.Net.Mail; namespace RandomConsoleTests { public static class ExceptionFactory { public static Exception CreateRandomException() { Random rand = new Random(); switch (rand.Next(0, 20)) { case 0: return new ApplicationException(); case 1: return new ProviderException(); case 2: return new SettingsPropertyIsReadOnlyException(); case 3: return new SettingsPropertyNotFoundException(); case 4: return new SettingsPropertyWrongTypeException(); case 5: return new IsolatedStorageException(); case 6: return new SmtpException(); case 7: return new SystemException(); case 8: return new AccessViolationException(); case 9: return new AppDomainUnloadedException(); case 10: return new ArgumentException(); case 11: return new ArithmeticException(); case 12: return new ArrayTypeMismatchException(); case 13: return new BadImageFormatException(); case 14: return new CannotUnloadAppDomainException(); case 15: return new KeyNotFoundException(); case 16: return new WarningException(); case 17: return new TimeoutException(); case 18: return new OutOfMemoryException(); case 19: return new DataException(); case 20: return new InvalidOperationException(); default: return new Exception(); } } } } Posted on Wednesday, April 23, 2008 2:50 PM | Back to top

Comments on this post: Random Exceptions

# re: Random Exceptions
Requesting Gravatar...
I love this. I would enhance it with custom exceptions.

Next time I need another exception, I will let you know.
Left by Sam on Apr 23, 2008 6:17 PM

# re: Random Exceptions
Requesting Gravatar...
Here's my take on more improved version of the factory. Some informative error messages are returned too!

public static class ExceptionFactory
public static Exception CreateRandomException()
Random rand = new Random();

List<string> head = new List<string>();
List<string> reason = new List<string>();
List<string> location = new List<string>();

head.Add("WARNING! Unexpected");
head.Add("FATAL! Priviledge violation:");
head.Add("DANGER! Resource unavailable,");

reason.Add("end of file encountered (no quack)");
reason.Add("boolean value file_not_found returned");
reason.Add("insufficient user rights to access Brillant Paula bean");

location.Add("at module 0xCAFEBABE.");
location.Add("at assembly loading.");
location.Add(string.Format("for handling {0}",

string msg = string.Format("{0} {1} {2}",
head[rand.Next(0, head.Count)],
reason[rand.Next(0, reason.Count)],
location[rand.Next(0, location.Count)]

switch (rand.Next(0, 20))
case 0: return new ApplicationException(msg);
case 1:return new ProviderException(msg);
case 2: return new SettingsPropertyIsReadOnlyException(msg);
case 3: return new SettingsPropertyNotFoundException(msg);
case 4: return new SettingsPropertyWrongTypeException(msg);
case 5: return new IsolatedStorageException(msg);
case 6: return new SmtpException(msg);
case 7: return new SystemException(msg);
case 8: return new AccessViolationException(msg);
case 9: return new AppDomainUnloadedException(msg);
case 10: return new ArgumentException(msg);
case 11: return new ArithmeticException(msg);
case 12: return new ArrayTypeMismatchException(msg);
case 13: return new BadImageFormatException(msg);
case 14: return new CannotUnloadAppDomainException(msg);
case 15: return new KeyNotFoundException(msg);
case 16: return new WarningException(msg);
case 17: return new TimeoutException(msg);
case 18: return new OutOfMemoryException(msg);
case 19: return new DataException(msg);
case 20: return new InvalidOperationException(msg);
default: return new Exception(msg);
Left by vonPryz on Apr 24, 2008 3:32 AM

# re: Random Exceptions
Requesting Gravatar...
vonPryz, I love the error messages. Thanks!
Left by Sean on Apr 24, 2008 8:56 AM

# re: Random Exceptions
Requesting Gravatar...
Only 20??

You can use reflection to instantiate every possible exception type and return a lot more exceptions...LOL
Left by Sijin Joseph on Apr 25, 2008 2:20 PM

# re: Random Exceptions
Requesting Gravatar...
Sijin, a friend of mine gave me the same suggestion, but I just never bothered to update it. If I have time this weekend, I'll make the update.

Thanks for the suggestion!
Left by Sean on Apr 25, 2008 2:45 PM

# re: Random Exceptions
Requesting Gravatar...
This is innovation at its best... I think we can make it more configurable by following a Windows workflow design and by making use of reflection and finally make sure that the person maintaining the application with this piece of code never finds this. lol. Great one!!!!
Left by Kaushik R on Apr 27, 2008 12:11 PM

# re: Random Exceptions
Requesting Gravatar...
What is worse, the fact that you have nothing better to do with your time or the fact that I took the time to read this?
Left by Kevin on Apr 30, 2008 10:14 PM

Your comment:
 (will show your gravatar)

Copyright © Sean Fao | Powered by: | Join free