Calling a function from within try block

Calling a function from within try block

Lesson Details:
June 29, 2020


I: Introduction

Let us begin by learning the basics of Python programming. A well-known and free programming language, Python was designed by Guido van Rossum and released in 1991. It is a general-purpose high-level programming language that has been used to develop applications that range from games to web servers. You can also use it to develop desktop applications or even mobile apps. The language’s simple syntax and clean design makes it a powerful tool for all levels of programming skills. In fact, it can be used as an introductory language for students since the basic concepts of programming are very easy to understand.

II: Body

In this section, we will learn how to call a function from within a try block. This is a common coding error committed by beginners who are unaware of what exactly a try block does. So let’s first understand what a try block is before we proceed further into the topic.

A try block is used to handle errors that may be caused due to invalid user input as well as other exceptions. The following code shows how you could write a try block in Python. Note the presence of the “except” statement within the try block.

try: print "This is the try block" print("I am inside the try block") def function(): print("I am inside a function") except: print("Something went wrong") else: print("This is the else block") else: print("This is the else block")

The above code produces the following output when executed:

This is the try block I am inside the try block This is the else block Something went wrong

So you can see that after entering the “try” line, the program goes into the first line of code within the try block, i.e., it prints “This is the try block”. Then it goes into the next line of code within the try block, i.e., it prints “I am inside the try block”. But when it attempts to execute the next line of code in the try block, which calls function(), an exception is triggered because it doesn’t have a parenthesis at its end. This exception then causes execution to move out of the try block and into the catch block, which in our case is empty thus causing execution to move into the else block where it executes all lines of code up till finally exiting the program normally.

Now let us understand what exactly happens when an exception occurs within a try block so that we can understand why calling a function from within a try block results in an exception being thrown. When an exception occurs within a try block, Python uses an exception handler specified in this order:

The first non-except statement found in the try block; if there are no non-except statements in the try block, then jump to step (2) The first except statement found in the try block; if there are no except statements in the try block, then jump to step (3) The default exception handler; if no exception handlers are found, then Python raises an “unhandled exception” error message and exits with exit code 1 (i.e., raise SystemExit).

So if we look at step (1) of the above procedure, our example’s except statement is empty therefore Python moves on to step (2) where it would find our empty except statement within the inner nested function()()()()()()()()(). So this exception handler would get executed and cause execution to move out of the inner nested function()()()()()()()()()()()()() function and enter the finally clause of the try/catch/finally statements which returns control back to outer nested function(). Then control passes on to all statements up to finally returning control back to main(). So this is why calling function()()()()()()()()()) causes an exception to occur because Python knows that no exception handler has been provided yet in this case and thus it throws an exception and exits with exit code 1.

III: Conclusion

In this article, we discussed how calling a function from within a try block results in an exception being thrown in Python. If you want to further enhance your knowledge about Python programming, you could visit these links below:

loader
Course content