STACKBASE

[Python] raise 구문 본문

프로그래밍/Python

[Python] raise 구문

잡뿌 2021. 11. 21. 21:39
반응형

1. 에러를 강제로 발생시킬 때 사용

 

2. 형태

raise 예외("에러 메세지")

3. 에러 만들기

class 예외(Exception):
	def __init__(self):
    		super().__init__("에러 메세지")

4. 예제

class PositiveError(Exception):
    def __init__(self):
        super().__init__("양수 입력 불가!")

try:
    val = int(input('input negative number: '))
    if val >= 0:
        raise PositiveError
        
except PositiveError as e:
    print("에러 발생", e)
반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python] map, filter 함수  (0) 2021.11.28
[Python] lambda function  (0) 2021.11.28
[Python] 위치/키워드 가변 매개변수  (0) 2021.11.28
[Python] 리스트 할당과 복사  (0) 2021.11.24
[Python] try, except 구문  (0) 2021.11.21