Avatar

Blogging tips

Avatar

Python, Django and Flask

Avatar

Latest tech posts

Tuesday, January 28, 2020

"Try/Except" and "Try/Except/Finally" statements in python

        In Python — along with most other languages — we use try statements to safeguard our code.

Let’s learn about what try statements do and how to begin using them!




What are Exceptions?
Before we dive into the syntax and implementation of try statements, it’s important to establish what exceptions are. 
An exception is the result of code within a try statement that fails. Think of an exception as your code being insulted — “I take exception to that request!”
There are a variety of exception types, we’ll be introduced to a few of them through this tutorial.

How to Use Try Statements?

A try statement, often referred to as a try block, in fact consists of at least two parts: try and except. 

The code we want to safeguard goes inside the try portion of the block. Afterwards, we define what happens when something goes wrong within the except.




In the example above, we use the catch-all value Exception to route any exception to this portion of the block. We also print a vague
message. But what if we want the actual error message from Python?

You can define multiple except blocks within a try, specifying the exact type of exception that should be routed to each. Additionally, you can set the error message to a variable—e is common—so it can be used in your program.



Now we’re in business! Our code is safeguarded, organised, and easily extended.

Let’s see what this looks like in a while loop tasked with collecting an integer input.



The Different Try/Except Variations

So far we’ve used a try/except and even a try/except/except, but this is only two-thirds of the story.

There are two other optional segments to a try block: else and finally. Both of these optional blocks will come after the try and the except. Also, there’s nothing stopping you from using both else and finally in a single statement — but keep them in that order if you do.

Let’s go through each individually and see how they extend the behaviour of a simple try/except.

Try/Except/Else

When attaching an else statement to the end of a try/except, this code will be executed after the try has been completed, but only if no exceptions occur.

We can take the previous example of prompting a user for an integer input and use an else block to thank them for valid input and breaking out of the while loop.






Try/Except/Finally

When attaching a finally statement to the end of a try/except, this code will be executed after the try has been completed, regardless of exceptions.

Again, we’ll use our previous example and add a simple counter to illustrate this behaviour.




This might look a bit odd because the break is still inside the try. It’s reasonable to think that the finally would be cut short upon proper input, however, that’s not the case. The finally section will still execute, regardless of how the try is exited.


No comments:

Post a Comment