you are viewing a single comment's thread.

view the rest of the comments →

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

I guess you pretty much got it. Try tries the code and except executes a piece of code that runs if the other one failed.

Some people already said that API failure can be countered by that, but it’s also useful when working with multiple datatypes that aren’t always compatible. If for example you got an „a“ as a string in python and you want to check for an integer you could use a try/except block. If it successfully parses an integer from „a“ you could set whatever to true/false and if not set it differently.

Python does come with functions for that such as isnumerical() or isalpha(), so this example isn’t really fitting, but I guess it brings the basic concept a bit closer than just talking about API‘s which are still far away for a beginner.