Skip to content

Commit 62d2bc8

Browse files
authored
added else statement with try except
also added some more explanation
1 parent a097b55 commit 62d2bc8

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

exception-handling.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# something more about try except
2+
# basic syntax
3+
'''
4+
try:
5+
code1
6+
7+
except:
8+
some code that will execute if code 1 fails or raise some error
9+
10+
else:
11+
this code is executed only if try was succesful i.e no error in code1
12+
13+
finally:
14+
15+
this code will execute in every situation if try fails or not
16+
'''
17+
118
filename = 'exception_data.txt'
219
# Outer try block catches file name or file doesn't exist errors.
320
try:
@@ -28,4 +45,22 @@ def this_fails():
2845
try:
2946
this_fails()
3047
except ZeroDivisionError as err:
31-
print('Handling run-time error:', err)
48+
print('Handling run-time error:', err)
49+
50+
51+
def divide_me(n):
52+
x = 1/n
53+
54+
i = int(input('enter a number '))
55+
try:
56+
divide_me(i)
57+
58+
except Exception as e:
59+
print(e) # this will print the error msg but don't kill the execution of program
60+
61+
else:
62+
print('Your Code Run Successfully') # this will execute if divide_me(i) run sucessfully without an error
63+
64+
finally:
65+
print('thanks') # this will execute in every condition
66+

0 commit comments

Comments
 (0)