-
[python] try-except 이용 시 에러 메세지 및 에러가 발생된 라인 넘버 알아내기IT/python 2022. 1. 4. 13:35
try-except문을 사용할 때에는 항상 except Exception as e: print(str(e)) 이런 식으로 에러 메세지만 확인했었다.
하지만 이런 '0' 에러메세지를 마주할 때면 이게 도대체 뭘 의미하는지 도통 알 수 도 없고,
코드 어디 부분에서 발생했는지 또한 알 수가 없었다.
# 코드
########################################################################################
import os import sys try: a = 1 b = 'a' c = a + b except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(f'file name: {str(fname)}') print(f'error type: {str(exc_type)}') print(f'error msg: {str(e)}') print(f'line number: {str(exc_tb.tb_lineno)}')########################################################################################
# 실행화면
########################################################################################

########################################################################################
'IT > python' 카테고리의 다른 글
[python][대신증권] cybos 2년 분봉데이터 받기 (0) 2022.01.04 [python][대신증권] cybos 자동로그인 (0) 2022.01.04 [python] mysql 이용하기 (0) 2021.12.12 [python] trading view webhook 수신하기 (0) 2021.12.12 [python] pyautogui를 이용한 카톡 메세지 전송 (0) 2021.12.12