How To Use Python Exception Handling With Examples

How To Use Python Exception Handling With Examples
How To Use Python Exception Handling With Examples 


How To Use Python Exception Handling With Examples 

Python Exception: A exception is a which occurs during the execution of a program that disrupts the normal flow of the program's instructions.

when a Python script encounters a situation that it cannot cope with it raises an exception.
An exception is a Python object that represents an error.
To understand the exception firstly we should know about the error in the program.

Python Errors in into two Types.

1. Compile Time Errors

2. Run-Time Errors

Compile Time Errors: A compile-time error caught during the compiled time is called a Compile-time error. 
Compile-time errors include python library syntax error or incorrect class import and incorrect Modules.

Run-Time Errors: A run time error that occurs during the run time of the program is called run time error.
They are also known as exceptions. Hence we can say that exception is a runtime error that occurs because of a user mistake in the Python program.


Reasons of Exception in Python 

Mismatched Input: A mismatch input lets you can entering our name in place of age, causing exception because age is of data type int and name will be a string.

File does not exist: A file does not exist you can read a text file icoderweb.txt and that file does not exist in the system causing the exception.

Divide by zero exception: A zero division Exception you can a number is divided by zero then the output will be undefined.

IndentationError: A indentations can have extra space in the Python program.

Exception Handling: Exception handling is the error of python program exception is called exception handling. python exceptions are three main keywords that are used to solve the python code problem of exception.

try block: A try block is a place where actual code is written and the exception occurs. when the code will lead to an error that exception will get caught inside the except block.

except for block: A except block is intended to catch the error and handle the exception condition. you can have multiple except blocks to handle different types of exceptions and perform different actions when the exceptions occur.

finally, block: A final block is always executed in Python Program.


Syntax of try-except Block

try: 

     #block of statements 

except: 

     #block of statements 



How To Use Without exception in Python?

try: 

    x=100 

    print("Value of X:",x) 

except: 

    print("An exception occurred") 

*****OUTPUT*****

Value of X: 100 

How To Use With exception in Python?

try:

     print("value of x:",x) 

except:

     print("An exception occurred") 

*****OUTPUT*****

An exception occurred 


Syntax of try _ except _ else

try: 


#statements 


except: 


#statements 


else: 


#statements


How To Use try else in Python?

try: 

x=100 

    print("value of X:",x) 

except: 

    print("An exception occurred") 

else: 

     print("I am else statement") 

*****OUTPUT*****

Value of X:100 

I am else statement 


How To Use Python Exception?

try: 

    x=100

    y=0 

    z=x/y

    print("Value of Z:",z)

except: 

    print("Zero Devision can not be divided") 

else: 

    print("else statement")

*****OUTPUT*****

Zero Division can not be divided


Syntax of try _ except _ else _ finally

try: 

     #block of statements 

except: 

     #block of statements 

else: 

     #block of statements 

finally:

      #block of statements 

How To Use Finally block in Python?

try: 

    x=100 

    y=20

    z=x/y

    print("Value of Z:",z) 

except: 

    print("Zero Devision can not be divided") 

else: 

    print("I am else statement") 

finally: 

    print("I am finally block") 

*****OUTPUT*****

Value of Z: 5.0 

I am else statement 

I am finally block

How To Use Zero Division in Python?

try: 

    x=100 

    y=20

    z=x/y

    print("Value of Z:",z) 

except: 

    print("Zero Devision can not be divided") 

else: 

    print("I am else statement") 

finally: 

    print("I am finally block") 

*****OUTPUT*****

Zero Division can not be divided

I am finally block 




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.