<jupyter_start><jupyter_text>Error & Exception  Syntax ErrorLet's take a look at basic error.<jupyter_code>while True print('Hello world')<jupyter_output><empty_output><jupyter_text>ExceptionNow let's take a look at some exceptions and resulting error messages<jupyter_code>10 * (1/0)
4 + spam*3
'2' + 2<jupyter_output><empty_output><jupyter_text>** Play with the code as much as you want **  Handling ExceptionsLat take a closer look at a built-in exception<jupyter_code>while True:
    try:
        x = int(input("Please enter a number: "))
        break
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")<jupyter_output>Please enter a number:  abc<jupyter_text>Multiple Except Blocks** Multiple Except Statement **<jupyter_code>try:
    print(x)
except NameError:
    print("Variable x is not defined")
except:
    print("Something else went wrong")<jupyter_output>Variable x is not defined<jupyter_text>** Handling Multiple Exception in a Single block **<jupyter_code>try:
    num = int(input("Enter the number: "))
    print(num**2)
except(KeyboardInterrupt, ValueError, TypeError):
    print("Please check before you enter....Program Terminating")
print("Bye")<jupyter_output>Enter the number:  abc<jupyter_text>Raising ExceptionThe code given below simply creates a variable and prints its value. There was no error in the code but we have deliberately raised an exception.<jupyter_code>try:
    num = 10
    print(num)
    raise ValueError
except:
    print("Exception occurred....Program Terminating")<jupyter_output>10
Exception occurred....Program Terminating<jupyter_text>The code given below is used to re-raise an exception from the except: block.<jupyter_code>try:
    raise NameError
except:
    print("Re-raising the exception")
    raise<jupyter_output>Re-raising the exception<jupyter_text>Error checks vs Exception Handling<jupyter_code># with checks
n = None
while n is None:
    s = input("Please enter an integer: ")
    if s.lstrip('-').isdigit():
        n = int(s)
    else:
        print(s + " is not an integer.")
# with exception handling
n = None
while n is None:
    try:
        s = input("Please enter an integer: ")
        n = int(s)
    except ValueError:
        print(s + " is not an integer.")<jupyter_output>Please enter an integer:  abc<jupyter_text>The *else* and *finally* clause*You can use the else keyword to define a block of code to be executed if no errors were raised:*<jupyter_code>try:
    print("Hello")
except:
    print("Something went wrong")
else:
    print("Nothing went wrong")<jupyter_output>Hello
Nothing went wrong<jupyter_text>*The finally block, if specified, will be executed regardless if the try block raises an error or not.*<jupyter_code>try:
    print(x)
except:
    print("Something went wrong")
finally:
    print("The 'try except' is finished")<jupyter_output>Something went wrong
The 'try except' is finished<jupyter_text>Built-in & User-defined exceptions*Built-in Exception*<jupyter_code>num = int(input("Enter the numerator: "))
deno = int(input("Enter the denominator: "))
try:
    quo = num/deno
    print("QUOTIENT: ", quo)
except: 
    print("Denominator cannot be zero")<jupyter_output>Enter the numerator:  5
Enter the denominator:  0<jupyter_text>*User-defined Exception*<jupyter_code>class MyError(Exception): 
    def __init__(self, value): 
        self.value = value  
    def __str__(self): 
        return(repr(self.value)) 
try: 
    raise(MyError(3*2)) 
except MyError as error: 
    print('A New Exception occured: ',error.value)<jupyter_output>A New Exception occured:  6
{'fraction_non_alphanumeric': 0.10274923632324354, 'fraction_numerical': 0.004998611496806442, 'mean_word_length': 4.474164133738602, 'pattern_counts': {'":': 0, '<': 39, '<?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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '663049', 'license': 'MIT', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1128, 'n_words': 357, 'path': 'Divyankar_Python_Error&ExceptionHandling.ipynb', 'repo_name': 'prathyu3122/Open-contributions', 'size': '3601'}