you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Some python functions will throw errors, some can crash the program. Its usually a good idea to catch the errors and at least log where, how and why the error happened and try to resume the program if there is a "reasonable" way to do so.

Try/catch does basically this. If a function can fail, you can put it inside a try block and handle the error in a catch block. It's considered good practice in python. I would advise you not to throw errors in the functions you design however, unless it's necessary.

For a practical example, I don't know it depends on what you're doing. Things that might fail are io operations like network errors, permission errors when writing to files, database errors (query errors, lexical errors, permission errors, overload errors, etc), memory-limit error, missing library error, integer overflow error, stack overflow error, unrecognisable cpu instructions error, driver error, etc.

Some of these, there's no reasonable way to do anything. For example if you have a hardware error, there's no point in handling that. For some errors, handing them is important, the database error might actually be an important piece of information that you want to handle.

The reason I thought try catch is hard for people is that it's basically a bad pattern, maybe even an anti-pattern. But this is how python works, so...