728x90
728x90
try-except 문 사용할 때 에러 발생 시, 전체 에러 정보 표시 방법
들어가며
try-except
문을 사용할 때, 에러를 표시하고 싶을 때가 있다.- 다음과 같이 에러를 표시할 경우, 에러 정보 중 1줄 정도만 짧게 출력된다.
- 이때 1줄이 아닌, 여러 줄의 모든 에러 정보가 표시되도록 하는 방법을 정리해본다.
try: print(1 / 0) except Exception as e: print(e)
division by zero

방법
traceback
모듈 사용하기
traceback
모듈을import
하여try-except
문의 예외 처리 부분에 다음과 같이 넣어주면 된다.
import traceback try: print(1 / 0) except Exception as e: print(e) traceback.print_exc() # 전체 오류 내역 출력하기
[ERROR] An exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero
참고 사이트
traceback — Print or retrieve a stack traceback
Source code: Lib/traceback.py This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when i...
docs.python.org
728x90
728x90
'Programming > Python' 카테고리의 다른 글
[Python] print 문 출력 문자에 색깔 넣는 방법 (ANSI Escape Code) (1) | 2024.11.29 |
---|---|
[Python] loc, iloc 차이점 비교 (Pandas) (0) | 2024.11.22 |
[Python] Pyinstaller로 패키징할 때 환경 변수 파일(.env) 인식 안되는 문제 해결 방법 (0) | 2024.10.30 |
[Python] 환경 변수 파일(.env) 다루는 방법 (0) | 2024.09.27 |
[Python] 데이터 전처리 할 때 결측값 제거 방법 (Pandas) (1) | 2024.06.21 |
[Python] or 연산자와 | 연산자의 차이 (0) | 2024.05.29 |
[Python] self (0) | 2023.11.29 |
[Python] ORM(Object Relational Mapping) 라이브러리 사용해보기 (SQLAlchemy) (0) | 2023.11.14 |