Consider this code:
try:
# potential erroneous statement
# other statements dependable on the above statement
except:
pass
Meaning, I have a statement that may cause error and next statements should execute only if no error occurs. However, I don’t need some statements that will execute if an error occurs. In this case, I have to put a simple pass statement in the exception block.
A lot of times this case is encountered and I think the two unnecessary except block statements can be eliminated by adding the following try function to the core module.
def try(self, statement) -> bool:
try:
eval(statement)
return True
except:
return False
This is how it works:
tup = (1, 2, 3)
statement = str(tup.append(4)) # potential erroneous statement
try(statement):
# statements that execute if no error occurs
# for example
print (" added successfully")
If there already exists an alternative to this, please let me know. Else, I would like to consider this to be added as an enhanced try() function to the core module of python.