From 1183ca3e4284ecc50081dbc531fc054655b2635d Mon Sep 17 00:00:00 2001 From: Vishal Mishra <60419110+vishalaadee@users.noreply.github.com> Date: Fri, 7 Oct 2022 00:10:12 +0530 Subject: [PATCH] Exception Handling --- .../Catching_20221006234551.txt | 0 .../Catching_20221006234554.txt | 10 ++ .../Catching_20221006234555.txt | 10 ++ .../Catching_20221006234607.txt | 22 +++++ .../Catching_20221006234609.txt | 22 +++++ .../Catching_20221006234622.txt | 43 +++++++++ .../Catching_20221006234630.txt | 44 +++++++++ .../Catching_20221006234639.txt | 44 +++++++++ .../Catching_20221006234642.txt | 45 +++++++++ .../Catching_20221006234645.txt | 46 +++++++++ .../Catching_20221006234646.txt | 47 ++++++++++ .../Catching_20221006234659.txt | 52 ++++++++++ .../Catching_20221006234709.txt | 52 ++++++++++ .../Catching_20221006234711.txt | 53 +++++++++++ .../Catching_20221006234803.txt | 55 +++++++++++ .../Catching_20221006234809.txt | 55 +++++++++++ .../Catching_20221006234811.txt | 56 +++++++++++ .../Catching_20221006234814.txt | 57 +++++++++++ .../Catching_20221006234826.txt | 57 +++++++++++ .../Catching_20221006234851.txt | 79 ++++++++++++++++ .../Catching_20221006234903.txt | 81 ++++++++++++++++ .../Catching_20221006234915.txt | 86 +++++++++++++++++ .../Catching_20221006234920.txt | 86 +++++++++++++++++ .../Catching_20221006234921.txt | 86 +++++++++++++++++ .../Catching_20221006234922.txt | 86 +++++++++++++++++ .../Catching_20221006234924.txt | 85 +++++++++++++++++ .../Catching_20221006234932.txt | 78 +++++++++++++++ .../Custom Exceptions_20221007000110.py | 0 .../Custom Exceptions_20221007000114.py | 55 +++++++++++ .../Custom Exceptions_20221007000116.py | 55 +++++++++++ .../Custom Exceptions_20221007000146.py | 94 +++++++++++++++++++ .../Custom Exceptions_20221007000153.py | 94 +++++++++++++++++++ .../Errors and Exceptions_20221006235715.py | 0 .../Errors and Exceptions_20221006235737.py | 2 + .../Errors and Exceptions_20221006235742.py | 2 + .../Errors and Exceptions_20221006235747.py | 3 + .../Errors and Exceptions_20221006235758.py | 10 ++ .../Errors and Exceptions_20221006235803.py | 11 +++ .../Errors and Exceptions_20221006235815.py | 13 +++ .../Errors and Exceptions_20221006235819.py | 13 +++ .../Errors and Exceptions_20221006235820.py | 13 +++ .../Errors and Exceptions_20221006235825.py | 13 +++ .../Errors and Exceptions_20221006235838.py | 44 +++++++++ .../Errors and Exceptions_20221006235844.py | 44 +++++++++ .../Errors and Exceptions_20221006235847.py | 44 +++++++++ .../Errors and Exceptions_20221006235848.py | 45 +++++++++ .../Errors and Exceptions_20221006235936.py | 63 +++++++++++++ .../Errors and Exceptions_20221006235942.py | 63 +++++++++++++ .../Errors and Exceptions_20221006235947.py | 64 +++++++++++++ .../Errors and Exceptions_20221007000016.py | 74 +++++++++++++++ .../Errors and Exceptions_20221007000022.py | 75 +++++++++++++++ .../Errors and Exceptions_20221007000031.py | 83 ++++++++++++++++ .../Examples_20221006233527.txt | 0 .../Examples_20221006233549.txt | 20 ++++ .../Examples_20221006233556.txt | 21 +++++ .../Examples_20221006233604.txt | 22 +++++ .../Introduction_20221006233318.txt | 0 .../Introduction_20221006233322.txt | 6 ++ .../Introduction_20221006233325.txt | 7 ++ .../Introduction_20221006233332.txt | 7 ++ .../Introduction_20221006233336.txt | 7 ++ .../Introduction_20221006233341.txt | 8 ++ .../Introduction_20221006233343.txt | 8 ++ .../Introduction_20221006233346.txt | 8 ++ .../Introduction_20221006233405.txt | 25 +++++ .../Introduction_20221006233408.txt | 25 +++++ .../Introduction_20221006233455.txt | 42 +++++++++ .../Introduction_20221006233458.txt | 43 +++++++++ .../examples_20221007000545.py | 0 .../examples_20221007000556.py | 15 +++ .../printreciprocal_20221007000710.py | 0 .../printreciprocal_20221007000904.py | 10 ++ .../raise-exception_20221006235505.py | 0 .../raise-exception_20221006235518.py | 5 + .../raise-exception_20221006235522.py | 5 + .../raise-exception_20221006235524.py | 6 ++ .../raise-exception_20221006235538.py | 14 +++ .../raise-exception_20221006235541.py | 14 +++ .../raise-exception_20221006235543.py | 15 +++ .../raise-exception_20221006235554.py | 21 +++++ .../raise-exception_20221006235557.py | 21 +++++ .../raise-exception_20221006235559.py | 21 +++++ .../raise-exception_20221006235600.py | 21 +++++ .../raise-exception_20221006235604.py | 21 +++++ .../raise-exception_20221006235609.py | 23 +++++ .../raise-exception_20221006235620.py | 23 +++++ .../raise-exception_20221006235622.py | 24 +++++ ...y-except-else statements_20221006235013.py | 0 ...y-except-else statements_20221006235020.py | 4 + ...y-except-else statements_20221006235023.py | 4 + ...y-except-else statements_20221006235025.py | 4 + ...y-except-else statements_20221006235037.py | 11 +++ ...y-except-else statements_20221006235042.py | 11 +++ ...y-except-else statements_20221006235045.py | 11 +++ ...y-except-else statements_20221006235047.py | 11 +++ ...y-except-else statements_20221006235048.py | 11 +++ ...y-except-else statements_20221006235050.py | 12 +++ ...y-except-else statements_20221006235051.py | 12 +++ ...y-except-else statements_20221006235114.py | 13 +++ ...y-except-else statements_20221006235116.py | 13 +++ ...y-except-else statements_20221006235118.py | 14 +++ ...y-except-else statements_20221006235130.py | 17 ++++ ...y-except-else statements_20221006235135.py | 18 ++++ ...y-except-else statements_20221006235147.py | 21 +++++ ...y-except-else statements_20221006235152.py | 21 +++++ ...y-except-else statements_20221006235153.py | 22 +++++ ...y-except-else statements_20221006235204.py | 27 ++++++ ...y-except-else statements_20221006235206.py | 27 ++++++ ...y-except-else statements_20221006235208.py | 27 ++++++ ...y-except-else statements_20221006235209.py | 28 ++++++ ...y-except-else statements_20221006235210.py | 28 ++++++ ...y-except-else statements_20221006235224.py | 30 ++++++ ...y-except-else statements_20221006235227.py | 30 ++++++ ...y-except-else statements_20221006235231.py | 31 ++++++ .../Catching Exceptions in Python-examples.py | 15 +++ Lesson 7 - Exception Handling/Catching.txt | 78 +++++++++++++++ .../Custom Exceptions.py | 94 +++++++++++++++++++ .../Errors and Exceptions.py | 83 ++++++++++++++++ .../Introduction.txt | 43 +++++++++ .../More into handling exceptions.txt | 22 +++++ .../printreciprocal.py | 10 ++ .../raise-exception.py | 24 +++++ .../try-except-else statements.py | 31 ++++++ 123 files changed, 3780 insertions(+) create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234551.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234554.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234555.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234607.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234609.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234622.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234630.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234639.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234642.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234645.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234646.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234659.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234709.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234711.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234803.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234809.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234811.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234814.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234826.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234851.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234903.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234915.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234920.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234921.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234922.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234924.txt create mode 100644 .history/Lesson 7 - Exception Handling/Catching_20221006234932.txt create mode 100644 .history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000110.py create mode 100644 .history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000114.py create mode 100644 .history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000116.py create mode 100644 .history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000146.py create mode 100644 .history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000153.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235715.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235737.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235742.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235747.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235758.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235803.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235815.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235819.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235820.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235825.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235838.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235844.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235847.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235848.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235936.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235942.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235947.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000016.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000022.py create mode 100644 .history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000031.py create mode 100644 .history/Lesson 7 - Exception Handling/Examples_20221006233527.txt create mode 100644 .history/Lesson 7 - Exception Handling/Examples_20221006233549.txt create mode 100644 .history/Lesson 7 - Exception Handling/Examples_20221006233556.txt create mode 100644 .history/Lesson 7 - Exception Handling/Examples_20221006233604.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233318.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233322.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233325.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233332.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233336.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233341.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233343.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233346.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233405.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233408.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233455.txt create mode 100644 .history/Lesson 7 - Exception Handling/Introduction_20221006233458.txt create mode 100644 .history/Lesson 7 - Exception Handling/examples_20221007000545.py create mode 100644 .history/Lesson 7 - Exception Handling/examples_20221007000556.py create mode 100644 .history/Lesson 7 - Exception Handling/printreciprocal_20221007000710.py create mode 100644 .history/Lesson 7 - Exception Handling/printreciprocal_20221007000904.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235505.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235518.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235522.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235524.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235538.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235541.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235543.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235554.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235557.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235559.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235600.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235604.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235609.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235620.py create mode 100644 .history/Lesson 7 - Exception Handling/raise-exception_20221006235622.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235013.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235020.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235023.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235025.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235037.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235042.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235045.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235047.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235048.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235050.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235051.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235114.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235116.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235118.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235130.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235135.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235147.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235152.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235153.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235204.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235206.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235208.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235209.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235210.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235224.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235227.py create mode 100644 .history/Lesson 7 - Exception Handling/try-except-else statements_20221006235231.py create mode 100644 Lesson 7 - Exception Handling/Catching Exceptions in Python-examples.py create mode 100644 Lesson 7 - Exception Handling/Catching.txt create mode 100644 Lesson 7 - Exception Handling/Custom Exceptions.py create mode 100644 Lesson 7 - Exception Handling/Errors and Exceptions.py create mode 100644 Lesson 7 - Exception Handling/Introduction.txt create mode 100644 Lesson 7 - Exception Handling/More into handling exceptions.txt create mode 100644 Lesson 7 - Exception Handling/printreciprocal.py create mode 100644 Lesson 7 - Exception Handling/raise-exception.py create mode 100644 Lesson 7 - Exception Handling/try-except-else statements.py diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234551.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234551.txt new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234554.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234554.txt new file mode 100644 index 0000000..ee6453d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234554.txt @@ -0,0 +1,10 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234555.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234555.txt new file mode 100644 index 0000000..72912cd --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234555.txt @@ -0,0 +1,10 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234607.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234607.txt new file mode 100644 index 0000000..ffa8069 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234607.txt @@ -0,0 +1,22 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234609.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234609.txt new file mode 100644 index 0000000..ff16f12 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234609.txt @@ -0,0 +1,22 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234622.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234622.txt new file mode 100644 index 0000000..c966f9c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234622.txt @@ -0,0 +1,43 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) +We get the output to this code as: +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234630.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234630.txt new file mode 100644 index 0000000..3f4576c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234630.txt @@ -0,0 +1,44 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234639.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234639.txt new file mode 100644 index 0000000..c0001dc --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234639.txt @@ -0,0 +1,44 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234642.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234642.txt new file mode 100644 index 0000000..12cc361 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234642.txt @@ -0,0 +1,45 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234645.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234645.txt new file mode 100644 index 0000000..6e7db72 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234645.txt @@ -0,0 +1,46 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234646.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234646.txt new file mode 100644 index 0000000..c3bb920 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234646.txt @@ -0,0 +1,47 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234659.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234659.txt new file mode 100644 index 0000000..5b44584 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234659.txt @@ -0,0 +1,52 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234709.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234709.txt new file mode 100644 index 0000000..a50a810 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234709.txt @@ -0,0 +1,52 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234711.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234711.txt new file mode 100644 index 0000000..7860cc1 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234711.txt @@ -0,0 +1,53 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +9 +The entry is a +Oops! occurred. +The entry is 0 +Oops! occured. +The entry is 2 +The reciprocal of 2 is 0.5 \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234803.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234803.txt new file mode 100644 index 0000000..34e86ff --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234803.txt @@ -0,0 +1,55 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234809.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234809.txt new file mode 100644 index 0000000..bb2bc10 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234809.txt @@ -0,0 +1,55 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234811.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234811.txt new file mode 100644 index 0000000..52cf44f --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234811.txt @@ -0,0 +1,56 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() + diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234814.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234814.txt new file mode 100644 index 0000000..333438c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234814.txt @@ -0,0 +1,57 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() + diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234826.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234826.txt new file mode 100644 index 0000000..f5a3f89 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234826.txt @@ -0,0 +1,57 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234851.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234851.txt new file mode 100644 index 0000000..0f24638 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234851.txt @@ -0,0 +1,79 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234903.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234903.txt new file mode 100644 index 0000000..b600db2 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234903.txt @@ -0,0 +1,81 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: + + +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234915.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234915.txt new file mode 100644 index 0000000..7b4c2c2 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234915.txt @@ -0,0 +1,86 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234920.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234920.txt new file mode 100644 index 0000000..ec4b23d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234920.txt @@ -0,0 +1,86 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234921.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234921.txt new file mode 100644 index 0000000..cb8994f --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234921.txt @@ -0,0 +1,86 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234922.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234922.txt new file mode 100644 index 0000000..c242811 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234922.txt @@ -0,0 +1,86 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +10 +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234924.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234924.txt new file mode 100644 index 0000000..1871615 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234924.txt @@ -0,0 +1,85 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +print("The reciprocal of", ele, "is", r) +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Catching_20221006234932.txt b/.history/Lesson 7 - Exception Handling/Catching_20221006234932.txt new file mode 100644 index 0000000..e9497ba --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Catching_20221006234932.txt @@ -0,0 +1,78 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +Arithmetic Exception diff --git a/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000110.py b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000110.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000114.py b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000114.py new file mode 100644 index 0000000..e9d5e32 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000114.py @@ -0,0 +1,55 @@ +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + print(n/m) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') +2 +0 +--------------------------------------------------------------------------- +ZeroDenominatorError Traceback (most recent call last) + in + 6 m = int(input()) + 7 if m==0: +----> 8 raise ZeroDenominatorError('Denominator should not be Zero') + 9 print(n/m) + 10 break + +ZeroDenominatorError: Denominator should not be Zero +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + print(n/m) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') +2 +abc +A & B SHOULD BE INTEGER +1 +0 +--------------------------------------------------------------------------- +ZeroDenominatorError Traceback (most recent call last) + in + 6 m = int(input()) + 7 if m==0: +----> 8 raise ZeroDenominatorError('Denominator should not be Zero') + 9 print(n/m) + 10 break + +ZeroDenominatorError: Denominator should not be Zero \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000116.py b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000116.py new file mode 100644 index 0000000..1b052b3 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000116.py @@ -0,0 +1,55 @@ +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# abc +# A & B SHOULD BE INTEGER +# 1 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000146.py b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000146.py new file mode 100644 index 0000000..46e00fa --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000146.py @@ -0,0 +1,94 @@ +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# abc +# A & B SHOULD BE INTEGER +# 1 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero + +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break +2 +s +A & B SHOULD BE INTEGER +2 +2 +1.0 +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break + finally: + print('OUTPUT PRINTED') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000153.py b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000153.py new file mode 100644 index 0000000..89e30be --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Custom Exceptions_20221007000153.py @@ -0,0 +1,94 @@ +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# abc +# A & B SHOULD BE INTEGER +# 1 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero + +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break +# 2 +# s +# A & B SHOULD BE INTEGER +# 2 +# 2 +# 1.0 +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break + finally: + print('OUTPUT PRINTED') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235715.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235715.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235737.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235737.py new file mode 100644 index 0000000..7d8848c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235737.py @@ -0,0 +1,2 @@ +a=10/0 +a \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235742.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235742.py new file mode 100644 index 0000000..eadbd6d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235742.py @@ -0,0 +1,2 @@ +a=10/0 +print(a) \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235747.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235747.py new file mode 100644 index 0000000..4c80be8 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235747.py @@ -0,0 +1,3 @@ +a=10/0 +print(a) + diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235758.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235758.py new file mode 100644 index 0000000..1a13510 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235758.py @@ -0,0 +1,10 @@ +a=10/0 +print(a) + +--------------------------------------------------------------------------- +ZeroDivisionError Traceback (most recent call last) + in +----> 1 a=10/0 + 2 a + +ZeroDivisionError: division by zero \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235803.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235803.py new file mode 100644 index 0000000..b9172b8 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235803.py @@ -0,0 +1,11 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235815.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235815.py new file mode 100644 index 0000000..7ab48e4 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235815.py @@ -0,0 +1,13 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +a = b*4 +a \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235819.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235819.py new file mode 100644 index 0000000..3bb2ce7 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235819.py @@ -0,0 +1,13 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +a = b*4 +print(a) \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235820.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235820.py new file mode 100644 index 0000000..a682d2d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235820.py @@ -0,0 +1,13 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +a = b*4 +print(a) diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235825.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235825.py new file mode 100644 index 0000000..b7cf3c8 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235825.py @@ -0,0 +1,13 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# print(a) diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235838.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235838.py new file mode 100644 index 0000000..b18f129 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235838.py @@ -0,0 +1,44 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# print(a) +--------------------------------------------------------------------------- +NameError Traceback (most recent call last) + in +----> 1 a = b*4 + 2 a + +NameError: name 'b' is not defined +a = 'b'+4 +a +--------------------------------------------------------------------------- +TypeError Traceback (most recent call last) + in +----> 1 a = 'b'+4 + 2 a + +TypeError: can only concatenate str (not "int") to str +a = int(input('enter value of a')) +b = int(input('enter value of b')) +c = a/b +c +enter value of a12 +enter value of babc +--------------------------------------------------------------------------- +ValueError Traceback (most recent call last) + in + 1 a = int(input('enter value of a')) +----> 2 b = int(input('enter value of b')) + 3 c = a/b + 4 c + +ValueError: invalid literal for int() with base 10: 'abc' \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235844.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235844.py new file mode 100644 index 0000000..0c5c8a7 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235844.py @@ -0,0 +1,44 @@ +a=10/0 +print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235847.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235847.py new file mode 100644 index 0000000..3add53c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235847.py @@ -0,0 +1,44 @@ +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235848.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235848.py new file mode 100644 index 0000000..1ada74b --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235848.py @@ -0,0 +1,45 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235936.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235936.py new file mode 100644 index 0000000..85e5506 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235936.py @@ -0,0 +1,63 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +ENTER THE VALUE:56 +ENTER THE VALUE:gn +A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235942.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235942.py new file mode 100644 index 0000000..5fe7934 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235942.py @@ -0,0 +1,63 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235947.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235947.py new file mode 100644 index 0000000..156c223 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221006235947.py @@ -0,0 +1,64 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000016.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000016.py new file mode 100644 index 0000000..bd4ea99 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000016.py @@ -0,0 +1,74 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000022.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000022.py new file mode 100644 index 0000000..624e19b --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000022.py @@ -0,0 +1,75 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + diff --git a/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000031.py b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000031.py new file mode 100644 index 0000000..2119720 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Errors and Exceptions_20221007000031.py @@ -0,0 +1,83 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except (ValueError,ZeroDivisionError): + print('A & B SHOULD BE INTEGER & B SHOULD NOT BE ZERO') \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Examples_20221006233527.txt b/.history/Lesson 7 - Exception Handling/Examples_20221006233527.txt new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/Examples_20221006233549.txt b/.history/Lesson 7 - Exception Handling/Examples_20221006233549.txt new file mode 100644 index 0000000..f590a6f --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Examples_20221006233549.txt @@ -0,0 +1,20 @@ +Exceptions in Python +● Python has many built-in exceptions that are raised when your program +encounters an error (something in the program goes wrong). +● When these exceptions occur, the Python interpreter stops the current +process and passes it to the calling process until it is handled. +● If not handled, the program will crash. +● For example, let us consider a program where we have a function A that calls +function B, which in turn calls function C. If an exception occurs in function C +but is not handled in C, the exception passes to B and then to A. +● If never handled, an error message is displayed and the program comes to a +sudden unexpected halt. +Some Common Exceptions +A list of common exceptions that can be thrown from a standard Python program is +given below. +● ZeroDivisionError: This occurs when a number is divided by zero. +● NameError: It occurs when a name is not found. It may be local or global. +● IndentationError: It occurs when incorrect indentation is given. +● IOError: It occurs when an Input-Output operation fails. +● EOFError: It occurs when the end of the file is reached, and yet operations +are being performed. diff --git a/.history/Lesson 7 - Exception Handling/Examples_20221006233556.txt b/.history/Lesson 7 - Exception Handling/Examples_20221006233556.txt new file mode 100644 index 0000000..ac72045 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Examples_20221006233556.txt @@ -0,0 +1,21 @@ +Exceptions in Python +------------------------------------------------------------------------- +● Python has many built-in exceptions that are raised when your program +encounters an error (something in the program goes wrong). +● When these exceptions occur, the Python interpreter stops the current +process and passes it to the calling process until it is handled. +● If not handled, the program will crash. +● For example, let us consider a program where we have a function A that calls +function B, which in turn calls function C. If an exception occurs in function C +but is not handled in C, the exception passes to B and then to A. +● If never handled, an error message is displayed and the program comes to a +sudden unexpected halt. +Some Common Exceptions +A list of common exceptions that can be thrown from a standard Python program is +given below. +● ZeroDivisionError: This occurs when a number is divided by zero. +● NameError: It occurs when a name is not found. It may be local or global. +● IndentationError: It occurs when incorrect indentation is given. +● IOError: It occurs when an Input-Output operation fails. +● EOFError: It occurs when the end of the file is reached, and yet operations +are being performed. diff --git a/.history/Lesson 7 - Exception Handling/Examples_20221006233604.txt b/.history/Lesson 7 - Exception Handling/Examples_20221006233604.txt new file mode 100644 index 0000000..a626406 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Examples_20221006233604.txt @@ -0,0 +1,22 @@ +Exceptions in Python +------------------------------------------------------------------------- +● Python has many built-in exceptions that are raised when your program +encounters an error (something in the program goes wrong). +● When these exceptions occur, the Python interpreter stops the current +process and passes it to the calling process until it is handled. +● If not handled, the program will crash. +● For example, let us consider a program where we have a function A that calls +function B, which in turn calls function C. If an exception occurs in function C +but is not handled in C, the exception passes to B and then to A. +● If never handled, an error message is displayed and the program comes to a +sudden unexpected halt. +Some Common Exceptions +--------------------------------------------------------------------------------- +A list of common exceptions that can be thrown from a standard Python program is +given below. +● ZeroDivisionError: This occurs when a number is divided by zero. +● NameError: It occurs when a name is not found. It may be local or global. +● IndentationError: It occurs when incorrect indentation is given. +● IOError: It occurs when an Input-Output operation fails. +● EOFError: It occurs when the end of the file is reached, and yet operations +are being performed. diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233318.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233318.txt new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233322.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233322.txt new file mode 100644 index 0000000..f1ac484 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233322.txt @@ -0,0 +1,6 @@ +Exception Handling +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233325.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233325.txt new file mode 100644 index 0000000..e761379 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233325.txt @@ -0,0 +1,7 @@ +Exception Handling + +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233332.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233332.txt new file mode 100644 index 0000000..46107ed --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233332.txt @@ -0,0 +1,7 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233336.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233336.txt new file mode 100644 index 0000000..e4225b4 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233336.txt @@ -0,0 +1,7 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233341.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233341.txt new file mode 100644 index 0000000..1b5a718 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233341.txt @@ -0,0 +1,8 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +---------------------------------------------------------------------------------- diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233343.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233343.txt new file mode 100644 index 0000000..d68f7db --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233343.txt @@ -0,0 +1,8 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +---------------------------------------------------------------------------------- \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233346.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233346.txt new file mode 100644 index 0000000..38b0047 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233346.txt @@ -0,0 +1,8 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233405.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233405.txt new file mode 100644 index 0000000..7f1f7eb --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233405.txt @@ -0,0 +1,25 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- +Difference between Syntax Errors and Exceptions +Syntax Error: As the name suggests this error is caused by the wrong syntax in the +code. It leads to the termination of the program. +Example: +Consider the given code snippet: +We will get the output as: +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233408.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233408.txt new file mode 100644 index 0000000..4e220c2 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233408.txt @@ -0,0 +1,25 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- +Difference between Syntax Errors and Exceptions +Syntax Error: As the name suggests this error is caused by the wrong syntax in the +code. It leads to the termination of the program. +Example: +Consider the given code snippet: +We will get the output as: +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233455.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233455.txt new file mode 100644 index 0000000..e787151 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233455.txt @@ -0,0 +1,42 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- +Difference between Syntax Errors and Exceptions +Syntax Error: As the name suggests this error is caused by the wrong syntax in the +code. It leads to the termination of the program. +Example: +Consider the given code snippet: +We will get the output as: +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +marks = 10000 +a = marks / 0 +print(a) +Output: +The above example raised the ZeroDivisionError exception, as we are trying to +divide a number by 0 which is not defined. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/Introduction_20221006233458.txt b/.history/Lesson 7 - Exception Handling/Introduction_20221006233458.txt new file mode 100644 index 0000000..1e70992 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/Introduction_20221006233458.txt @@ -0,0 +1,43 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- +Difference between Syntax Errors and Exceptions +Syntax Error: As the name suggests this error is caused by the wrong syntax in the +code. It leads to the termination of the program. +Example: +Consider the given code snippet: +We will get the output as: +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +marks = 10000 +a = marks / 0 +print(a) +Output: +The above example raised the ZeroDivisionError exception, as we are trying to +divide a number by 0 which is not defined. + diff --git a/.history/Lesson 7 - Exception Handling/examples_20221007000545.py b/.history/Lesson 7 - Exception Handling/examples_20221007000545.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/examples_20221007000556.py b/.history/Lesson 7 - Exception Handling/examples_20221007000556.py new file mode 100644 index 0000000..f5f15be --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/examples_20221007000556.py @@ -0,0 +1,15 @@ +# import module sys to get the type of exception +import sys + +randomList = ['a', 0, 2] + +for entry in randomList: + try: + print("The entry is", entry) + r = 1/int(entry) + break + except: + print("Oops!", sys.exc_info()[0], "occurred.") + print("Next entry.") + print() +print("The reciprocal of", entry, "is", r) \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/printreciprocal_20221007000710.py b/.history/Lesson 7 - Exception Handling/printreciprocal_20221007000710.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/printreciprocal_20221007000904.py b/.history/Lesson 7 - Exception Handling/printreciprocal_20221007000904.py new file mode 100644 index 0000000..dab7a64 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/printreciprocal_20221007000904.py @@ -0,0 +1,10 @@ +# program to print the reciprocal of even numbers + +try: + num = int(input("Enter a number: ")) + assert num % 2 == 0 +except: + print("Not an even number!") +else: + reciprocal = 1/num + print(reciprocal) \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235505.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235505.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235518.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235518.py new file mode 100644 index 0000000..cee9a77 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235518.py @@ -0,0 +1,5 @@ +Raising Exceptions in Python +In Python programming, exceptions are raised when errors occur at runtime. We +can also manually raise exceptions using the raise keyword. We can optionally +pass values to the exception to clarify why that exception was raised. Given below +are some examples to help you understand this better \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235522.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235522.py new file mode 100644 index 0000000..dcae5a7 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235522.py @@ -0,0 +1,5 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235524.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235524.py new file mode 100644 index 0000000..5d70c06 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235524.py @@ -0,0 +1,6 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235538.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235538.py new file mode 100644 index 0000000..22748c7 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235538.py @@ -0,0 +1,14 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +>>> raise KeyboardInterrupt +Traceback (most recent call last): +... +KeyboardInterrupt +>>> raise MemoryError("This is an argument") +Traceback (most recent call last): +... +MemoryError: This is an argument \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235541.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235541.py new file mode 100644 index 0000000..065e0b6 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235541.py @@ -0,0 +1,14 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235543.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235543.py new file mode 100644 index 0000000..6173d17 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235543.py @@ -0,0 +1,15 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235554.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235554.py new file mode 100644 index 0000000..be32c6d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235554.py @@ -0,0 +1,21 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235557.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235557.py new file mode 100644 index 0000000..f6dcc1b --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235557.py @@ -0,0 +1,21 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235559.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235559.py new file mode 100644 index 0000000..ab1cafb --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235559.py @@ -0,0 +1,21 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235600.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235600.py new file mode 100644 index 0000000..a9f3dfd --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235600.py @@ -0,0 +1,21 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235604.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235604.py new file mode 100644 index 0000000..67d06e8 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235604.py @@ -0,0 +1,21 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235609.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235609.py new file mode 100644 index 0000000..2d8cfcf --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235609.py @@ -0,0 +1,23 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) + + diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235620.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235620.py new file mode 100644 index 0000000..a75b56c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235620.py @@ -0,0 +1,23 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) + +# Output: That is not a positive number! diff --git a/.history/Lesson 7 - Exception Handling/raise-exception_20221006235622.py b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235622.py new file mode 100644 index 0000000..50da5f4 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/raise-exception_20221006235622.py @@ -0,0 +1,24 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) + +# Output: That is not a positive number! + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235013.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235013.py new file mode 100644 index 0000000..e69de29 diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235020.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235020.py new file mode 100644 index 0000000..acf842d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235020.py @@ -0,0 +1,4 @@ +try-except-else Statements +We can also use the else statement with the try-except statement in which, we can +place the code which will be executed in the scenario if no exception occurs in the +else block. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235023.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235023.py new file mode 100644 index 0000000..bc9e8e4 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235023.py @@ -0,0 +1,4 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235025.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235025.py new file mode 100644 index 0000000..88b7dc3 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235025.py @@ -0,0 +1,4 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235037.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235037.py new file mode 100644 index 0000000..6225637 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235037.py @@ -0,0 +1,11 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235042.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235042.py new file mode 100644 index 0000000..a54a6d7 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235042.py @@ -0,0 +1,11 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235045.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235045.py new file mode 100644 index 0000000..22f079d --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235045.py @@ -0,0 +1,11 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235047.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235047.py new file mode 100644 index 0000000..2f89433 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235047.py @@ -0,0 +1,11 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235048.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235048.py new file mode 100644 index 0000000..9724e9e --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235048.py @@ -0,0 +1,11 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235050.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235050.py new file mode 100644 index 0000000..bd4b9fe --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235050.py @@ -0,0 +1,12 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235051.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235051.py new file mode 100644 index 0000000..83ad726 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235051.py @@ -0,0 +1,12 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235114.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235114.py new file mode 100644 index 0000000..f34e104 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235114.py @@ -0,0 +1,13 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235116.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235116.py new file mode 100644 index 0000000..ae5379f --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235116.py @@ -0,0 +1,13 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235118.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235118.py new file mode 100644 index 0000000..bd0ab4e --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235118.py @@ -0,0 +1,14 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235130.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235130.py new file mode 100644 index 0000000..2bf4060 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235130.py @@ -0,0 +1,17 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +We get this output because there is no exception in the try block and hence the else +block is executed. If there was an exception in the try block, the else block will be +skipped and except block will be executed \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235135.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235135.py new file mode 100644 index 0000000..d19e2e2 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235135.py @@ -0,0 +1,18 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235147.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235147.py new file mode 100644 index 0000000..c69cdc2 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235147.py @@ -0,0 +1,21 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +The try statement in Python can have an optional finally clause. This clause is +executed no matter what and is generally used to release external resources. +Here is an example of file operations to illustrate this: \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235152.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235152.py new file mode 100644 index 0000000..56682b4 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235152.py @@ -0,0 +1,21 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235153.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235153.py new file mode 100644 index 0000000..0ef8b99 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235153.py @@ -0,0 +1,22 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235204.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235204.py new file mode 100644 index 0000000..9ceb66a --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235204.py @@ -0,0 +1,27 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235206.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235206.py new file mode 100644 index 0000000..871cd45 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235206.py @@ -0,0 +1,27 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235208.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235208.py new file mode 100644 index 0000000..db07d7a --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235208.py @@ -0,0 +1,27 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235209.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235209.py new file mode 100644 index 0000000..8ef6302 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235209.py @@ -0,0 +1,28 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235210.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235210.py new file mode 100644 index 0000000..4079bbf --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235210.py @@ -0,0 +1,28 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235224.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235224.py new file mode 100644 index 0000000..47f872c --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235224.py @@ -0,0 +1,30 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + +This type of construct makes sure that the file is closed even if an exception occurs +during the program execution \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235227.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235227.py new file mode 100644 index 0000000..21934ef --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235227.py @@ -0,0 +1,30 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + +# This type of construct makes sure that the file is closed even if an exception occurs +# during the program execution \ No newline at end of file diff --git a/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235231.py b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235231.py new file mode 100644 index 0000000..4ec2675 --- /dev/null +++ b/.history/Lesson 7 - Exception Handling/try-except-else statements_20221006235231.py @@ -0,0 +1,31 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + +# This type of construct makes sure that the file is closed even if an exception occurs +# during the program execution + diff --git a/Lesson 7 - Exception Handling/Catching Exceptions in Python-examples.py b/Lesson 7 - Exception Handling/Catching Exceptions in Python-examples.py new file mode 100644 index 0000000..f5f15be --- /dev/null +++ b/Lesson 7 - Exception Handling/Catching Exceptions in Python-examples.py @@ -0,0 +1,15 @@ +# import module sys to get the type of exception +import sys + +randomList = ['a', 0, 2] + +for entry in randomList: + try: + print("The entry is", entry) + r = 1/int(entry) + break + except: + print("Oops!", sys.exc_info()[0], "occurred.") + print("Next entry.") + print() +print("The reciprocal of", entry, "is", r) \ No newline at end of file diff --git a/Lesson 7 - Exception Handling/Catching.txt b/Lesson 7 - Exception Handling/Catching.txt new file mode 100644 index 0000000..e9497ba --- /dev/null +++ b/Lesson 7 - Exception Handling/Catching.txt @@ -0,0 +1,78 @@ +Catching Exceptions +In Python, exceptions can be handled using try-except blocks. +● If the Python program contains suspicious code that may throw the +exception, we must place that code in the try block. +● The try block must be followed by the except statement, which contains a +block of code that will be executed in case there is some exception in the try +block. +● We can thus choose what operations to perform once we have caught the +exception. +● Here is a simple example: + +# import module sys to get the type of exception +import sys +l = ['a', 0, 2] +for ele in l: + try:#This block might raise an exception while executing + print("The entry is", ele) + r = 1/int(ele) + break + except:#This block executes in case of an exception in "try" + print("Oops!", sys.exc_info()[0], "occurred.") + print() +print("The reciprocal of", ele, "is", r) + +We get the output to this code as: + + The entry is a + Oops! occurred. + The entry is 0 + Oops! occured. + The entry is 2 + The reciprocal of 2 is 0.5 + +● In this program, we loop through the values of a list l. +● As previously mentioned, the portion that can cause an exception is placed +inside the try block. +● If no exception occurs, the except block is skipped and normal flow +continues(for last value). +● But if any exception occurs, it is caught by the except block (first and second +values). +● Here, we print the name of the exception using the exc_info() function +inside sys module. +● We can see that element “a” causes ValueError and 0 causes +ZeroDivisionError. +Every exception in Python inherits from the base Exception class. Thus we can +write the above code as: + +l = ['a', 0, 2] +for ele in l: + try: + print("The entry is", ele) + r = 1/int(ele) + except Exception as e: #Using Exception class + print("Oops!", e.__class__, "occurred.") + print("Next entry.") + print() +print("The reciprocal of", ele, "is", r) + +Catching Specific Exceptions in Python +● In the above example, we did not mention any specific exception in the +except clause. +● This is not a good programming practice as it will catch all exceptions and +handle every case in the same way. +● We can specify which exceptions an except clause should catch. +● A try clause can have any number of except clauses to handle different +exceptions, however, only one will be executed in case an exception occurs. +● You can use multiple except blocks for different types of exceptions. +● We can even use a tuple of values to specify multiple exceptions in an except +clause. Here is an example to understand this better: +try: + a=10/0; +except(ArithmeticError, IOError): + print("Arithmetic Exception") +else: + print("Successfully Done") + +Output: +Arithmetic Exception diff --git a/Lesson 7 - Exception Handling/Custom Exceptions.py b/Lesson 7 - Exception Handling/Custom Exceptions.py new file mode 100644 index 0000000..89e30be --- /dev/null +++ b/Lesson 7 - Exception Handling/Custom Exceptions.py @@ -0,0 +1,94 @@ +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero +# class ZeroDenominatorError(Exception): +# pass +# while True: +# try: +# n = int(input()) +# m = int(input()) +# if m==0: +# raise ZeroDenominatorError('Denominator should not be Zero') +# print(n/m) +# break +# except ValueError: +# print('A & B SHOULD BE INTEGER') +# except ZeroDivisionError: +# print('B SHOULD NOT BE ZERO') +# 2 +# abc +# A & B SHOULD BE INTEGER +# 1 +# 0 +# --------------------------------------------------------------------------- +# ZeroDenominatorError Traceback (most recent call last) +# in +# 6 m = int(input()) +# 7 if m==0: +# ----> 8 raise ZeroDenominatorError('Denominator should not be Zero') +# 9 print(n/m) +# 10 break + +# ZeroDenominatorError: Denominator should not be Zero + +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break +# 2 +# s +# A & B SHOULD BE INTEGER +# 2 +# 2 +# 1.0 +class ZeroDenominatorError(Exception): + pass +while True: + try: + n = int(input()) + m = int(input()) + if m==0: + raise ZeroDenominatorError('Denominator should not be Zero') + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + else: + print(n/m) + break + finally: + print('OUTPUT PRINTED') \ No newline at end of file diff --git a/Lesson 7 - Exception Handling/Errors and Exceptions.py b/Lesson 7 - Exception Handling/Errors and Exceptions.py new file mode 100644 index 0000000..2119720 --- /dev/null +++ b/Lesson 7 - Exception Handling/Errors and Exceptions.py @@ -0,0 +1,83 @@ + +# a=10/0 +# print(a) + +# --------------------------------------------------------------------------- +# ZeroDivisionError Traceback (most recent call last) +# in +# ----> 1 a=10/0 +# 2 a + +# ZeroDivisionError: division by zero + +# a = b*4 +# # print(a) +# --------------------------------------------------------------------------- +# NameError Traceback (most recent call last) +# in +# ----> 1 a = b*4 +# 2 a + +# NameError: name 'b' is not defined +# a = 'b'+4 +# a +# --------------------------------------------------------------------------- +# TypeError Traceback (most recent call last) +# in +# ----> 1 a = 'b'+4 +# 2 a + +# TypeError: can only concatenate str (not "int") to str +# a = int(input('enter value of a')) +# b = int(input('enter value of b')) +# c = a/b +# c +# enter value of a12 +# enter value of babc +# --------------------------------------------------------------------------- +# ValueError Traceback (most recent call last) +# in +# 1 a = int(input('enter value of a')) +# ----> 2 b = int(input('enter value of b')) +# 3 c = a/b +# 4 c + +# ValueError: invalid literal for int() with base 10: 'abc' + +try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) +except: + print('A & B SHOULD BE INTEGER') +# ENTER THE VALUE:56 +# ENTER THE VALUE:gn +# A & B SHOULD BE INTEGER +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except ValueError: + print('A & B SHOULD BE INTEGER') + except ZeroDivisionError: + print('B SHOULD NOT BE ZERO') + +while True: + try: + A = int(input('ENTER THE VALUE:')) + B = int(input('ENTER THE VALUE:')) + print(A/B) + break + except (ValueError,ZeroDivisionError): + print('A & B SHOULD BE INTEGER & B SHOULD NOT BE ZERO') \ No newline at end of file diff --git a/Lesson 7 - Exception Handling/Introduction.txt b/Lesson 7 - Exception Handling/Introduction.txt new file mode 100644 index 0000000..1e70992 --- /dev/null +++ b/Lesson 7 - Exception Handling/Introduction.txt @@ -0,0 +1,43 @@ +Exception Handling +--------------------------------------------------------------------------------- +Error in Python can be of two types i.e. Syntax errors and Exceptions. +● Errors are the problems in a program due to which the program will stop the +execution. +● On the other hand, exceptions are raised when some internal events occur +which changes the normal flow of the program. +--------------------------------------------------------------------------------- +Difference between Syntax Errors and Exceptions +Syntax Error: As the name suggests this error is caused by the wrong syntax in the +code. It leads to the termination of the program. +Example: +Consider the given code snippet: +We will get the output as: +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +The syntax error is because there should be a “:” at the end of an if statement. +Since it is not present, it gives a syntax error. +Exceptions: Exceptions are raised when the program is syntactically correct but the +code resulted in an error. This error does not stop the execution of the program, +however, it changes the normal flow of the program. +Example: +6 +amount = 10000 +if(amount>2999) + print("Something") +SyntaxError: invalid syntax +marks = 10000 +a = marks / 0 +print(a) +Output: +The above example raised the ZeroDivisionError exception, as we are trying to +divide a number by 0 which is not defined. + diff --git a/Lesson 7 - Exception Handling/More into handling exceptions.txt b/Lesson 7 - Exception Handling/More into handling exceptions.txt new file mode 100644 index 0000000..a626406 --- /dev/null +++ b/Lesson 7 - Exception Handling/More into handling exceptions.txt @@ -0,0 +1,22 @@ +Exceptions in Python +------------------------------------------------------------------------- +● Python has many built-in exceptions that are raised when your program +encounters an error (something in the program goes wrong). +● When these exceptions occur, the Python interpreter stops the current +process and passes it to the calling process until it is handled. +● If not handled, the program will crash. +● For example, let us consider a program where we have a function A that calls +function B, which in turn calls function C. If an exception occurs in function C +but is not handled in C, the exception passes to B and then to A. +● If never handled, an error message is displayed and the program comes to a +sudden unexpected halt. +Some Common Exceptions +--------------------------------------------------------------------------------- +A list of common exceptions that can be thrown from a standard Python program is +given below. +● ZeroDivisionError: This occurs when a number is divided by zero. +● NameError: It occurs when a name is not found. It may be local or global. +● IndentationError: It occurs when incorrect indentation is given. +● IOError: It occurs when an Input-Output operation fails. +● EOFError: It occurs when the end of the file is reached, and yet operations +are being performed. diff --git a/Lesson 7 - Exception Handling/printreciprocal.py b/Lesson 7 - Exception Handling/printreciprocal.py new file mode 100644 index 0000000..dab7a64 --- /dev/null +++ b/Lesson 7 - Exception Handling/printreciprocal.py @@ -0,0 +1,10 @@ +# program to print the reciprocal of even numbers + +try: + num = int(input("Enter a number: ")) + assert num % 2 == 0 +except: + print("Not an even number!") +else: + reciprocal = 1/num + print(reciprocal) \ No newline at end of file diff --git a/Lesson 7 - Exception Handling/raise-exception.py b/Lesson 7 - Exception Handling/raise-exception.py new file mode 100644 index 0000000..50da5f4 --- /dev/null +++ b/Lesson 7 - Exception Handling/raise-exception.py @@ -0,0 +1,24 @@ +# Raising Exceptions in Python +# In Python programming, exceptions are raised when errors occur at runtime. We +# can also manually raise exceptions using the raise keyword. We can optionally +# pass values to the exception to clarify why that exception was raised. Given below +# are some examples to help you understand this better + +# >>> raise KeyboardInterrupt +# Traceback (most recent call last): +# ... +# KeyboardInterrupt +# >>> raise MemoryError("This is an argument") +# Traceback (most recent call last): +# ... +# MemoryError: This is an argument + +try: + a = -2 + if a <= 0: + raise ValueError("That is not a positive number!") +except ValueError as ve: + print(ve) + +# Output: That is not a positive number! + diff --git a/Lesson 7 - Exception Handling/try-except-else statements.py b/Lesson 7 - Exception Handling/try-except-else statements.py new file mode 100644 index 0000000..4ec2675 --- /dev/null +++ b/Lesson 7 - Exception Handling/try-except-else statements.py @@ -0,0 +1,31 @@ +# try-except-else Statements +# We can also use the else statement with the try-except statement in which, we can +# place the code which will be executed in the scenario if no exception occurs in the +# else block. +try: + c = 2/1 +except Exception as e: + print("can't divide by zero") + print(e) +else: + print("Hi I am else block") + +# Output: Hi I am else block + +# We get this output because there is no exception in the try block and hence the else +# block is executed. If there was an exception in the try block, the else block will be +# skipped and except block will be executed + +# The try statement in Python can have an optional finally clause. This clause is +# executed no matter what and is generally used to release external resources. +# Here is an example of file operations to illustrate this: + +try: + f = open("test.txt",encoding = 'utf-8') + # perform file operations +finally: + f.close() + +# This type of construct makes sure that the file is closed even if an exception occurs +# during the program execution +