Exceptions are difficult.
You should catch only those that you expect, and those that you expect, should be caught at the level of abstraction, which knows about the task that it is trying to accomplish, and not about the lower one, which simply knows about a small part of the task.
For example, you have a function that saves temporary files. This function calls functions that, for example, generate a temporary file name, save the file, and then return the path to the temporary file.
If the file save function shows that you gave it a file name that already exists in the directory, then this function should not work with it. He does not know about naming temporary files. The function you call to save the temporary file should probably handle this - it knows about the file names, and knew that you could create a duplicate. This way you can create a new name and try again.
function new_temp_file: try: name = generate_temp_name () save_temp_file (name) return name catch ExistingNameError: return new_temp_file ()
If you understand this above, then higher levels of abstraction know too much about lower levels.
This is just a simple example, but I hope you get what I am saying so ambiguously: catch the exception where the exception should be detected.
source share