Exception handling in python.
Tags: programming, python, beginners, tutorial
Published: 2022-02-15 18:01:21

**Intro**
        You're a Python programmer or a beginner in programming or perhaps you're a computer scientist who needs to further his knowledge in python programming or a person who's curious about what I mean by handling exceptions. Here are detailed explanations on the topics, see what you'll learn after reading this.

**_What are exceptions?_**
        Exceptions are basically what you can call' error codes', They appear when you type an incorrect statement or input in your code. For example
/For this example I'll be using an application called 'pydroid 3' on my Android device
Trying to add a letter and number :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9nn24jewq0tzs9tw2tr.png)
The output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmn17wrtgaysvueb7hb4.png)
        NameError and as you can see the program can't run.
        You might be thinking, 'who will try to add a number and a letter?', let us take a more realistic example then, 
/A Google hashcode practice program written and bugged by me.
/A program to prepare a pizza for a certain number of customers using only their likes and removing any dislikes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hyapv00ha3xni4pkaf4.png)
Looks Good? Well, 
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ticl65i1d2vgi5pl0drn.png)
        As little as the mistake of capitalizing letters can cause the code to stop running and you'll end up searching for the error, thankfully it helps by indicating the line where the error occurred.

#_**Types of Exceptions**_
        
So what are the types of exceptions in Python, There are several but here are some examples:

-   **NameError**:an unknown variable is used(like in the first and second example).
- **ValueError**: a function is called on a value of the appropriate type, but with an inappropriate value.
-  **TypeError**: a function is called on a value of an incompatible type.
-  **SyntaxError**: the code can't be parsed appropriately. 
- **ImportError**: an import fails.

#**_Why exceptions are in place?_**

Exceptions are in place to help us avoid unanticipated errors in our codes and thus prevent the programs from crashing, and when understood properly, you can easily debug or correct the mistakes that might have been made when typing the code, as seen in the example above.

#_**How to handle all kinds of exceptions**_

Ever heard of the 'try...except...finally' statement?
These can make the program skip the exception and run.
For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5augl2krbenevp0uqm8.png)
The output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5zpdgjb1hp2jjbmn0pyi.png)
        As you can see, all those gibberish are gone and the program still runs.

        You can also specify the type of error to be handled by the try...except statement but it might be advisable to just use the except statement so no matter what error could occur the program will run.
         Also, the 'except...' statement catches all types of errors/exceptions, both the expected and unexpected errors, and it can hide the logical flaws in the program, So it's better if you catch specific errors that you expect.Depending on what the programmer needs any can be used.
 
And with the '...finally' statement,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9a8nlwfqqceuvl0a8jj.png)
The output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tx9um6dbxci8iwbkn27.png)
        Whether the code under the 'try' statement or the 'except' statement runs, the code under the 'finally' statement will always run.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jhghsqobujuj1vyu591.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omnb3hbjcmqm77jj7g6b.png)
#_**Tips on using exception handlers**_

Most programmers code without the use of these exception handlers, to understand where the error is coming from In the code, others prefer handling specific errors by thelling the user the error has occurred for specific reason other than crashing the program, either way, it depends on the programmer, feel free to use this statement and play around with the code, it could be used to make some complex and fun codes too.
#**_Conclusion_** 

Hope you enjoyed reading this as I did writing and hope you learned something new, thanks for your time, comment,like and keep coding.