Try/Except/Else/Finally Statement

try/except/else/finally គឺ​ជា statement តំរូវ​អោយ​សាកល្បង​អនុវត្ត block នៃ statement នៅ​ក្នុង statement try, តំរូវ​អោយ​អនុវត្ត block នៃ statement នៅ​ក្នុង​ statement except ក្នុង​ករណី​មាន exception កើត​មាន​ឡើង, តំរូវ​អោយ​អនុវត្ត​ block នៃ statement នៅ​ក្នុង statement else ក្នុង​ករណី​គ្មាន exception, និង​តំរូវ​អោយ​អនុវត្ត block នៃ statement នៅ​ក្នុង statement finally ទោះ​បី​ជា​មាន​ឬ​គ្មាន​ exception យ៉ាង​ណា​ក៏​ដោយ​។

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try:
    5/5
 
except:
    print('Exception occurred!')
    print('Block of statement ends here.')
 
else:
    print("No exception occurred")
 
finally:
    print('This statement is always executed.')
 
print('The program ends here.')
1
2
3
No exception occurred
This statement is always executed.
The program ends here.