Errors in python
There is maybe a bot more to error handing then just understanding some basic examples of a try block, so in this post I will be going over everything that has come up so far when it comes to errors in python. This will include ways to go about causing them, how to handle them, and how to throw user defined errors, any any other little related things that might pop up in the process. After the basics I think it might be a good idea to work out a few quick use case examples that make use of Error handing when making certain kinds of functions, and basic projects. This is an important part of knowing how to program with python, and any language for that matter actually so lets take a quick look at some examples of Errors in python.
1 - Basic Error handing example with the try statement
2 - Types of exceptions
There are a great number of built in types of exceptions, for a full list it might be best to check out the official python doc on built in exceptions. However that doc might not do the best job when it comes to showing a few examples of the various types of exceptions. In this section I will be going over a few examples of some of the types of exceptions that I have run into so far when it comes to working out simple python code examples.
2.1 - KeyError
When it comes to working with dictionary values it is possible to end up with a Key Error when trying to get a key that is not in the dictionary value. When working with a dictionary value in python a key value should not be confused with an attribute, those are two different things when it comes to this kind of data structure value.
2.2 - AttributeError
When working with a dictionary value for the first time things might not work they way one might expect coming from other programing languages. When setting key values for a dictionary these key values are not to be confused with attributes which are a little different from keys actually. If I set a key value and attempt to get at it by way of an attribute rather than key value then I will end up with an attribute error.
2.3 - Name Error
A Name Error is when I attempt to get a global, or local variable that is not there.
2.4 - Syntax Error
Another typical error that I run into now and then is when I mess up with some of the actually syntax of python itself. This kind of exception can not be cached with a try except block unless the python code is evaluated with a call to the eval built in function, or some other such option.
3 - div by zero example
So how about a basic division by zero example, say I have a function that takes to arguments and uses those values in a division operation. When doing so it is possible to pass a value of zero for the denominator which will result in a zero division error. When some functions it will be necessary to define a custom value that will be returned when this kind of error happens.
5 - Conclusion
This is an important step when it comes to learning all the basics of a language that I intend to use to start creating and using some real projects with. There are all kinds of things that can go wrong when it comes to using an application and being able to work out additional code to help handle those kinds of situations is helpful for making a robust project.