what is the point of the "TRY" and "EXCEPT" code? | SoloLearn: Learn to code for FREE!

0

what is the point of the "TRY" and "EXCEPT" code?

Im confused... so what is the main use for exceptions? It seems like you're just labeling errrors essentially. But arent you writing your code so that you dont have errors? Are you just trying to anticipate them? When is an exception code useful?

5/21/2020 9:46:22 PM

sam myung

4 Answers

New Answer

+2

An Exception is not only reporting an error. Eg.: You can say Exception is used to handle the wrong error entered by user. Here your code is right and error free but user may have entered invalid data.

+1

We're of course all writing perfect code (:P) but some errors are unavoidable. Like, you ask a user to enter a number between 0 and 5 and they pick 6. You download a file but the internet goes out right before you finish. Exceptions are one possible way to deal with abnormal outcomes like that. try: file = open("bla.txt", ...) except IOError: # could not open file for whatever reason Also, exceptions defer the onus of who has to handle an error. A program is just a function calling more functions calling more functions right? So a function may throw an exception if it sees fit, and an `except` block way up the calling hierarchy can handle the error which is pretty neat. The function producing the error does not necessarily need to know how to deal with it! But that's maybe getting a bit too theoretical, sorry if I am.

0

It is so that the program when getting an error does not break. The TRY tests a code and if there is a problem Except display a message but can follow the code. Check it my code. https://code.sololearn.com/cOtc2LDMbe85/?ref=app

0

Ty guys. That was all very helpful. I think I kinda realized the answer right after I wrote the question but your answers have made it more clear. Ty again!