Exceptions are unanticipated events that occur in a program. Python, like many other programming languages, uses a block of statements to deal with common exceptions. To manage and process exceptions in Python, use the try-except block.
The idea is that we’ll use a try block to wrap every code that can throw an exception. Any exception that occurred in the try block is caught in an except block.
Exception Syntax in Python – try-excpet

Unless correctly handled, an exception will usually kill the program. When an exception occurs in Python try-except, the program searches the except block for a matching exception handler. If a match is found, the control is sent to the except block. If no matches are found, the software throws an unhandled exception and terminates.
Python try-except example
Take a look at the sample below. Let’s pretend we’re trying to open a file for reading and the file isn’t in the project folder. You’ll notice that an exception was thrown, and the except block handled it correctly.
Also, because we handled the exception correctly, the application will not be terminated. It will also execute the last print statement in this situation.

When to use try-except?
- When you have a code block to execute that may sometimes run well and sometimes not, depending on conditions you can’t predict when you’re writing the code, you should use try/except.
- When you run code that gets data from a website, for example, you can run it even if you don’t have a network connection or if the external website is momentarily unavailable.
You’d prefer to manage the exception and have the rest of your code execute if your application can still accomplish something valuable in those scenarios.
Let’s say you’ve fetched some nested data from a website and stored it in a dictionary d. When attempting to extract specific elements, some may be missing: for example, d may not include a specific key. You may build an if..else check to account for the possibility of a certain key not being present.

However, if you’re extracting a lot of distinct data, checking for each one can become laborious. All of the data extraction may be wrapped in a try/except statement.

Attempting to capture all exceptions in this manner is considered bad practice. Instead, Python provides a method for you to select which exceptions you’ll capture (for example, just catching KeyError exceptions, which occur when a key is missing from a dictionary).

What happens in TRY-except?
To fully understand what happens in TRY-except, we first of all need to understand what an exception means.
Exceptions are a tool that programmers use to indicate problems or flaws that are fatal to the program; for example, when an exception occurs, the program cannot or should not continue. Exceptions can occur as a result of code flaws, human errors, or unanticipated circumstances such as a lack of internet access.
If the exception was generated by a programming fault, the developer can simply alter the code to fix it. You don’t want to crash the program if the exception was caused by incorrect user input or a terrible environmental state (for example, the wireless is down). Instead, you should provide the user feedback and let them rectify the problem or try again. As a result, you can catch exceptions where they occur in your code by using the following syntax:

Basically, we’re stating that you should try running the code in the first block and see if it works. If it raises an error (exception), save it in a variable called e (using the as e syntax), and then deal with it in the unless block.
Try-except print error
Python has a lot of built-in support for exceptions and handling them. An exception event interrupts a running program and, if uncaught, ends it immediately. IndexError, ValueError, and TypeError are the most common errors.
Example 1: Catch and Print IndexError
Python will issue an IndexError whenever you attempt accessing the list element that has index 100 but your lists only have three elements.

Fortunately, you caught the exception and wrapped the code in a try/catch block. The program is not terminated. As a result, it executes the final print() statement after the exception has not only been caught but handled as well. Below is the result of the previous code snippet.

Example 2: Catch and Print ValueError
If you try to use incorrect values in some functions, you’ll get a ValueError. Here’s an example of when you get a ValueError when you try to square the root of a negative number:

Not only the error message but also the string ‘Am I executed?’ are printed in the output.

Example 3: Catch and Print TypeError
If you use indexing using the square bracket notation on an object that isn’t indexable, Python throws the TypeError object isn’t subscriptable. This is the case if the object’s __getitem__() method isn’t defined. The following is how you may capture the error and print it to your shell:

Not only the error message but also the string ‘Am I executed?’ are printed in the output.

Conclusion
Keep in mind that exceptions are your friend. They are communication technologies that protect you and your users from serious harm. We believe that by learning how to read the syntax of exceptions, you will find them to be friendlier and less frustrating; they are a map to help you find answers, not roadblocks!