Sean's Blog

My Development Blog

  Home  |   Contact  |   Syndication    |   Login
  11 Posts | 0 Stories | 44 Comments | 0 Trackbacks

News

Twitter












Archives

.NET Development

Wednesday, April 23, 2008 #

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.

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

ExceptionFactory.cs
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(); } } } }