All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Try And Except In Python

Last Updated : Mar 11, 2024

Try And Except In Python

In this article we will show you the solution of try and except in python, exceptions can be handled with the try except statement. The execution of a programme could result in exceptions.

Exceptions are mistakes that occur while the programme is running. Python will immediately stop if it encounters issues such as syntax errors (grammar faults).

The end user and developer both suffer from a sudden exit. You can effectively address the issue by using a try except statement rather than an emergency halt.

If you don't handle exceptions correctly, an emergency halt will occur. Built-in exceptions in Python have the ability to produce an error.

During the course of the program's execution, an error is referred to as an exception.

The type of exception is displayed if one occurs. Exceptions must be handled properly in order to prevent programme crashes. Use of the try-catch block is required to handle exceptions.

We will now discuss the idea of how to try and except in python with an example.

Step By Step Guide On Try And Except In Python :-

Code 1

try:
    numerator = 10
    denominator = 0
    result = numerator/denominator
    print(result)
except:
    print("Error: Denominator cannot be 0.")
finally:
    print("TalkersCode finally block.")

Code 2

try:
  f = open("demo.txt")
  try:
    f.write("Welcome To Talkers Code")
  except:
    print("Something went wrong when writing to the file")
  finally:
    f.close()
except:
  print("Something went wrong when opening the file")
  1. First, the code initializes the two variables numerator and denominator with the values 10 and 0, respectively.
  2. The code tries to divide the numerator by the denominator and stores the outcome in the result variable. However, a ZeroDivisionError arises because you cannot divide by zero.
  3. A try-except block in the code allows it to detect this issue. When an error happens, a message is printed stating that the denominator cannot be zero.
  4. Next, the programme enters the "finally" section, which always runs whether or not there was an error. In this instance, it displays the message "TalkersCode finally blocked."

Conclusion :-

As a result, we have successfully learned how to try and except in python with an example.

The try except syntax is an excellent technique to manage problems and stop your programme from terminating during execution, as this article has demonstrated.

Even better, you can use the type() function to determine the specific kind of the exception and print that exception to the terminal by assigning the error to a variable.

I hope this article on try and except in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪