# Code Smell 165 - Empty Exception Blocks

*On Error resume next was the first thing I learned in my first job*

> TL;DR: Don't avoid exceptions. Handle Them.

# Problems

- [Fail Fast](https://maximilianocontieri.com/fail-fast) Principle Violation

# Solutions

1. Catch the exception and deal with it explicitly

# Context

On early programming days, we privileged the systems running before error handling.

We have evolved.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/9569c95218a7d8a33d68587fa70e5782)
```python
# bad
import logging

def send_email(): 
  print("Sending email") 
  raise ConnectionError("Oops")
  
try:
  send_email() 
except: 
  # AVOID THIS
pass
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/02d281247e7baac4d5dca91bc77a146a)
```python
import logging

logger logging.getLogger(__name___)
try:
  send_email()
except ConnectionError as exc:
  logger.error(f"Cannot send email {exc}")
```

# Detection

[X] Automatic 
  
Many linters warn us on empty exception blocks

# Exceptions

If we need to skip and ignore the exception, we should document it explicitly.

# Tags

- Exceptions

# Conclusion

Prepare to deal with the errors. 

Even if you decide to do nothing, you should be explicit with this decision.

# Relations

%[https://maximilianocontieri.com/code-smell-132-exception-try-too-broad]

# More Info

%[https://maximilianocontieri.com/fail-fast]

[On Error Resume Next Package](https://www.npmjs.com/package/on-error-resume-next)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [James Best](https://unsplash.com/@jim_at_jibba) on [Unsplash](https://unsplash.com/)
  
Thank you @[Jan Giacomelli](@jangia)

%[https://twitter.com/jangiacomelli/status/1571126817322602496]

* * *

> Optimization hinders evolution. Everything should be built top-down, except the first time. Simplicity does not precede complexity, but follows it.

_Alan Perlis_
 
%[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]
