In case you have started Python as a beginner, you might have got a lot of Tracebacks. These are generated in python due to runtime errors while applying it in data science.
In Python runtime errors are provided via exception handling method with the help of try-except. Some standard exceptions in frequent use are IndexError, ImportError, IOError, ZeroDivisionError, TypeError.
For all the exceptions in Python, Exception is the base class.
Now in the example below, we will access the array element whose index is out of bounds, and we will handle the corresponding exception.
In order to specify handlers for different exceptions, a try statement can have more than one except clause.
In case the value of ‘a’ is change to ‘a>=4’, we will get an output
We can use else clause on try-except block given that it must be present after all the except clauses. We enter else block only if no exception is raise.
The raise statement, which is there in Python helps the programmer to force a specific exception to occur. There is only one argument in raise, which is there for indicating the exception to be raised. It must be either an exception class or an exception instance.
If you want to learn more about exception handling in python for data science, then you can check this and this as well. These blogs will help you getting all your queries solved and will provide you the practical aspect of programming as well.