Don’t be afraid of running into errors! Sometimes, you can actually catch ‘em.
When writing code, it is almost inevitable that you will run into errors. And knowing how to debug and catch the errors is probably one of the most important skills any programmer should have.
Perhaps one of the most salient reasons is: Errors stop your program from running. Basically, it will crash 👎🏽. Expecting such errors and catching them in python programming is what this blog is going to teach.
There are 4 Keywords when dealing with Exception Handling:
It is pointless to try and explain what each of those key words do. Lets see how they work in an actual coding environment.
Suppose there is a game that can only be played by people 18 years and over.
In the first prompt, it will require one to input their age to check whether someone can proceed or not.
The code-block will look like below:
As you can note, the program requires one to enter a number. eg: 20 and not Twenty(in words)!
Lets see what happens in the console if someone does that.
2. Instance 2: Instead of 20, I entered the word ‘twenty’. As you can see, this will throw an error. This is a value Error. (Look at the last line in red)
So, what is a value error exactly?
The simplest way to think of it would be: the value that is entered is wrong!
The program expects an input we have give, but the format is not exactly what the program expected.
What if, instead of letting the program crash when the user inputs a word instead of a number, simply notify the user to enter a number and not a word. How would you go about that? That is where Error handling comes in. Check this out!.
This is how we would code the logic.
And here’s the result.
As you can see from the code snippet above, instead of letting the program crash and end leaving the user confused, the user will instead be notified that they need to enter a number.
The Try keyword as used in the code basically holds the code that you are testing. The code where the error might arise. In this case this is where the user will enter their age.
The except Keyword handles the error from the“ try” key-word and gives an alternative of what should be done in such an instance. In this case, the error is a value error, which had been explained error. As a requirement, you will need to specify what kind of error is likely to arise.
Let’s Switch it up!
Now that you have noted that the error will arise in the user input, we can actually switch up the code as shown:
In this part, there is the else key word: (This refers to line 9 in the snippet above.)
Basically, what the else keyword does is to say: If the code runs successfully (i.e the code in the try keyword,) then do this:
in this case, if the user inputs an integer in the age input, the code in the else keyword will then execute. However, if there is an error, then it will notify the user to use the correct format.
a) Scenario 1 — The user enter’s an integer and the code in the else block executes.
b) The user makes a mistake and the code terminates at the except block.
For the finally keyword, this will execute whether the code terminates or not. For instance, in this case, this can be to thank the user for trying out the game!.
Basically, this part doesn't care what happens in the try, except or else blocks.
This is how the code would look like:
Result 1 — Sorry Pal, your underage. But thanks!
Result 2- Oh! man you’re cool. Thanks for your interest. we hope you liked it!
Result 3 — You can go back and enter your age correctly, but thanks for your interest.
As you can see under Result 3: The program tells the user to enter a number. But code in the finally keyword still executes even though the user messed up!.