Basic Python - Error handling

avatar

NaN years ago

Computers are so popular because of one reason, it doesn't make mistakes however programmers make mistakes therefore every programmer should know how to handle errors. Some error can be prevented if programmer can predict problems but most times an application fails because of excepted actions done by users and natural causes. Python have three simple statements to handle errors.


Try block: lets you test a block of code for errors.

Except block: lets you handle the error.

Finally block: lets you execute code, regardless of the result of the try- and except blocks.


Syntax:

try:
    # Some Code.... 
except:
    # optional block
    # Handling of all exception (if required)
except NameError:
    # optional block
    # Handling of exception according to error
else:
    # execute if no exception
finally:
    # Some code .....(always executed)
approvedApproved by experts
Advance Topics

Neuton's Mission

We are a team of young and enthusiastic people who are passionate about education and want to help students to learn the skills they need to succeed in life. If you want to support us, please join our community.

phonephonephonephonemail