파이썬[Python]: keyword - raise 키워드

내장함수 - raise 키워드(keyword)


/// 설명

예외(오류)를 발생시킵니다.

실행 후 차례로 주석처리하시면서 확인해 보시기 바랍니다.

※ 형식
raise

reference
https://docs.python.org/3/tutorial/errors.html
https://docs.python.org/3/library/exceptions.html

/// 예제

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
61
62
63
64
65
66
67
raise
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 1, in <module>
#     raise
# RuntimeError: No active exception to reraise
 
 
raise Exception
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 8, in <module>
#     raise Exception
# Exception
 
 
raise Exception('Write Exception')
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 14, in <module>
#     raise Exception('Write Exception')
# Exception: Write Exception
 
 
def func_a():
    raise ConnectionError
 
func_a()
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 24, in <module>
#     func_a()
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 22, in func
#     raise ConnectionError
# ConnectionError
 
 
def func_b():
    raise ConnectionError
 
try:
    func_b()
except ConnectionError as exc:
    raise RuntimeError('Failed to open database'from None
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 35, in <module>
#     raise RuntimeError('Failed to open database') from None
# RuntimeError: Failed to open database
 
 
def func_c():
    raise ConnectionError
 
try:
    func_c()
except ConnectionError as exc:
    raise RuntimeError('Failed to open database'from exc
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 50, in <module>
#     func_c()
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 47, in func_c
#     raise ConnectionError
# ConnectionError
#
# The above exception was the direct cause of the following exception:
#
# Traceback (most recent call last):
#   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 52, in <module>
#     raise RuntimeError('Failed to open database') from exc
# RuntimeError: Failed to open database
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수