I'm writing a script to read data from Google Sheets and process them using other libraries. The full dataset contains various input values that could produce the same 2 unique error scenarios. If I do a general Try-Except block, I can handle any error scenario, but I can only print the same error message to the console regardless of the error.
try:
# Run some code
except:
print("General Error Message")
pass
I would also like to output a message to the console if an IndexError message pops up. How would I use a single Try-Except block to handle two different error messages? I'm trying to avoid using a second Except block.
The get()
function from the gspread
library inputs a column range as a 2D array. As we iterate through the array (see below), we reach the []
in the middle, which we get the error message: IndexError: list index out of range
.
Input Array (allVal
): [['apples'], ['banana'], [], ['grapes'], ['oranges]]
This is how gspread
reads a column in Google Sheets if there's a blank cell in the range. The []
in the middle represents the blank cell in the range. This is intentional and there could be other scenarios where there are multiple blank rows in the sheet to separate different data.
Everything can be processed just fine until we reach allVal[2][0]
which returns the error message IndexError: list index out of range
whether I try to store it into a variable or print it directly. Since the code sees []
as something out of range, I'm not able to put this into any conditional statement. I want to do something like this.
for i in range(len(allVal)):
try:
val = allVal[i][0]
parsedJSON = someLibrary(val)
except:
checkStr = isinstance(val, str)
if checkStr is False:
print("Skipping over blank cell")
else:
print("Invalid input value: ", val)
So, if the variable isn't a string, we would say that it's a blank cell and print "Skipping over blank cell". Else, we would print that it's invalid and provide the affected string. But since we're not able to do checks on []
, then we might not have the answer.
Am I approaching this the correct way? How would I handle two different error messages in a single Try-Except block?
CodePudding user response:
You can check for multiple errors using the following method:
try:
...
except (NameError, TypeError) as error:
print(error)
So if it is a NameError
it will print the error regarding that error, and the same goes for TypeError
.
Unfortunately other than this, using multiple except
s is your best option.
CodePudding user response:
From what I understand from your issue, what you are looking for is a way to catch different exceptions using a single try
and except
block without too many conditions and checks everywhere.
You can have different except
blocks of code for each exception type using this syntax, that should help you have a different error message for each exception.
try:
val = allVal[i][0]
parsedJSON = someLibrary(val)
except IndexError:
print("skip empty cell")
except NameError:
print("something else")
you might be able to commit the exception type on the last occurrence of except
, but I'm not completely sure.