Error Handling

In Python, if the program is syntactically correct but results in error generally is exception error like dividing by zero will leads to exception error.

We have built-in exception error and we can also raise the error using “raise an exception”. We use to try and except block to raise an error. Try block contains the part to be executed. If it is having some error like undefined variable then exception block will throw an error.

We will see the error handling with examples -:

Checking error using exception

Using try and except block

How to raise a custom exception.

Suppose we are writing one usable program and in that, I want that variable not to be of the int data type. So, by taking that example only we are raising the exception.

Hope this gives you the idea of how to check the error and handle it using try and exception block.

Stay tuned! Keep learning with us. Enjoy learning Python.

Close Menu