0% found this document useful (0 votes)
7 views3 pages

Exception Handeling

Uploaded by

khushbu.shahni13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Exception Handeling

Uploaded by

khushbu.shahni13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# what is exception handling

1. Exception handling is a programming concept used to manage and respond to errors


or exceptional situations that may occur during the execution of a program.

2. These exceptions can range from simple issues like dividing by zero to more
complex scenarios like trying to access a file that doesn't exist.

The basic structure of exception handling typically involves:

1. Try: The block of code where potential exceptions may occur is placed within a
try block. The code inside this block is monitored for exceptions.

2. Catch: If an exception occurs within the try block, it is caught or captured by


a corresponding catch block. Here, the type of exception is specified, and actions
to handle the exception (such as displaying an error message or taking corrective
actions) are defined.

3. Finally (optional): This block, if included, is executed whether an exception


occurs or not. It's commonly used for cleanup tasks like closing files or releasing
resources.

# division by zero
try:
num=int(input("enter any value"))
result = 10 / num # This will raise a ZeroDivisionError
print(result)
except ZeroDivisionError:
print("Error: Division by zero!")

#ValueError:
try:
user_input = int(input("Enter a number: ")) # Trying to convert user input to
an integer
print("Squared value:", user_input ** 2)
except ValueError:
print("Error: Please enter a valid number!")

# Handling Multiple Exceptions:


try:
num = int(input("Enter a number: "))
result = 100 / num
print("Result:", result)
except ValueError:
print("Error: Please enter a valid number.")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")

# Handling Multiple Exceptions:


try:
result = 10 / 0 # This will raise a ZeroDivisionError
print(result)
except ZeroDivisionError:
print("Error: Division by zero!")
try:
list=[1,2,3,4]
value=list[9]
print(value)
except IndexError:
print("index out of range")

#next code
try:
number=int(input("enter any number"))
print("square of number",number**2)
except ValueError:
print("please enter a valid number")

#next code
try:
num=int(input("enter any number"))
result=100/num
print(result)
except ValueError:
print("Error: please enter a valid input")
except ZeroDivisionError:
print("Error: Divison is not possible with zero")

# finally block
try:
value=int(input("enter any value"))
result=10/value
print(result)
except ZeroDivisionError:
print("invalid operation")
finally:
print("final message ")

do not try this code


#next code
try:
file=open(r'C:\Users\Ajit\Downloads\bank\ajit.txt' ,'r')
content=file.read()
print(content)
except FileNotFoundError:
print("file is not found")
finally:
if 'file' in locals():
file.close()
print("exicution completed")
#next code
def divide(a,b):
try:
return a/b
except ZeroDivisionError:
print("division is not posssible by zero")
finally:
print("finally block exicuted")
a=int(input("enter any value"))
b=int(input("enter any value"))
result=divide(a,b)
print(result)

You might also like