DEV Community

Cover image for Exception handling in python.
EzeanaMichael
EzeanaMichael

Posted on • Edited on

Exception handling in python.

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
The output:

Image description
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
Looks Good? Well,
Output:

Image description
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
The output:

Image description
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.
Enter fullscreen mode Exit fullscreen mode

And with the '...finally' statement,

Image description
The output:

Image description
Whether the code under the 'try' statement or the 'except' statement runs, the code under the 'finally' statement will always run.

Image description
Output:

Image description

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.

Top comments (0)