Suppose i define a function like this:
def convert(num):
try:
return int(num)
except ValueError as e:
raise Exception(f"Error Value error exception occurred: {str(e)}")
Now while using this function i also want to catch exceptions unrelated to ValueError
I tried something like this:
try:
convert([1, 2]) # passing a list to cause type error
except Exception as e:
print(f"Error An exception occurred but it's not a value error: {str(e)}")
This worked but when i go to another scenario where ValueError
exception will be raised:
try:
convert("not a number")
except Exception as e:
print(f"Error An exception occurred but it's not a value error: {str(e)}")
Output: Error An exception occurred but it’s not a value error: invalid literal for int() with base 10: ‘not a number’
Although the above code is indeed raising ValueError
and i was assuming this to be caught by my convert
function raise
statement instead.
So am assuming it has something to do with how try
and except
block functions. I appreciate any type of help.