Catching All Exceptions Will Break Your Code // Python Tips

preview_player
Показать описание


👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Sybren A. Stüvel
- Dale Hagglund

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

In some contexts I think it can be ok to catch all exceptions as long as you are aware that it is doing so and propagate the error to the logger. For example, if you have a rest api that encounters an unexpected error you likely do not want the application exit with an exception so it can continue to accept requests. Instead, you may want to catch all exceptions, log the error and pass the error back to the client without raising the exception.

mikesmith
Автор

On the contrary i think it's good sometime to catch all error because most of the time you don't know what type of error you are expecting.

Idk why people are so against the idea of catching all erros.

techlogger
Автор

This is good advice for most situations, including the one in the video. However, the video title makes it sound like you should *never ever* catch all exceptions.

If you have a program that's running as a service on a server and it's important that it will never crash, wrapping your main function in a try block that catches everything is okay as long as you log the errors somewhere. Maybe even notify an administrator via e-mail that something happened.

Betacak
Автор

Push Back ::

I've seen this advise given a lot, but I want to give push back for times when I actually use this effectively. But I'd love your input on if there's a better way I could be handling exceptions.

I have a user request listener that takes in a user request, and then runs a ton of calculations on the user's investment portfolio. Within the portfolio, most anticipated errors should all be handled appropriately. However, at the top level listener, since any type or error could come back unexpectedly I do:
portfolio = listener.get_request()
try:
portfolio.solve()
except KeyboardInterruptError:
exit(1)
except Exception as e:
logger.error(e)
continue

This keeps the listener up without crashing even if some error gets through that I didn't anticipate. And also allows me to test new code on a huge batch of portfolios at the same time. If 1% of them are throwing an error that I didn't anticipate, I want to log that, but I still want to run the rest of them to check whatever new calculation I'm working on. Once I see all the errors that haven't gotten caught at the appropriate level, I can go back and address them, but I don't risk having the whole listener shut down because I didn't anticipate one type of error.

The keyboard interrupt is just so that I can still exit if I need to, if I see something is going terribly wrong.

Thoughts?

thejedijohn
Автор

we call this pokémon exception handling...

(gotta catch 'em all, u know?)

MCRuCr
Автор

how do you find out what types of exceptions there are, to be able to catch them?

zenmaster
Автор

Hi Arjan can you share your VSCode Python linting options?

iyadahmed
Автор

If you are writing an application that needs 100% uptime, like a web application, or some other type of app that might use web sockets or such, you really need to catch all exceptions and deal with them in some fashion. If you do not, and someone meaning you harm finds an exception that will cause an unhandled error, they will create a script to run that process over and over again to keep bringing your application down, even if you attempt to restart it. Global exception handlers are critical for application uptime and availability, which is a cornerstone of application security.

kickthesky
Автор

In the example that you provided (NaN), would it not be better to have the input in a while loop to enter the number rather than using try/catch?

lechprotean
Автор

Please i want to learn exceptions for an implementarion of an apu

mauriciosanchez
Автор

Rule: Usually, you should catch as narrowly as possible.

ncmathsadist
Автор

For example, you might catch the COVID-19

SirSidi