From ee1870baff9ed3f947c95ee2769ce20b2dad0b85 Mon Sep 17 00:00:00 2001 From: Sagor Sarker Date: Mon, 20 Jan 2020 19:20:31 +0600 Subject: [PATCH] added answer under Python/Explain Exception.... --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index a664203..bcf55ff 100644 --- a/README.md +++ b/README.md @@ -3230,6 +3230,29 @@ Generally, every compiling process have a two steps.
Explain Exception Handling and how to use it in Python
+ +**Exceptions:** Errors detected during execution are called Exceptions. + +**Handling Exception:** When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
+Exceptions can be handled using `try` and `except` statement in python. + +**Example:** Following example asks the user for input until a valid integer has been entered.
+If user enter a non-integer value it will raise exception and using except it will catch that exception and ask the user to enter valid integer again. + + +```py +while True: + try: + a = int(input("please enter an integer value: ")) + print(a) + break + except: + print("Ops! Please enter a valid integer value.") + +``` + +For more details about errors and exceptions follow this [https://docs.python.org/3/tutorial/errors.html](https://docs.python.org/3/tutorial/errors.html) +