Handling errors and exceptions

Python follows the EAFP (short for easier to ask for forgiveness than permission) style of coding as opposed to the LBYL (look before you leap) style that is followed by most programming languages.

Therefore, handling exceptions in a way that is similar to the following one is normally cleaner in Python than checking conditions using the if-then block.

So when coding in Python, rather than using the following style in coding:

if some_thing_wrong:  do_something_else()else:  do_something_normal()

Consider using this instead:

try:  do_some_thing_normal()except some_thing_wrong:  do_some_thing_else()

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.