-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_except.py
34 lines (25 loc) · 1.22 KB
/
try_except.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# try() is used in Error and Exception Handling
# There are two kinds of errors :
# Syntax Error : Also known as Parsing Errors, most basic. Arise when the Python parser is unable to understand a line of code.
# Exception : Errors which are detected during execution. eg – ZeroDivisionError.
# List of Exception Errors :
# IOError : if file can’t be opened
# KeyboardInterrupt : when an unrequired key is pressed by the user
# ValueError : when built-in function receives a wrong argument
# EOFError : if End-Of-File is hit without reading any data
# ImportError : if it is unable to find the module
# How try() works?
# First try clause is executed i.e. the code between try and except clause.
# If there is no exception, then only try clause will run, except clause is finished.
# If any exception occured, try clause will be skipped and except clause will run.
# Python code to illustrate
# working of try()
def divide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result = x // y
print("Yeah ! Your answer is :", result)
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
# Look at parameters and note the working of Program
divide(3, 2)