ℹ️ 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 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.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/ |
| Last Crawled | 2026-04-09 20:23:06 (15 hours ago) |
| First Indexed | 2023-03-16 00:46:03 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Print Exception – How to Try-Except-Print an Error |
| Meta Description | Every programming language has its way of handling exceptions and errors, and Python is no exception. Python comes with a built-in try…except syntax with which you can handle errors and stop them from interrupting the running of your program. In thi... |
| Meta Canonical | null |
| Boilerpipe Text | Every programming language has its way of handling exceptions and errors, and Python is no exception.
Python comes with a built-in
try…except
syntax with which you can handle errors and stop them from interrupting the running of your program.
In this article, you’ll learn how to use that
try…except
syntax to handle exceptions in your code so they don’t stop your program from running.
What We'll Cover
What is an Exception?
The
try…except
Syntax
How to Handle Exceptions with
try…except
How to Print an Exception with
try…except
How to Print the Exception Name
Conclusion
What is an Exception?
In Python, an exception is an error object. It is an error that occurs during the execution of your program and stops it from running – subsequently displaying an error message.
When an exception occurs, Python creates an exception object which contains the type of the error and the line it affects.
Python has many built-in exceptions such as
IndexError
,
NameError
,
TypeError
,
ValueError
,
ZeroDivisionError
KeyError
, and many more.
The
try…except
Syntax
Instead of allowing these exceptions to stop your program from running, you can put the code you want to run in a
try
block and handle the exception in the
except
block.
The basic syntax of
try…except
looks like this:
try
:
# code to run
except
:
# handle error
How to Handle Exceptions with
try…except
You can handle each of the exceptions mentioned in this article with
try…except
. In fact, you can handle all the exceptions in Python with
try…except
.
For example, if you have a large program and you don’t know whether an identifier exists or not, you can execute what you want to do with the identifier in a
try
block and handle a possible error in the
except
block:
try
:
print
(
"Here's variable x:"
,
x
)
except
:
print
(
"An error occured"
)
# An error occured
You can see that the
except
ran because there’s no variable called
x
in the code.
Keep reading. Because I will show you how to make those errors look better by showing you how to handle exceptions gracefully.
But what if you want to print the exact exception that occurred? You can do this by assigning the
Exception
to a variable right in front of the
except
keyword.
When you do this and print the Exception to the terminal, it is the value of the
Exception
that you get.
This is how I printed the
ZeroDivisionError
exception to the terminal:
try
:
res
=
190
/
0
except
Exception
as
error
:
# handle the exception
print
(
"An exception occurred:"
,
error
)
# An exception occurred: division by zero
And this is how I printed the
NameError
exception too:
try
:
print
(
"Here's variable x:"
,
x
)
except
Exception
as
error
:
print
(
"An error occurred:"
,
error
)
# An error occurred: name 'x' is not defined
You can follow this pattern to print any exception to the terminal.
How to Print the Exception Name
What if you want to get the exact exception name and print it to the terminal? That’s possible too. All you need to do is use the
type()
function to get the type of the exception and then use the
__name__
attribute to get the name of the exception.
This is how I modified the
ZeroDivisionError
example to print the exact exception:
try
:
res
=
190
/
0
except
Exception
as
error
:
# handle the exception
print
(
"An exception occurred:"
,
type
(
error
)
.
__name__
)
# An exception occurred: ZeroDivisionError
And this is how I modified the other example to print the
NameError
example:
try
:
print
(
"Here's variable x:"
,
x
)
except
Exception
as
error
:
print
(
"An error occurred:"
,
type
(
error
)
.
__name__
)
# An error occurred: NameError
Normally, when you encounter an Exception such as
NameError
and
ZeroDivisionError
, for example, you get the error in the terminal this way:
You can combine the
type()
function and that error variable to make the exception look better:
try
:
print
(
"Here's variable x:"
,
x
)
except
Exception
as
error
:
print
(
"An error occurred:"
,
type
(
error
)
.
__name__
,
"–"
,
error
)
# An error occurred: NameError – name 'x' is not defined
try
:
res
=
190
/
0
except
Exception
as
error
:
# handle the exception
print
(
"An exception occurred:"
,
type
(
error
)
.
__name__
,
"–"
,
error
)
# An exception occurred: ZeroDivisionError – division by zero
Conclusion
As shown in this article, the
try…except
syntax is a great way to handle errors and prevent your program from stopping during execution.
You can even print that
Exception
to the terminal by assigning the error to a variable, and get the exact type of the
Exception
with the
type()
function.
Happy coding!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers.
Get started |
| Markdown | [](https://www.freecodecamp.org/news/)
Menu Menu
- [Forum](https://forum.freecodecamp.org/)
- [Curriculum](https://www.freecodecamp.org/learn)
[Donate](https://www.freecodecamp.org/donate/)
[Learn to code — free 3,000-hour curriculum](https://www.freecodecamp.org/)
March 15, 2023
/ [\#error handling](https://www.freecodecamp.org/news/tag/error-handling/)
# Python Print Exception – How to Try-Except-Print an Error

[Kolade Chris](https://www.freecodecamp.org/news/author/koladechris/)

Every programming language has its way of handling exceptions and errors, and Python is no exception.
Python comes with a built-in `try…except` syntax with which you can handle errors and stop them from interrupting the running of your program.
In this article, you’ll learn how to use that `try…except` syntax to handle exceptions in your code so they don’t stop your program from running.
## What We'll Cover
- [What is an Exception?](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-what-is-an-exception)
- [The `try…except` Syntax](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-the-tryexcept-syntax)
- [How to Handle Exceptions with `try…except`](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-handle-exceptions-with-tryexcept)
- [How to Print an Exception with `try…except`](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-print-an-exception-with-tryexcept)
- [How to Print the Exception Name](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-print-the-exception-name)
- [Conclusion](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-conclusion)
## What is an Exception?
In Python, an exception is an error object. It is an error that occurs during the execution of your program and stops it from running – subsequently displaying an error message.
When an exception occurs, Python creates an exception object which contains the type of the error and the line it affects.
Python has many built-in exceptions such as `IndexError`, `NameError`, `TypeError`, `ValueError`, `ZeroDivisionError` `KeyError`, and many more.
## The `try…except` Syntax
Instead of allowing these exceptions to stop your program from running, you can put the code you want to run in a `try` block and handle the exception in the `except` block.
The basic syntax of `try…except` looks like this:
```
try:
# code to run
except:
# handle error
```
## How to Handle Exceptions with `try…except`
You can handle each of the exceptions mentioned in this article with `try…except`. In fact, you can handle all the exceptions in Python with `try…except`.
For example, if you have a large program and you don’t know whether an identifier exists or not, you can execute what you want to do with the identifier in a `try` block and handle a possible error in the `except` block:
```
try:
print("Here's variable x:", x)
except:
print("An error occured") # An error occured
```
You can see that the `except` ran because there’s no variable called `x` in the code.
Keep reading. Because I will show you how to make those errors look better by showing you how to handle exceptions gracefully.
## How to Print an Exception with `try…except`
But what if you want to print the exact exception that occurred? You can do this by assigning the `Exception` to a variable right in front of the `except` keyword.
When you do this and print the Exception to the terminal, it is the value of the `Exception` that you get.
This is how I printed the `ZeroDivisionError` exception to the terminal:
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", error) # An exception occurred: division by zero
```
And this is how I printed the `NameError` exception too:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", error) # An error occurred: name 'x' is not defined
```
You can follow this pattern to print any exception to the terminal.
## How to Print the Exception Name
What if you want to get the exact exception name and print it to the terminal? That’s possible too. All you need to do is use the `type()` function to get the type of the exception and then use the `__name__` attribute to get the name of the exception.
This is how I modified the `ZeroDivisionError` example to print the exact exception:
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", type(error).__name__) # An exception occurred: ZeroDivisionError
```
And this is how I modified the other example to print the `NameError` example:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", type(error).__name__) # An error occurred: NameError
```
Normally, when you encounter an Exception such as `NameError` and `ZeroDivisionError`, for example, you get the error in the terminal this way:


You can combine the `type()` function and that error variable to make the exception look better:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", type(error).__name__, "–", error) # An error occurred: NameError – name 'x' is not defined
```
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", type(error).__name__, "–", error) # An exception occurred: ZeroDivisionError – division by zero
```
## Conclusion
As shown in this article, the `try…except` syntax is a great way to handle errors and prevent your program from stopping during execution.
You can even print that `Exception` to the terminal by assigning the error to a variable, and get the exact type of the `Exception` with the `type()` function.
Happy coding\!
***

[Kolade Chris](https://www.freecodecamp.org/news/author/koladechris/)
I'm a software developer and tech writer focusing on frontend technologies
***
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn)
ADVERTISEMENT
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
You can [make a tax-deductible donation here](https://www.freecodecamp.org/donate/).
## Trending Books and Handbooks
- [REST APIs](https://www.freecodecamp.org/news/build-consume-and-document-a-rest-api/)
- [Clean Code](https://www.freecodecamp.org/news/how-to-write-clean-code/)
- [TypeScript](https://www.freecodecamp.org/news/learn-typescript-with-react-handbook/)
- [JavaScript](https://www.freecodecamp.org/news/learn-javascript-for-beginners/)
- [AI Chatbots](https://www.freecodecamp.org/news/how-to-build-an-ai-chatbot-with-redis-python-and-gpt/)
- [Command Line](https://www.freecodecamp.org/news/command-line-for-beginners/)
- [GraphQL APIs](https://www.freecodecamp.org/news/building-consuming-and-documenting-a-graphql-api/)
- [CSS Transforms](https://www.freecodecamp.org/news/complete-guide-to-css-transform-functions-and-properties/)
- [Access Control](https://www.freecodecamp.org/news/how-to-build-scalable-access-control-for-your-web-app/)
- [REST API Design](https://www.freecodecamp.org/news/rest-api-design-best-practices-build-a-rest-api/)
- [PHP](https://www.freecodecamp.org/news/the-php-handbook/)
- [Java](https://www.freecodecamp.org/news/the-java-handbook/)
- [Linux](https://www.freecodecamp.org/news/learn-linux-for-beginners-book-basic-to-advanced/)
- [React](https://www.freecodecamp.org/news/react-for-beginners-handbook/)
- [CI/CD](https://www.freecodecamp.org/news/learn-continuous-integration-delivery-and-deployment/)
- [Docker](https://www.freecodecamp.org/news/the-docker-handbook/)
- [Golang](https://www.freecodecamp.org/news/learn-golang-handbook/)
- [Python](https://www.freecodecamp.org/news/the-python-handbook/)
- [Node.js](https://www.freecodecamp.org/news/get-started-with-nodejs/)
- [Todo APIs](https://www.freecodecamp.org/news/build-crud-operations-with-dotnet-core-handbook/)
- [JavaScript Classes](https://www.freecodecamp.org/news/how-to-use-classes-in-javascript-handbook/)
- [Front-End Libraries](https://www.freecodecamp.org/news/front-end-javascript-development-react-angular-vue-compared/)
- [Express and Node.js](https://www.freecodecamp.org/news/the-express-handbook/)
- [Python Code Examples](https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/)
- [Clustering in Python](https://www.freecodecamp.org/news/clustering-in-python-a-machine-learning-handbook/)
- [Software Architecture](https://www.freecodecamp.org/news/an-introduction-to-software-architecture-patterns/)
- [Programming Fundamentals](https://www.freecodecamp.org/news/what-is-programming-tutorial-for-beginners/)
- [Coding Career Preparation](https://www.freecodecamp.org/news/learn-to-code-book/)
- [Full-Stack Developer Guide](https://www.freecodecamp.org/news/become-a-full-stack-developer-and-get-a-job/)
- [Python for JavaScript Devs](https://www.freecodecamp.org/news/learn-python-for-javascript-developers-handbook/)
## Mobile App
- [](https://apps.apple.com/us/app/freecodecamp/id6446908151?itsct=apps_box_link&itscg=30200)
- [](https://play.google.com/store/apps/details?id=org.freecodecamp)
## Our Charity
[Publication powered by Hashnode](https://hashnode.com/) [About](https://www.freecodecamp.org/news/about/) [Alumni Network](https://www.linkedin.com/school/free-code-camp/people/) [Open Source](https://github.com/freeCodeCamp/) [Shop](https://www.freecodecamp.org/news/shop/) [Support](https://www.freecodecamp.org/news/support/) [Sponsors](https://www.freecodecamp.org/news/sponsors/) [Academic Honesty](https://www.freecodecamp.org/news/academic-honesty-policy/) [Code of Conduct](https://www.freecodecamp.org/news/code-of-conduct/) [Privacy Policy](https://www.freecodecamp.org/news/privacy-policy/) [Terms of Service](https://www.freecodecamp.org/news/terms-of-service/) [Copyright Policy](https://www.freecodecamp.org/news/copyright-policy/) |
| Readable Markdown | 
Every programming language has its way of handling exceptions and errors, and Python is no exception.
Python comes with a built-in `try…except` syntax with which you can handle errors and stop them from interrupting the running of your program.
In this article, you’ll learn how to use that `try…except` syntax to handle exceptions in your code so they don’t stop your program from running.
## What We'll Cover
- [What is an Exception?](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-what-is-an-exception)
- [The `try…except` Syntax](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-the-tryexcept-syntax)
- [How to Handle Exceptions with `try…except`](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-handle-exceptions-with-tryexcept)
- [How to Print an Exception with `try…except`](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-print-an-exception-with-tryexcept)
- [How to Print the Exception Name](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-how-to-print-the-exception-name)
- [Conclusion](https://www.freecodecamp.org/news/python-print-exception-how-to-try-except-print-an-error/#heading-conclusion)
## What is an Exception?
In Python, an exception is an error object. It is an error that occurs during the execution of your program and stops it from running – subsequently displaying an error message.
When an exception occurs, Python creates an exception object which contains the type of the error and the line it affects.
Python has many built-in exceptions such as `IndexError`, `NameError`, `TypeError`, `ValueError`, `ZeroDivisionError` `KeyError`, and many more.
## The `try…except` Syntax
Instead of allowing these exceptions to stop your program from running, you can put the code you want to run in a `try` block and handle the exception in the `except` block.
The basic syntax of `try…except` looks like this:
```
try:
# code to run
except:
# handle error
```
## How to Handle Exceptions with `try…except`
You can handle each of the exceptions mentioned in this article with `try…except`. In fact, you can handle all the exceptions in Python with `try…except`.
For example, if you have a large program and you don’t know whether an identifier exists or not, you can execute what you want to do with the identifier in a `try` block and handle a possible error in the `except` block:
```
try:
print("Here's variable x:", x)
except:
print("An error occured") # An error occured
```
You can see that the `except` ran because there’s no variable called `x` in the code.
Keep reading. Because I will show you how to make those errors look better by showing you how to handle exceptions gracefully.
But what if you want to print the exact exception that occurred? You can do this by assigning the `Exception` to a variable right in front of the `except` keyword.
When you do this and print the Exception to the terminal, it is the value of the `Exception` that you get.
This is how I printed the `ZeroDivisionError` exception to the terminal:
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", error) # An exception occurred: division by zero
```
And this is how I printed the `NameError` exception too:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", error) # An error occurred: name 'x' is not defined
```
You can follow this pattern to print any exception to the terminal.
## How to Print the Exception Name
What if you want to get the exact exception name and print it to the terminal? That’s possible too. All you need to do is use the `type()` function to get the type of the exception and then use the `__name__` attribute to get the name of the exception.
This is how I modified the `ZeroDivisionError` example to print the exact exception:
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", type(error).__name__) # An exception occurred: ZeroDivisionError
```
And this is how I modified the other example to print the `NameError` example:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", type(error).__name__) # An error occurred: NameError
```
Normally, when you encounter an Exception such as `NameError` and `ZeroDivisionError`, for example, you get the error in the terminal this way:


You can combine the `type()` function and that error variable to make the exception look better:
```
try:
print("Here's variable x:", x)
except Exception as error:
print("An error occurred:", type(error).__name__, "–", error) # An error occurred: NameError – name 'x' is not defined
```
```
try:
res = 190 / 0
except Exception as error:
# handle the exception
print("An exception occurred:", type(error).__name__, "–", error) # An exception occurred: ZeroDivisionError – division by zero
```
## Conclusion
As shown in this article, the `try…except` syntax is a great way to handle errors and prevent your program from stopping during execution.
You can even print that `Exception` to the terminal by assigning the error to a variable, and get the exact type of the `Exception` with the `type()` function.
Happy coding\!
***
***
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn) |
| Shard | 32 (laksa) |
| Root Hash | 13723046482134587832 |
| Unparsed URL | org,freecodecamp!www,/news/python-print-exception-how-to-try-except-print-an-error/ s443 |