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

Python Exception Handling Guide

The document contains Python code snippets demonstrating exception handling using try-except blocks for various scenarios, including division by zero, index out of range, and key errors. It also includes custom exception handling with a user-defined AgeError class to validate age input. The code illustrates the use of else and finally clauses in exception handling.

Uploaded by

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

Python Exception Handling Guide

The document contains Python code snippets demonstrating exception handling using try-except blocks for various scenarios, including division by zero, index out of range, and key errors. It also includes custom exception handling with a user-defined AgeError class to validate age input. The code illustrates the use of else and finally clauses in exception handling.

Uploaded by

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

##try:

## a=10

## b=5

## print(a/b)

##except:

## print("not divided by 0")

##

##

##try:

## num=[1,2,3,4,4,50]

## print(num[1])

##except:

## print("Out of range")

##try:

## a=10

## b=5

## print(a/b)

##

## num=[1,5,2,4,6,3]

## print(num[2])

##

## name="shivam"

## print(name)

##

## dict1={"shivam":25,"shubham":26}

## print(dict1["Ashu"])

##
##except ZeroDivisionError as e:

## print(e)

##except IndexError as e:

## print(e)

##except ValueError as e:

## print(e)

##except KeyError as e:

## print("Key error")

##except ValueError as e:

## print("Value error")

##except Exception as e:

## print("common Error")

#else executed when error not throw

##try:

## a=10

## b=5.0

## print(a//b)

##except Exception as e:

## print(e)

##else:

## print("Executed successfully")

#Finally executed wether error throw or not

##try:

## a=10
## b=0

## print(a//b)

##except Exception as e:

## print(e)

##finally:

## print("Executed or Error")

class AgeError(Exception):

pass

age=int(input("Enter your age"))

try:

if age<18 or age>100:

raise AgeError("Age should be Between 18 and 100")

except Exception as e:

print(e)

class AgeError(Exception):

pass

age=int(input("Enter your age"))

if age<18 or age>100:

raise AgeError("Age should be greater then 18")

You might also like