. Advertisement .
. Advertisement .
A try-except block is the simplest solution for handling exceptions in Python. But what if you don’t want to include the except block and do anything about the errors? Let’s learn about how to use try without except In Python and possible alternatives.
try Without except In Python
Exceptions in Python are errors that the interpreter detects during execution. They aren’t necessarily fatal and stop your program. But failure to do so may lead to unexpected behaviors.
There are plenty of exception types in Python, such as TypeError, NameError, and ZeroDivisionError. The name of these exceptions is printed alongside the error message when they occur. Remember that this applies to built-in exceptions but isn’t necessarily true for user-defined ones.
You can use try-except statements to handle specific exceptions like this:
try: # Your Code except: # Code to execute if error occurs else: # Code to execute if no error occurs
This is an example of using
try-except to handle selected exceptions:
Please enter your choice (1-3): 1 The site's name is ITTutoria. Its ranking is 1 and the top language is Python.
In the above example, the
try-except statements are placed inside a white loop. They tell the program to handle only the ValueError exception. When it occurs, the while loop will print the message and bring the code back to the try statement’s code block. It only escapes the while loop when there is no ValueError to handle.
However, there are times when you may just want to silence the error message and don’t respond to it when a specific exception occurs. This can be done if you leave the code block under the except statement empty, except for a pass statement.
while True: try: x = int(input("Please enter your choice (1-3): ")) break except ValueError: pass
Please enter your choice (1-3): a Please enter your choice (1-3): 1 The site's name is ITTutoria. Its ranking is 1 and the top language is Python.
In Python, pass acts like a null statement. Python doesn’t ignore it like comments but doesn’t execute anything either. Developers generally use it as a placeholder. In the example above, it has another role. When the program encounters a ValueError (like when we enter a non-numeric string when it expects a number), the pass statement prevents Python from printing an error message before going back to the while loop.
suppress() function from the contextlib module can suppress certain exceptions when they occur and resume exceptions. You can rewrite the program with this function and replace the whole while loop above like this:
with suppress(ValueError): x = int(input("Please enter your choice (1-3): "))
If you want to use try without except In Python, you can use either the pass statement or the
suppress() function. Though these options work in different ways, they don’t print an error message and stop your program. Remember that they only work with errors detected at runtime. If you want to solve issues like “Unexpected Indent”, consult this guide.