Updated March 30, 2023
Introduction to Python try except
In general, an exception is an error that occurs when the program is executing. We can handle a few errors by using try and except blocks. Whenever an error occurs, the execution of the programs stops and generates exceptions which are handled by try and except block to avoid program crashing. These try and except blocks are used for error handling in Python programming language as other programming languages. Therefore the exceptions or errors generated are caught by try block and are handled in the except block.
Working of try except Block in Python
In a Python program, there is a concept known as exception handling for handling errors when an error occurs. So when an exception occurs, the programs stop running and generate some error messages so such messages can be caught by try block. The try block is used for testing the block of code for checking the errors. The except block is used to handle such errors that occurred during program execution.
There are two kinds of errors in Python:
- One is syntax error which occurs when the Python parser is unable to understand any line of codes.
- The other type of error is known as an exception which occurs during the program execution.
So we are seeing how to handle these exceptions using try and except block.
Let us see a few of exception errors that occur and are handled using try and except block:
- IOError: This occurs when the file cannot open.
- ValueError: This type of error occurs when any built-in function receives the wrong argument.
- EOFError: This type of error occurs when the end of the file is reached without reading any data.
- ImportError: This type of error occurs when we have not imported the module that is needed, which means if the module is unable to find.
try: code or statements except: code or statements
How does try except() Block works with examples?
Whenever we use try() and except() blocks first, try() block is executed, that is, the code between the try and except clause. A try() block can have more than one except clause. Whenever is there is no exception is occurred then only try block will run, and except clause is finished, and if exceptions occur, then the try clauses will be skipped and execute except clause. Another special case is when an exception occurs, but the code within except clause couldn’t handle it, then it is passed to the outer try statement, and after all, still, the exception is unhandled, then the execution stops.
As the above statements, we will demonstrate two cases below:
But, first, let us see when there are no exceptions, then we can run only try block.
print("The program where no exception and only try block is executed:") def divide(a, b): try: r = a // b print("The result of the above expression is :", r) except ZeroDivisionError: print("Oops you are trying to divide by Zero:") divide(3, 2)
In the above program, we can see we are trying to divide two numbers. So the code is written in a try block, and if we have the second number to divide the first number by zero, then an error occurs; hence this exception is handled by except block. But in this, when we call the function with proper numbers to divide, then no error occurs; therefore, only try block is executed.
Now we will see another condition where exceptions occur, and we will see the above example only for the demonstration. Then this error occurs because we are trying to divide the number by zero. So an error occurs and is handled by except block, so only except block is executed only.
print("The program where no exception and only try block is executed:") def divide(a, b): try: r = a // b print("The result of the above expression is :", r) except ZeroDivisionError: print("Oops you are trying to divide by Zero:") divide(3, 0)
Now let us see a simple example, where a variable is not defined, and we are handling NameError by using except clause and try statement.
Now let us see the below example where an error occurs.
print("The demonstarton of exception handling in Python:") print(x)
In the above program, we can see we are trying to print the variable “x”, which is not defined and hence it throws an error known as NameError, which is handled by the below code.
print("The demonstarton of exception handling in Python:") try: print(x) except NameError: print("An exception occurred due to variable x is not defined")
In the above program, we can see we are trying to print variable “x” in a try block because we don’t know if an error might occur as we have not defined the variable “x”; therefore, this error is handled by using except block where if this error occurs then except block identifies it as “NameError” and then we print a message saying the variable is not defined. Therefore we write the code that throws an error within the try block.
Now let us see how one try block with many except blocks example:
print("Program to demonstrate single try and many except block:") try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong")
In the above program, we can see that NameError occurs because a variable “x” is not defined, and if this error does not occur, it will show another message of the except block.
This article concludes that an error that occurs during the program execution is handled using exception handling concepts in Python. In Python, there are different types of errors which we saw above, and we also saw which error or exception occurs and what are they. We also saw how to try block works and with examples. We also saw one simple example of NameError, which occurs when anything is not defined. Finally, we also saw how only one try with many except block works with an example.
This is a guide to Python try except. Here we discuss the introduction and working of try except block in python with examples respectively. You may also have a look at the following articles to learn more –