Popular Posts

Thursday, September 3, 2020

Python Beginner: Try & Except

 Hello, Developer!


Today, we will learn "Try and Except". "Try and Except" feature enables you to catch an error and to handle it. You can identify each specific errors and execute designated codes by specific error. 


Try and Except command form 

try:

      codes # Indented codes will be examined and if it has an error, it will execute "Except" codes. 

except: # If you want to handle whole errors in "Try" codes


try: 

      codes

except ZeroDivisionError as error: # If "Try" codes has "ZeroDivisionError", following codes will be executed.

      codes  

except ValueError: # If "Try" codes has "ValueError", following codes will be executed.

      codes

  

I showed full "Try and Except" exercise codes and execution result as following.

try:
answer = 10 / 0
except ZeroDivisionError as err:
print(err)

try:
number = int(input("Enter a number: "))
print(number)
except ValueError:
print("Invalid Input")

Can you see the same result on your PC? Great job! Now you can handle error & exceptional cases.


See you in the next post, Python developer! 😊

No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...