The Daily WTF
Follow
CodeSOD: Exceptional Control
Sebastian encountered an excessive amount of error handling code in his new job, with around 70% of a 1,000-line class dedicated to it. This code throws exceptions for contract violations, despite the preferable option of enforcing them at compile time.The code catches various exceptions and returns null, including the general Exception class. This practice is discouraged as it obscures the specific cause of the error.The calling function exacerbates the issue by throwing another exception if Method1 returns null, leading to a final return value of false.This error handling approach essentially uses exceptions for flow control, which is considered an anti-pattern.The specific code example provided in the text demonstrates the use of a try block to throw exceptions that are immediately caught and result in a null return value.This unnecessary exception handling could have been replaced with simple return statements.The author suggests that the excessive error handling is unwarranted and could be simplified.The provided code snippet demonstrates that the exceptions are not actually providing any meaningful error handling or flow control.Overall, the text highlights the pitfalls of excessive and unnecessary error handling code.