Article 3FD0B CodeSOD: I Take Exception

CodeSOD: I Take Exception

by
snoofle
from The Daily WTF on (#3FD0B)

We've all seen code that ignores errors. We've all seen code that simply rethrows an exception. We've all seen code that wraps one exception for another. The submitter, Mr. O, took exception to this exceptionally exceptional exception handling code.

I was particularly amused by the OutOfMemoryException handler that allocates another exception object, and if it fails, another layer of exception trapping catches that and attempts to allocate yet another exception object. if that fails, it doesn't even try. So that makes this an exceptionally unexceptional exception handler?! (ouch, my head hurts)

It contains a modest amount of fairly straightforward code to read config files and write assorted XML documents. And it handles exceptions in all of the above ways.

You might note that the exception handling code was unformatted, unaligned and substantially larger than the code it is attempting to protect. To help you out, I've stripped out the fairly straightforward code being protected, and formatted the exception handling code to make it easier to see this exceptional piece of code (you may need to go full-screen to get the full impact).

After all, it's not like exceptions can contain explanatory text, or stack context information...

namespace HotfolderMerger { public class Merger : IDisposable { public Merger() { try { object section = ConfigurationManager.GetSection("HFMSettings/DataSettings"); if (section == null) throw new MergerSetupException(); _settings = (DataSettings)section; } catch (MergerSetupException) { throw; } catch (ConfigurationErrorsException ex){ throw new MergerSetupException("Error in configuration", ex); } catch (Exception ex) { throw new MergerSetupException("Unexpected error while loading configuration",ex); } } // A whole bunch of regex about as complex as this one... private readonly Regex _fileNameRegex = new Regex(@"^(?<System>[A-Za-z0-9]{1,10})_(?<DesignName>[A-Za-z0-9]{1,})_(?<DocumentID>\d{1,})_(?<FileTimeUTC>\d{1,})(_(?<BAMID>\d+))?\.(?<extension>\w{0,3})$"); public void MergeFiles() { try { foreach (FileElement filElement in _settings.Filelist) { // Lots of declarations here... foreach (FileInfo fi in hotfolder.GetFiles()) { try { // 35 lines of innocuous code.. } catch (ArgumentException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergePreRunArgumentException), ErrorMessages.MergePreRunArgumentException); } catch (ConfigurationException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergePreRunConfigurationException),ErrorMessages.MergePreRunConfigurationException); } catch (Exception ex) { throw new UnexpectedMergerException("Unexpected exception while setting up for merge!", ex); } try { // 23 lines of StreamReader code to load some XML from a file... } catch (OutOfMemoryException ex) { // OP: so if we're out of memory, how is this new exception going to be allocated? // Maybe in the wrapping "try/catch Exception" - which allocates a new UnexpectedMergerException object??? Oh, wait... throw new BasisException( ex,int.Parse(ErrorCodes.MergeRunOutOfMemoryException), ErrorMessages.MergeRunOutOfMemoryException); } catch (ConfigurationException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunConfigurationException),ErrorMessages.MergeRunConfigurationException); } catch (FormatException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunFormatException), ErrorMessages.MergeRunFormatException); } catch (ArgumentException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunArgumentException), ErrorMessages.MergeRunArgumentException); } catch (SecurityException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunSecurityException), ErrorMessages.MergeRunSecurityException); } catch (IOException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunIOException), ErrorMessages.MergeRunIOException); } catch (NotSupportedException ex) { throw new BasisException( ex, int.Parse(ErrorCodes.MergeRunNotSupportedException), ErrorMessages.MergeRunNotSupportedException); } catch (Exception ex) { throw new UnexpectedMergerException("Unexpected exception while merging!", ex); } } // ... } } catch (UnexpectedMergerException) { throw; } catch (BasisException ex) { BasisLog.WriteError(ex); } catch (Exception ex) { throw new UnexpectedMergerException("Unexpected error while attempting to parse settings prior to merge", ex); } } private static void prepareNewMergeFile(ref XmlTextWriter xtw, string filename, int numfiles) { if (string.IsNullOrEmpty(filename)) throw new BasisException( int.Parse(ErrorCodes.MergeSetupNullReferenceException), ErrorMessages.MergeSetupNullReferenceException, "filename parameter was null or empty"); try { // Use XmlTextWriter to concatenate ~30 lines of canned XML... } catch (InvalidOperationException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeSetupInvalidOperationException), ErrorMessages.MergeSetupInvalidOperationException); } catch (ArgumentException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeSetupArgumentException), ErrorMessages.MergeSetupArgumentException); } catch (IOException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeSetupIOException), ErrorMessages.MergeSetupIOException); } catch (UnauthorizedAccessException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeSetupUnauthorizedAccessException), ErrorMessages.MergeSetupUnauthorizedAccessException); } catch (SecurityException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeSetupSecurityException), ErrorMessages.MergeSetupSecurityException); } catch (Exception ex) { throw new UnexpectedMergerException("Unexpected exception while setting up for merge!", ex); } } private void closeMergeFile(ref XmlTextWriter xtw, ref List<FileInfo> filesComplete, string filename, double i) { if (xtw == null) throw new BasisException(int.Parse(ErrorCodes.MergeCleanupNullReferenceException), ErrorMessages.MergeCleanupNullReferenceException, "xtw ref parameter was null"); if (filesComplete == null) throw new BasisException(int.Parse(ErrorCodes.MergeCleanupNullReferenceException), ErrorMessages.MergeSetupNullReferenceException, "filesComplete ref parameter was null"); if (string.IsNullOrEmpty(filename)) throw new BasisException(int.Parse(ErrorCodes.MergeCleanupNullReferenceException), ErrorMessages.MergeSetupNullReferenceException, "filename parameter was null or empty"); try { // ~ 30 lines of XmlTextWriter, StreamWriter and File IO... } catch (ArgumentException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupArgumentException), ErrorMessages.MergeCleanupArgumentException); } catch (InvalidOperationException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupInvalidOperationException), ErrorMessages.MergeCleanupInvalidOperationException); } catch (UnauthorizedAccessException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupUnauthorizedAccessException), ErrorMessages.MergeCleanupUnauthorizedAccessException); } catch (IOException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupIOException), ErrorMessages.MergeCleanupIOException); } catch (NullReferenceException ex) { throw new BasisException(int.Parse(ErrorCodes.MergeCleanupNullReferenceException), ErrorMessages.MergeCleanupNullReferenceException, "unknown exception details"); } catch (NotSupportedException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupNotSupportedException), ErrorMessages.MergeCleanupNotSupportedException); } catch (MergerException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupMergerException), ErrorMessages.MergeCleanupMergerException); } catch (SecurityException ex) { throw new BasisException(ex, int.Parse(ErrorCodes.MergeCleanupSecurityException), ErrorMessages.MergeCleanupSecurityException); } catch (Exception ex) { throw new UnexpectedMergerException("Unexpected exception while merging!", ex); } } }}
inedo50.png [Advertisement] Otter, ProGet, BuildMaster - robust, powerful, scalable, and reliable additions to your existing DevOps toolchain. TheDailyWtf?d=yIl2AUoC8zA65xm77iJjk4
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments