# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Error and Exception Handling

5/0
print("hellow")

# +


a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
try:
    c = a/b
    print(c)
except ZeroDivisionError:
    print("Exception is caught ZeroDivisionError")


print('Hello Error')

# +

a =int(input("Enter a number")) 
b =int(input("Enter a number")) 


c = a/b
print(c)

print('Hello World')
# -

a = [1,2,3]
try:
    print(a[8])
except IndexError:
    print('Yeah! I Have handled that error')

# +

a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
    print(c)
    print(d[2])
except ZeroDivisionError:
    print('Exception is caught ZeroDivisionError')
print('Hello World')

# +

a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
try:
    c = a/b
except ZeroDivisionError:
    print("Handled ZeroDivisionError")
else:
    print("Else " +str(c))





# +


a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
    e = d[5]
    print(e)
except ZeroDivisionError:
    print("Handled ZeroDivisionError")
else:
    print(c)





# +

print("First")
a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
#     e = d[5]
except ZeroDivisionError:
    print("Handled ZeroDivisionError")
else:
    print(c)

print("last")



# +


a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
    e = d[5]
except ZeroDivisionError: #Type Z and press tab to get list of Exception
    print("Exception is caught ZeroDivisionError")
except  IndexError:                #Type I and press tab to get list of Exception
    print("Exception is caught is list index out of range")
else:
    print(c)
    print(e)




# +


 
d = []
try:
    a =int(input("Enter a number")) 
    b =int(input("Enter a number")) 
    c = a/b
    e = d[5]
except:           # here we did not explain the type of exception. Genral exception will
                  # be used. means it will catch every kind of exception
    print("koi si bhi Exception is caught")
else:
    print(c)




# +

# a =int(input("Enter a number")) 
# b =int(input("Enter a number")) 
d = []
try:
    '''built in message print karna hy exception ka to {except Exception as e:}
    is me jistrah ki acception aegi wo uska message khud he utha k laega or print karega
    is code ko khud se run kare or exception raise kar k dekhen'''
    a =int(input("Enter a number")) 
    b =int(input("Enter a number"))
    c = a/b
    ee = d[5]
except Exception as e:
    print("Handled Error == " +str(e))
else:
    print(c)

print('HelloWorld')

# +

a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
    e = d[5]
    
    """yahan ek humne apni message likha hy or ek built in use kia hy"""
    
except ZeroDivisionError:
    print("Handel ZeroDivisionError")
    
except Exception as e:
    
    print("Handle Error == " +str(e))
    
else:
    print(c)





# +


a =int(input("Enter a number")) 
b =int(input("Enter a number")) 
d = []
try:
    c = a/b
    e = d[5]
except ZeroDivisionError:
    print("Handel ZeroDivisionError")

else:
    print(c)

finally:
    print("Finally will always run")




# -



# + active=""
# ###Rasing our own exceptions
#
# We can rasie exception by ourselves. ony any condition we defined like if a>10 raise exception
# like if user inputs value 10 raise exception bla bla bla bla......

# +

class Student():
    def __init__(self,name,age):
        if age > 80 or age <16:
            raise Exception("Age can not be greater then 80 and less then 16")
        self.name = name
        self.age = age


# +
age = int(input("Enter students age: "))


st = Student("ALi",age)


print(st.age)


# + active=""
# #Catching the exception raised by ourself###
# Exception catch karne ki liye wahi kam karenge k jahan se raise hone k dar ho usko try me
# dal den. or except me catch kara den. Go through the code......

# +
class Student():
    def __init__(self,name,age):
        if age > 80:
            raise Exception("Age can not be greater then 80")
        self.name = name
        self.age = age




# +

'''ab humen try except laga diya hy code crash nahi hoga or izzat batch jaegi hahahahah'''
try:
    age = int(input("Enter students age: "))
    st = Student("Hello",age)   # send different ages and see the result
except Exception as e:
    print("Exception "+str(e))
else :
    print(st.age)


# + active=""
#
# #####################################################
# #######Importantttttttttttttttttttttttttttttttt######
# #####################################################

# + active=""
# Making our class of exception::::::::
# Raising exception ourselves::::::
#
# Last code me we used python exception class
# Now we will make our own exception class and will used in the code.
# Dont panic guls and guys...
#
#

# +

class Student():
    def __init__(self,name,age):
        if age > 80 or age <16:
            raise StudentAgeException("Age can not be greater then 80 and less then 16")
            # we used here own class of exceptio
        self.name = name
        self.age = age

'''All the exception raised are derived from a class Exception by default 
   We can make our own class of exception and can derived(inherit) it from parent
   Exception class like this:'''        
        
class StudentAgeException(Exception):
    pass



# +
age = int(input("Enter students age: "))
st = Student("Hello",age)
# abhi it is creating exception that is not handled
print(st.age)

####Note###
'''exception raise karane k liye humne Exception python ki general wali use nahiiiiiii ki hy
   abhi code crash hogaya hy kyu k humne abhi exception catch nahiiiiiiiiiiiiiiiiii ki hy'''


# + active=""
#
# #################################################################################
# Raising and catching our own exception [[[[defined in our own exception class]]]]
# Raising and catching our own exception [[[[defined in our own exception class]]]]
# Raising and catching our own exception [[[[defined in our own exception class]]]]
# Raising and catching our own exception [[[[defined in our own exception class]]]]
# Raising and catching our own exception [[[[defined in our own exception class]]]]
# #################################################################################

# +

class Student():
    def __init__(self,name,age):
        if age > 80 or age <16:
            raise StudentAgeException("Age can not be greater then 80 and less then 16")
        self.name = name
        self.age = age

class StudentAgeException(Exception):
    pass


try:
    age = int(input("Enter students age: "))
    st = Student("Hello",age)

except StudentAgeException as e:
    print("StudentAgeException == "+str(e))
else :
    print(st.age) 


# -





StarcoderdataJupyterScriptsDedupFiltered