Python try except

Python try except

Python try except and the block is to test a particular block of code to make sure if it runs properly before the rest of the program runs. You have written a big new code for a program, you want to ensure it runs before letting the rest of the program run. try…except block allows running your code and manage an exception if the user detects one.

Add finally and else statements to process extra code depending on the result of the try…except block. The Python try…except statement interprets the code under the “try” section. If the code does not execute, the program will stop where it detected the error, and the “except” will run. The try block provides you to process a block of code for mistakes. The except block allows you to manage the error with a response. The syntax for the try…except block:

try:

​your code…

except:

​your code…

Exception Handling

The try and except block are to detect and manage exceptions. Python runs a code regarding the try statement as a part of the program. The except statement works as the program’s answer to any exceptions in the previous try clause.

Exceptions are easy to handle errors and particular conditions in a program, if you are executing a code that results in an error then use exception handling. Moreover, you can alert an exception in your program using the raise exception statement. Detecting an exception deintegrates the current code running and falls back to the exception back until it is operated.

Example :

amount = 9000
 
# check that You are eligible to
#  purchase Dsa Self Paced or not
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")

Output :

The syntax error here is due to the wrong syntax in the code. It leads to the termination of the program.

Let us now see how the user can handle this exception. Check out the code below:

Example :

marks = 10000

a = marks / 0
print(a)

Output :

Multiple Except Statements

You can reiterate except statements for many types of errors to detect for multiple exceptions. This is better if you think that one of the exceptions may be alert but you are unsure which one you will face. An illustration of try…except blocks that search for a Name Error:

Example :

try:
​print(our variable)
except NameError:
​print('ourVariable is not defined')
except:
​print('Error returned')

Here, the code that sends back our Variable is not described because the code returns a Name Error. We can increase the errors, such as an OSError or a Zero Division Error.

Python Finally Block

If you want to message print in both cases where an error is returned and if no error is detected. This happens when the final block executes. If you run a finally clause, its composition will be run regardless of if they try…except block detects an error. Finally, restrictions are indicators that your code has been executed, because they are not distinctive from a code that has been executed, they are not as generally executed.

try…except blocks swiftly debug your Python code, a program attempts to execute the code in a “try” block. If this fails, the “except” block executes. The code in a “finally” statement processes regardless of an “except” block. These blocks are useful when you’re testing current code or creating new code. It makes sure that your program executes properly and does not contain any kind of errors.

Example :

def divide(a, b):
    try:
        
        result = a // b
    except ZeroDivisionError:
        print("No result, You are dividing by zero ")
    else:
        print("Your answer will be ", result)
    finally: 
        print('Finally block is always executed')  
 
divide(5, 4)

Output :

Your answer will be  1
Finally block is always executed