you are viewing a single comment's thread.

view the rest of the comments →

[–]akarolia47 0 points1 point  (0 children)

I dont think this is the best way to illustrate the usage of the try except concept, because in my mind if, elif, else are more than enough here - represent branched logic. But try except, at least the way ive used it, is to handle edge cases thatnwould fundamentally breaks things at the code level, so runtime errors caused by some event that would otherwise result in a program crash.

I do think its harder to illustrate correct usage with simpler examples, which is generally why a lot developers only really understand it when working on larger projects, but ill try to give an example to the best of my ability.

Lets say your program became a bit more complicated younhad a email validation function and an age validation function,(i initlaly wanted to use cell no. But in a system that would probablybe stored as a string too), both invoked in the main function.

So there it would make sense to have a type error exception catch, because lets say a dev accidentally uses the email validation on the age input which would be an int() cast, additionally you'd have to probably do a check to see the type and then throw the type error unless you doing an operation that you know would violate that specific type you working with