-1

As you can see in the code, i have a function that needs a tuple (*args) and now i am handling errors about that. It's mission is not necessary now but i have a problem with syntax error. When i write 1a6 in the parenthesis, i expect the customized error message. When i run it, normally i see an error message but it's not mine.

def member_finder_outer(*args):
 return args

try:
   print(member_finder_outer(1, 12, 13, 14, 15, 1a6, 17, 18, 19, 20, 21)) 
except:
   print("I want here.")

File "d:/Burak/Yazılım/Python/Dersler/17-/Binary Search/binary_search.py", line 55
   print(member_finder_outer(1, 12, 13, 14, 15, 1a6, 17, 18, 19, 20, 21)
                                                 ^
SyntaxError: invalid syntax

I have tried these:

except SyntaxError:
    print("I want here.")

except Exception:
    print("I want here.")

except SyntaxError as e:
    print("I want here.",e)

But it still writes it's own error message.

  • It's not reaching a point where the code is actually run because of syntax errors. `try` and `except` handle *runtime* errors, not parsing errors. Change `1a6` to `16`, and add the missing closing paren to that line. Once you eliminate the syntax errors, your `try` will catch any any runtime errors that arise. – Tom Karzes Aug 13 '20 at 11:22
  • https://stackoverflow.com/questions/25049498/failed-to-catch-syntax-error-python#:~:text=You%20can%20only%20catch%20SyntaxError,%2C%20exec%20%2C%20or%20import%20operation.&text=This%20is%20because%2C%20normally%2C%20the,the%20try%20statement%20is%20executed. – Petronella Aug 13 '20 at 11:23

2 Answers2

1

Unfortunately most SyntaxErrors are thrown when the file is being parsed ref. So it's being thrown before the try and except are registered. If the SyntaxError is thrown inside an eval() though, then you can catch it

def member_finder_outer(*args):
 return args

try:
   eval("print(member_finder_outer(1, 12, 13, 14, 15, 1a6, 17, 18, 19, 20, 21)")
except:
   print("I want here.")

Disclaimer: eval is a pretty dangerous tool, so do use with caution. https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html

Shadab
  • 1,297
  • 6
  • 9
  • Ah sorry, fixed that on instinct. I've edited the answer to include the missing paren. – Shadab Aug 13 '20 at 11:34
  • You're moving in the wrong direction. OP should definitely not be using `eval` for this, although it may allow the syntax error to be caught at runtime (but no one should want to do that). – Tom Karzes Aug 13 '20 at 11:36
  • Yep, definitely shouldn't use this for any practical application. I assumed OP was just exploring how `SyntaxError`s work, and left an answer assuming that. – Shadab Aug 13 '20 at 11:40
  • 1
    It's possible. I think the most helpful thing for OP to learn is the difference between a syntax error and a runtime error, as opposed to turning *all* errors into runtime errors. – Tom Karzes Aug 13 '20 at 11:41
  • Agreed, added a disclaimer to the original answer. – Shadab Aug 13 '20 at 11:42
  • Thank you Tom Karzes and Shadab. I have just understood the problem. As you have said, `try` does not look to syntax. It looks to **running** code. – Burak Emmiler Aug 14 '20 at 20:26
0

The 1a6 is an invalid item. It has to be a number, may 16, or possibly a string ‘1a6’. You are getting the error before your program even executes.

Jeff Sinason
  • 83
  • 1
  • 8