DEV Community

Cover image for Learning Python-Basic course: Day 14, Basic Exception and error handling using try-except
Aatmaj
Aatmaj

Posted on • Updated on

Learning Python-Basic course: Day 14, Basic Exception and error handling using try-except

Today we will cover exception handling in Python by using the try and except statements.


The try statement is used to prevent errors while coding. You might try to pop from an empty list, process user input character as an int or divide by zero, this statement has got you covered!.

try:
 #statement1- try to execute, but if any error is returned, then do not execute
except:
 #statement2- execute if any error occurs
finally: #optional
 #Statement2- execute nevertheless
Enter fullscreen mode Exit fullscreen mode

The try & except statements provide a robust alternative for the if-else statement. But this comes at the cost of a little more difficulty while debugging, when unexpected errors are not displayed. This is somewhat similar too the throw catch statements in C.

Here is a program which error checks if the user enters a number or not.

a=input("Please enter a number ")
try:
    b=int(a)
    print(b)
except:
    print("OOPS! You entered a non-numeric character! ")
finally:
    print("End of program!")
Enter fullscreen mode Exit fullscreen mode

OUTPUT-

Please enter a number 6
6
End of program!
Enter fullscreen mode Exit fullscreen mode
Please enter a number g
OOPS! You entered a non-numeric character! 
End of program!
Enter fullscreen mode Exit fullscreen mode

According to the python documentation here, there are three types of errors as shown-

>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
Enter fullscreen mode Exit fullscreen mode

We can specify multiple error handling as shown-

try:
  print(x)
except ZeroDivisionError:
  print("Cannot divide by zero.")
except:
  print("Something else went wrong")
Enter fullscreen mode Exit fullscreen mode

Pass statement

The except statement is compulsory, means that even if you do not want anything to happen if there is an exception, you still have to write it. Here is where the pass statement comes into play. The pass statement is basically a 'no-operation' statement in Python.

try:
  #try to execute
except:
    pass
#do nothing
Enter fullscreen mode Exit fullscreen mode

Exercise

1) Solve the exercise 2 of the day 13 using the try except. Answer


✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me or fork this file 😉
Thank you all👍

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too!

Top comments (1)

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Nice!