<jupyter_start><jupyter_text>Syntax Error<jupyter_code>print 'Hello'
print('Hello')
print 'World'
print('Hello')
prnt('World')<jupyter_output>Hello<jupyter_text>Runtime Error<jupyter_code>print(1)

print(1/0)

print('Hello')
print('*'*50)

x = int(input('Enter an Integer Number: '))

print(x)

print('a'+ x)
x = int('one')

print(x)<jupyter_output><empty_output><jupyter_text>try........except:<jupyter_code># without try-except

print(1)

print(1/0)

print('Hello')
print(1)

try:
    print(1/0)
except:
    print(10/5)

print('Hello')<jupyter_output>1
2.0
Hello<jupyter_text>Control Flow in try.......except<jupyter_code>print('stmt - 0')
try:
    print('stmt - 1')
    print('stmt - 2')
    print('stmt - 3')
except:
    print('stmt-4')
    
print('stmt - 5')


'''
Cases:
1. No exception

2. exception in stmt 1 to 3

3. exception at stmt 5

4. exception in stmt 1 to 3 and corresponding except block

'''<jupyter_output>stmt - 0
stmt - 1
stmt - 2
stmt - 3
stmt - 5<jupyter_text>Printing Exception as Message<jupyter_code>try:
    print(10/0)
except ZeroDivisionError as message:
    print('Exception:', message)<jupyter_output>Exception: division by zero<jupyter_text>try With Multiple except<jupyter_code>try:
    x = int(input('Enter First Number: '))
    y = int(input('Enter Second Number: '))
    print(x/y)
    
except ValueError:
    print("Please enter integer value only")
    
except ZeroDivisionError:
    print("You can't divide with Zero")

# default exception
# Single except can handle multiple exceptions

try:
    x = int(input('Enter First Number: '))
    y = int(input('Enter Second Number: '))
    print(x/y)
except (ZeroDivisionError, ValueError):
    print('Somethings wrong!')
'''
If try with multiple except blocks available then default except block 
should be last,otherwise we will get SyntaxError.
'''<jupyter_output><empty_output><jupyter_text>finally block<jupyter_code>try:
    print('try block')
except:
    print('except block')
finally:
    print('finally block')
try:
    print('try block')
    print(1/0)
except ZeroDivisionError:
    print('except block')
finally:
    print('finally block')
# Is ordering Important?

try:
    print('try block')
finally:
    print('finally block')
except:
    print('except block')<jupyter_output><empty_output><jupyter_text>try....except.....else<jupyter_code>try:
    print("try")
except:
    print("except")
else:
    print("else")
try:
    print("try")
    print(1/0)
except:
    print("except")
else:
    print("else")
finally:
    print("finally")
# Ordering?

try:
    print("try")
    print(10/0)
except:
    print("except")
finally:
    print("finally")
else:
    print("else")<jupyter_output><empty_output><jupyter_text>Nested try....except....finally<jupyter_code>try:
    print('Outer try block')
    try:
        print('Inner try block')
        print(10/0)
    except ValueError:
        print('Inner except block')
    finally:
        print('Inner finally block')
except ValueError:
    print('Outer except block')
finally:
    print('Outer finally block')
    
# try ValueError on both<jupyter_output>Outer try block
Inner try block
Inner finally block
Outer finally block<jupyter_text>raise an exception<jupyter_code>try:
    age = int(input('Enter your age'))
    if(age<10):
        print('you are too young....so i am gonna through an exception')
        raise ZeroDivisionError('get out')
except ZeroDivisionError as message:
    print(message)<jupyter_output>Enter your age45<jupyter_text>Exception Hierarchy ![title](exception.png)  User Defined Exception<jupyter_code># Also known as customized exception

# Programmer is responsible to define and raise these exceptions

# class classname(predefined exception class):

class TooYoungException(Exception):
    def __init__(self, msg):
        self.msg = msg

try:
    age = int(input('Enter your Age: '))
    if(age<10):
        raise TooYoungException('You are too young!')
    else:
        print('You can Enter!')
except TooYoungException as msg:
    print(msg)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13395946613939694, 'fraction_numerical': 0.01384083044982699, 'mean_word_length': 4.188461538461539, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '433094', 'license': 'MIT', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1376, 'n_words': 345, 'path': 'ML-week/python/exception_handling.ipynb', 'repo_name': 'gopala-kr/a-wild-week-in-ai', 'size': '4046'}