1

I am trying to raise an exception only when a specific error message occurs. In my example I need the string stl_load_error to be present in the error message.

try:
    generic s3 copy command

except Exception as exec:
    if 'stl_load_error' in exec:

When I evaluate exec, exec = {InternalError}Load into table some_table failed. Check stl_load_errors system table for details. \n

However, my code is breaking at the if statement. Is there a correct way to do this?

spak
  • 253
  • 1
  • 2
  • 12
  • 2
    try chaning it to `if 'stl_load_error' in str(exec):`... although you should really be checking for the exception type, not its message. – jfaccioni Jun 05 '20 at 14:16
  • If you have control over the code that raises the exception, it would probably be more correct to create a new exception type for just this scenario. – 0x5453 Jun 05 '20 at 14:17

1 Answers1

3

Try converting the exception to a string before doing a string operation on it:

try:
    generic s3 copy command
except InternalError as err:
    if 'stl_load_error' in str(err):

(also, it's better to catch the specific exception class rather than Exception!)

Samwise
  • 68,105
  • 3
  • 30
  • 44