Ways For Using try Without except (Ignoring Exceptions) in Python

You want to test a block code for an exception using try but don’t want to handle using except. This is not an impossible thing to do. In this article, we show you some easy ways for using try without except (ignoring exceptions) in Python. Scroll down to read!

The pass statement in Python

It is not hard to ignore exceptions in Python. The pass statement is built-in that allows you to only use try in your program.

The pass is considered a placeholder for doing nothing. It replaces a block of code the Python program should have to run after catching an exception. 

Here is the syntax you need to use:

try:
   [code]
except [Kind of exception]:
   pass

In other words, the pass statement does not do anything. The Python program will move to the next line or block of code.

Using try without except (ignoring exceptions) with the pass statement

Below are 2 ways you can use try without except (ignoring exceptions) in Python using the built-in pass statement.

Using except [Kind of exception]

You follow the syntax sample that we have mentioned above to use try without except. You write the exception name in the [Kind of except]  to ignore the specific exception you desire.

Please keep in mind that only the exception that you have added to the [Kind of exception] will be ignored. If the exception is different from the one that you have desired, Python will still stop your program and give you the error message.

For example, here is a code that causes ValueError:

varStr = "this is a string"
int(varStr)

The error message is:

    int(varStr)
ValueError: invalid literal for int() with base 10: 'this is a string'

The reason is, you can not transform a string text to an integer. You use pass to ignore the ValueError but do nothing else:

varStr = "this is a string"

try:
    int(varStr)
except ValueError:
    pass

print(varStr)
print("Type: ", type(varStr))

The int(varStr) will fail, but the Python program does not stop running. The varStr is still a string instead of an int, which leads to the output:

this is a string
Type:  <class 'str'>

Using only except

You don’t have to write the exception name in the [Kind of exception]. The Python program still runs with no issues.

As you write no kind of exception next to the except, the Python program will catch all kinds of errors, including ones that you don’t intend to catch. Some exceptions that you may not want to ignore are KeyboardInterrupt, BaseException, and SystemExit

Here is the sample code:

varStr = "this is a string"

try:
    int(varStr)
except:
    pass

print(varStr)
print("Type: ", type(varStr))

As you see, the program does not catch any kind of exceptions, including ValueError. The varStr still remains as a string, and the output will be:

this is a string
Type:  <class 'str'>

Summary

Using try without except (ignoring exceptions) in Python is totally possible. You will rely on the pass statement to ignore exceptions. Please consider carefully between using except [Kind of exception] and only except!

Leave a Reply

Your email address will not be published. Required fields are marked *