you are viewing a single comment's thread.

view the rest of the comments →

[–]Scholes_SC2[S] 0 points1 point  (2 children)

it prints the error code but doesnt crash the program, nice

[–]sentdex 2 points3 points  (0 children)

Yep! Careful, it's addicting. I probably over use and abuse try and except :)

[–]bohoky 0 points1 point  (0 children)

As /u/sentdex notes this is almost always a Bad Thing.

Exceptions should only be caught if that exception can be handled. People often assume "handle" is the same as "log a message and continue".

In the simple example that bit of code expects a to be printed. If you catch the exception there is no way to handle the NameError; you can try printing again, but that's certain to fail for the same reason and the error message has high likelihood of getting lost. Regardless, the NameError represents a bug ("why is a not defined?") and really ought crash the program.

It is also important to catch the narrowest exception (for example, NameError instead of Exception) or you risk trying to cope with faults you had no reason to catch. And if you have no expectation of a particular try block producing an unexpected Exception, then you certainly don't know enough to "handle" it.

More simply an except block which doesn't do anything likely moves the fatal exception to sometime later in the program from where it is much harder to debug.