-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_01_Error_Exception.py
More file actions
60 lines (54 loc) · 1.46 KB
/
Copy path06_01_Error_Exception.py
File metadata and controls
60 lines (54 loc) · 1.46 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import time
def causerError():
start = time.time()
try:
time.sleep(0.5)
return 1/0
except Exception as e:
print(e)
print(type(e))
finally:
print(f'this took {time.time() - start} seconds to execute')
def causerMultipleError():
try:
return 1 + 'a'
except TypeError as typeError:
print(f'There was type error {typeError}')
except ZeroDivisionError as zeroDivisionError:
print(f'There was zero by divide error {zeroDivisionError}')
except Exception as e:
print(e)
print(type(e))
finally:
print('We have reached finally')
#### Code for custom decorator
def handleException(func):
def wrapper(*args):
try:
func(*args)
except TypeError as typeError:
print(f'There was type error {typeError}')
except ZeroDivisionError as zeroDivisionError:
print(f'There was zero by divide error {zeroDivisionError}')
except Exception as e:
print('There is some error')
print(type(e))
finally:
print('We have reached finally')
return wrapper
@handleException
def customDecorater():
return 1/0
## Raise Exception
@handleException
def raiseError(n):
if n == 0:
raise Exception()
print(n)
causerError()
print('Multiple Error........')
causerMultipleError()
print('Custom Decorator........')
customDecorater()
print('Raise Exception........')
raiseError(0)