+ 1
Can someone explain about the "try" and "except"?
I'm still very confused about "try" can anyone explain?
2 Antworten
0
Try is simply a block of which you want to execute. When a block of code in try is executed it is executed normally. Except is used when an error occurs in try block. Like
try:
Print(1/0)
In above code, you will get a ZeroDivisionError. That's when except block comes to picture.
try:
print(1/0)
except:
print("Cannot divide by zero")
Here output will not be some weird error message but the string which we have specified. You can even use any other block of code rather than just printing a message.
And "finally" is the keyword when you want to execute a block after end of try and except.
In short they is block of code which you think will throw an error, except is when the error has occurred and finally is after complete execution of try and except
+ 1
And also about "finally"