# Code Smell 132 - Exception Try Too Broad

> TL;DR: Be as specific as possible when handling errors.

# Problems 

- Fail fast principle violation

- Missing errors 

- False negatives

# Solutions

1. Narrow the exception handler as much as possible 

# Sample Code

## Wrong 

[Gist Url]: # (https://gist.github.com/mcsee/0ff3c46988812be98da1e256b54c00d1)
```python
import calendar, datetime
try: 
    birthYear= input('Birth year:')
    birthMonth= input('Birth month:')
    birthDay= 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(birthYear) + ' 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

[Gist Url]: # (https://gist.github.com/mcsee/0d7e270416ebc934fbfbe8934175e52c)
```python
import calendar, datetime

birthYear= input('Birth year:')
birthMonth= input('Birth month:')
birthDay= input('Birth day:')
# try scope should be narrow
try: 
    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(birthYear) + ' 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 must make exceptions as surgical as possible.

# Relations

%[https://maximilianocontieri.com/code-smell-26-exceptions-polluting]

%[https://maximilianocontieri.com/code-smell-73-exceptions-for-expected-cases]

%[https://maximilianocontieri.com/code-smell-72-return-codes]

# Credits

Photon from [Jakob Braun](https://unsplash.com/es/fotos/Js2Tv3-uLB8) on 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
