Python try catch exception
The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program.
Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding mistakes like syntax errors (punctuation or grammar flaws), rather it will suddenly stop.
A sudden exit is awful for both the developer and end client.
Rather than an emergency stop, you can utilize a try-except to appropriately manage the issue. A sudden end will occur on the off chance that you don't as expect handle exceptions.
What is Try block?
- Try block is used to test codes for errors or we can say exceptions are handled using “try block”.
- Try block contains the code which the user thinks might raise an exception. If the code inside the try clause executes that simply means that the code doesn’t contain any error or exceptions.
- We cannot have a try block without except block.
Let us take a real-life example,
# working of try()
def divide(x, y):
try:
result = x // y
print("Yeah ! Your answer is :", result)
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
divide(3, 2)
Output:
('Yeah ! Your answer is :', 1)
What is Except block?
- It is common in the python world to use exceptions for flow control.
- This statement runs when an error is encountered in a given code.
- The use of these statements is completely optional and isn’t strictly necessary.
- Except block is used to catch errors in a program.
- Except block catches the errors in the code written in the try block.
Let’s take a real-life example,
def divide(x, y):
try:
result = x // y
print("Your answer is :", result)
except ZeroDivisionError:
print("You are dividing by zero ")
divide(3, 0)
Output:
Sorry ! You are dividing by zero
It returned an error in the except block cause we tried to divide the value by 0.
Note: Only except clause will run because there is an exception.
Most common exception in python:
AttributeError
This exception addresses the activity of referring to or appointing to an attribute that doesn't in any case exist - the exemplary invalid reference. Certain parameters are relied upon to have a value in the happy case will as often as possible, not at runtime, which causes this exception.
TypeError:
TypeError is an extremely normal exception and gets tossed when an individual operation is performed on a different type. On the off chance that this happens in a web context because of distorted client input (particularly basic in a REST API), the exception gets covered inside a log file. Utilizing an instrument like Raygun will guarantee you are notified and have every one of the information you want to investigate the exception and push a fix rapidly.
ValueError
ValueError occurs when a user passes an invalid value but a correct type of argument to the function.
AssertionError
It’s a programming concept that a user applies while writing a code where he declares a condition to be true where he uses an assert statement before running the module.
If it returns True then it moves to the next line but if it returns False the program stops and returns AssertionError.
Conclusion
In this article, we tried to learn about try block and except block and which are the most common exceptions you can come across in python. Using Try and except block isn’t strictly necessary and completely optional but is a great way to catch exceptions in our codes and programs.