Can someone explain the try and except functions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain the try and except functions?

Could someone please explain in detail the purpose and practical uses of the try and except functions? Thanks:)

12th Sep 2021, 7:24 PM
MajesticPlatypus
MajesticPlatypus - avatar
2 Answers
+ 3
The idea is to catch errors so the code can continue. They are most often used when user input is required so that if the user gives bad input, it wont crash the code. I have also used it when a feature is supported on some devices but not others. That allows the rest of the code to be used without giving error. Be careful while using however, it can cause you to miss some very fatal bugs
12th Sep 2021, 8:00 PM
Brain & Bones
Brain & Bones - avatar
+ 2
You can call it "Block Based Code Validator" A block of code written in try statement, if any type of error occurred while running the try block it jumps to the except block and execute it The third statement is "finally" that used the end of try except For example: try: myInput = 123 "XYZ" print(myInput) except: print("Error occured in try statement ") finally: print("Finish") In try block error occurs, then output will be Output: Error occured in try statement Finish
12th Sep 2021, 9:20 PM
Tahir Iqbal
Tahir Iqbal - avatar