An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
May 18, 2022 02:47 am GMT
Original Link: https://dev.to/mcsee/code-smell-132-exception-try-too-broad-3f8n
Code Smell 132 - Exception Try Too Broad
Exceptions are handy. But should be as narrow as possible
TL;DR: Be as specific as possible when handling errors.
Problems
Fail fast principle violation
Missing errors
False negatives
Solutions
- Narrow the exception handler as much as possible
Sample Code
Wrong
import calendar, datetimetry: birthYear= int(input('Birth year:')) birthMonth= int(input('Birth month:')) birthDay= int(input('Birth day:')) #we don't expect the above to fail print(datetime.date(int(birthYear), int(birthMonth), int(birthDay)))except ValueError as e: if str(e) == 'month must be in 1..12': print('Month ' + str(birthMonth) + ' is out of range. The month must be a number in 1...12') elif str(e) == 'year {0} is out of range'.format(birthYear): print('Year ' + str(birthMonth) + ' is out of range. The year must be a number in ' + str(datetime.MINYEAR) + '...' + str(datetime.MAXYEAR)) elif str(e) == 'day is out of range for month': print('Day ' + str(birthDay) + ' is out of range. The day must be a number in 1...' + str(calendar.monthrange(birthYear, birthMonth)))
Right
import calendar, datetimebirthYear= int(input('Birth year:'))birthMonth= int(input('Birth month:'))birthDay= int(input('Birth day:'))# try scope should be narrowtry: print(datetime.date(int(birthYear), int(birthMonth), int(birthDay)))except ValueError as e: if str(e) == 'month must be in 1..12': print('Month ' + str(birthMonth) + ' is out of range. The month must be a number in 1...12') elif str(e) == 'year {0} is out of range'.format(birthYear): print('Year ' + str(birthMonth) + ' is out of range. The year must be a number in ' + str(datetime.MINYEAR) + '...' + str(datetime.MAXYEAR)) elif str(e) == 'day is out of range for month': print('Day ' + str(birthDay) + ' is out of range. The day must be a number in 1...' + str(calendar.monthrange(birthYear, birthMonth)))
Detection
[X] Manual
If we have a good enough test suite, we can perform mutation testing to narrow the exception scope as much as possible.
Tags
- Exceptions
Conclusion
We should exceptions as surgical as possible.
Relations
Code Smell 26 - Exceptions Polluting
Maxi Contieri Nov 16 '20 2 min read
#oop #exceptions #tutorial #programming
Credits
Foto de Jakob Braun en Unsplash
The primary duty of an exception handler is to get the error out of the lap of the programmer and into the surprised face of the user.
Verity Stob
Software Engineering Great Quotes
Maxi Contieri Dec 28 '20 13 min read
#codenewbie #programming #quotes #software
This article is part of the CodeSmell Series.
Original Link: https://dev.to/mcsee/code-smell-132-exception-try-too-broad-3f8n
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To