Where ever you go in this world you’ll always find exceptions and the same goes for the world of python. But what is exactly an exception? An exception can be understood as an event that disrupts the normal flow of the program. The error which arises during the runtime while the program is in execution is called exception. These are not syntactical errors but occur because of a situation that the program cannot handle on its own.
Some of the most common exception errors are:
I/O Error- If you are trying to open a file that doesn't exist in your system then it is an I/O error.
Import Error- Usually, we import modules when they are needed but what if you called a module that doesn't exist in the first place. If such a situation occurs then it is an import error.
Type Error- Can you build a birdhouse for a fish? Similarly, in python, you cannot store a float value in integer variable otherwise it throws an exception
Value Error- In case there is an issue with the content of the object rather than the type of object then it is referred to as value error.
Keyboard Interrupt- It's a simple exception that is thrown when you manually stop an executing program using the shortcut Ctrl-C or delete.
EOF Error- When built-in functions such as input() or raw_input() hit End-Of-File without reading any data. It occurs when a user is supposed to enter data but presses enter beforehand.
It's said that if there is a will, there is a way. Similarly, if there is an illness, there is a doctor. Python kind of does the same. For exceptions, we have exception handlers. Here are the combinations of these exception handlers:
1. Try and except- In 'try-except', the main code is in the try clause and the exception which might occur during execution is under the except clause. Either the 'try' clause is executed completely or the 'except' clause.
2. One try, multiple except- Similar to 'try-except' but with an expectation of more errors than just one. You can add multiple 'except' clauses for multiple exceptions.
3. Try, except and finally- This type of handling is similar to 'try-except' with and addition 'finally' clause. The statements under the 'finally' block are guaranteed to be executed irrespective of try or except block.
4. Try and finally- This combination of handlers is used when we have to execute some statements irrespective of an exception in the program. It is quite helpful in file handling where we are bound to close the file the even if the function has been executed successfully or not.
5. Try, except, else, finally- This exception handler has 4 clauses each having their purpose. Firstly, the 'try' clause is executed, if an exception is encountered then control is moved to 'except' clause. In case there is no exception then 'else' clause is executed and then the 'finally' clause is executed for sure.
6. Nested try- When an inner 'try-except' clause is nested inside an outer 'try-except' clause then it is referred to as nested try-except. If the inner try-except block cannot handle the exception then the outer try-except block handles the exception.
7. Custom exception- Python provides you with the option to create your exceptions in case you need one. We user the assert statement to create an exception. It is used for debugging purpose and it raises an exception as soon as a condition is found to be false.
So these were some of the methods to handle an exception. Keep learning more because as Amit Kalantri said: “Beginner knows rules, but veterans know exceptions.”. See you next week and you know that it won't be an exception.