ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.3 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.geeksforgeeks.org/python/python-print-exception/ |
| Last Crawled | 2026-04-07 10:41:18 (10 days ago) |
| First Indexed | 2025-06-15 21:59:45 (10 months ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Print Exception - GeeksforGeeks |
| Meta Description | Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. |
| Meta Canonical | null |
| Boilerpipe Text | null |
| Markdown | [](https://www.geeksforgeeks.org/)
- Courses
- Tutorials
- Interview Prep
- [Python Tutorial](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/)
- [Data Types](https://www.geeksforgeeks.org/python/python-data-types/)
- [Interview Questions](https://www.geeksforgeeks.org/python/python-interview-questions/)
- [Examples](https://www.geeksforgeeks.org/python/python-programming-examples/)
- [Quizzes](https://www.geeksforgeeks.org/python/python-quizzes/)
- [DSA Python](https://www.geeksforgeeks.org/dsa/python-data-structures-and-algorithms/)
- [Data Science](https://www.geeksforgeeks.org/data-science/data-science-with-python-tutorial/)
- [NumPy](https://www.geeksforgeeks.org/python/numpy-tutorial/)
- [Pandas](https://www.geeksforgeeks.org/pandas/pandas-tutorial/)
- [Practice](https://www.geeksforgeeks.org/dsa/geeksforgeeks-practice-best-online-coding-platform/)
- [Django](https://www.geeksforgeeks.org/python/django-tutorial/)
- [Flask](https://www.geeksforgeeks.org/python/flask-tutorial/)
# Python Print Exception
Last Updated : 23 Jul, 2025
In [Python](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/), ****exceptions**** are errors that occur at runtime and can crash your program if not handled. While [catching exceptions](https://www.geeksforgeeks.org/python/python-exception-handling/) is important, printing them helps us understand what went wrong and where. In this article, we'll focus on different ways to print exceptions.
## Using as keyword
[as keyword](https://www.geeksforgeeks.org/python/python-as-keyword/) lets us store the exception in a variable so you can access its message. We catch a specific error like ****ValueError**** or ****ZeroDivisionError**** and print the message directly. This is the most common and beginner-friendly way to print exceptions. It keeps the output clean and easy to understand.
Python
``
****Output****
```
Enter a number: gfg
ValueError: invalid literal for int() with base 10: 'gfg'
```
****Explanation:**** This code asks the user for a number, converts it to an integer and divides 10 by it. If the input isn't a valid integer, a ****ValueError**** is printed. If the input is 0, a ****ZeroDivisionError**** occurs since division by zero isn’t allowed and that error message is printed.
## Using type()
This method helps us print the type of exception you caught, like \<class 'ValueError'\>. It's useful when we're not sure what kind of error occurred. Along with type(e), we can also print e to get the actual error message. It’s handy for debugging or logging detailed error types.
Python
``
**Output**
```
Type of Exception: <class 'ValueError'>
Message: invalid literal for int() with base 10: 'text'
```
****Explanation:**** This code tries to convert the string "text" into an integer, which isn’t possible and raises a ValueError. The except block catches the error, prints its type using type(e) and displays the error message using e.
## Using traceback.print\_exc()
The [traceback module](https://www.geeksforgeeks.org/python/traceback-in-python/) prints the full traceback of the exception. It shows the exact line of code and function call that caused the error. Very useful when you're debugging large programs or need detailed logs. Make sure to import traceback before using it.
Python
``
****Output****
```
Traceback Info:
Traceback (most recent call last):
File "<ipython-input-36-a96b18b23dc7>", line 4, in <cell line: 0>
res = 10 / 0
~~~^~~
ZeroDivisionError: division by zero
```
****Explanation:**** This code tries to divide 10 by 0, which raises a ZeroDivisionError. The except block catches the error and uses ****traceback.print\_exc()**** to print a detailed traceback, showing where the error occurred in the code.
## Using str() and repr()
****str()**** returns a user-friendly version of the exception message and ****repr()**** gives a more detailed and developer-focused representation of the error. Using both can help differentiate between readable output and debug-level logs. This method is useful when you want more control over how the exception is displayed.
Python
``
**Output**
```
str(): invalid literal for int() with base 10: 'not_a_number'
repr(): ValueError("invalid literal for int() with base 10: 'not_a_number'")
```
****Explanation:**** This code tries to convert the string "not\_a\_number" into an integer, which raises a ValueError. The except block catches the error and prints the message using ****str(e)**** for a readable format and ****repr(e)**** to show the official string representation, which includes the exception type and message.
Comment
Article Tags:
Article Tags:
[Python](https://www.geeksforgeeks.org/category/programming-language/python/)
[Geeks Premier League](https://www.geeksforgeeks.org/category/geeksforgeeks-initiatives/geeks-premier-league/)
[Geeks Premier League 2023](https://www.geeksforgeeks.org/tag/geeks-premier-league-2023/)
### Explore
[](https://www.geeksforgeeks.org/)

Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)

Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
[](https://geeksforgeeksapp.page.link/gfg-app)[](https://geeksforgeeksapp.page.link/gfg-app)
- Company
- [About Us](https://www.geeksforgeeks.org/about/)
- [Legal](https://www.geeksforgeeks.org/legal/)
- [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/)
- [Contact Us](https://www.geeksforgeeks.org/about/contact-us/)
- [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/)
- [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/)
- [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/)
- Explore
- [POTD](https://www.geeksforgeeks.org/problem-of-the-day)
- [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/)
- [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent)
- [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/)
- Tutorials
- [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/)
- [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/)
- [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/)
- [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/)
- [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/)
- [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/)
- [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/)
- [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/)
- Courses
- [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science)
- [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements)
- [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing)
- [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages)
- [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops)
- [GATE](https://www.geeksforgeeks.org/courses/category/gate)
- [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/)
- Videos
- [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/)
- [Python](https://www.geeksforgeeks.org/videos/category/python/)
- [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/)
- [C++](https://www.geeksforgeeks.org/videos/category/c/)
- [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/)
- [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/)
- [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/)
- Preparation Corner
- [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/)
- [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/)
- [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/)
- [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series)
- [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/)
[@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) |
| Readable Markdown | null |
| Shard | 103 (laksa) |
| Root Hash | 12046344915360636903 |
| Unparsed URL | org,geeksforgeeks!www,/python/python-print-exception/ s443 |