What is try and except

what is try except and finally in python?

hello.
try block lets you test the block of code.
except block let you handle the error if occured i.e. if something wents wrong what to do.
and finally, finally lets you execute it’s code regardless of the result or circumstance of try and except block.
hope it helped

1 Like

Well, Python does not have a compiler. It uses interpreters or follows direct execution.
Now if your code generates some error then the execution is terminated.
To avoid this we use ‘try, except and finally block of statements’.
If the code in the “try” block has some errors, then instead of terminating the code execution the interpreter will execute the code in the “except” block.
And, in the end, it will execute the “finally” block regardless whether the error is generated or not
You can further refer to https://docs.python.org/3/tutorial/errors.html
It has an explanation with sober examples.

Hi
try and except is used for exception handling in python.If any error occur while executing your program it will terminate there itself.So to avoid termination of program we use try and except blocks in our program.try block consists of the code ,statements and except block consists of exceptions that need to be handled
for more information use this https://www.w3schools.com/python/python_try_except.asp
this may help you for better understanding
:slightly_smiling_face:

try and except is used for handling error in code.we give some condition in try block to test the code and show error with exceptions

The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.