Python Forum
Raise an exception for syntax error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raise an exception for syntax error
#1
try:
    print("Syntax Error)  # Ending quote omitted   
except SyntaxError:
    print("Syntax Error")
SyntaxError: EOL while scanning string literal

Right after the 1st print statement system is generating error message. How can we make it to use our except statement?

Thank U.
Reply
#2
look at your code.
what are you telling it to print?
try it this way:
>>> try:
...     print("--syntax error")
... except SyntaxError:
...     print("ex syntax error")
... 
--syntax error
>>>
Reply
#3
Sir,

I want to parse the 1st print statement which has wrong syntax. Then it should go to the exception print message. But my code does not go beyond 1st print statement. Once it parses the 1st print statement the system is generating the error message.

But I want to print my own error message. How do we do that?

Thank U.
Reply
#4
You will only execute the exception if there is an error.
You don't actually have a syntax error, the print statement is making you think so, because it prints 'Syntax Error'

it's not simple to capture a syntax error, but it can be done.
This may be of interest to you: https://airbrake.io/blog/python-exceptio...yntaxerror
Reply
#5
Hi Larz60+,

Thanks for the blog link. It is lot of info. Here I trying very simple thing.

try:
    print("Syntax Error)  # Ending quote omitted   
except SyntaxError:
    print("Syntax Error")
I want to parse the 1st print statement which has wrong syntax. Then it should go to the exception print message. But my code does not go beyond 1st print statement. Once it parses the 1st print statement the system is generating the error message.

But I want to print my own error message. How do we do that?

Thank U.
Reply
#6
Why do you want to do that?
Reply
#7
Hi ndc85430,

If this works

x = [1]
try:
    x / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
    
then why this does not work?

try:
    print("Syntax Error)  # Ending quote omitted   
except SyntaxError:
    print("Syntax Error")
Reply
#8
Because the interpreter checks that the syntax is valid before executing your code? It won't even allow you to catch the SyntaxError.

Still, you haven't answered the question: why do you want to do that? This sounds like the XY problem.
Reply
#9
Hi ndc85430,

I am a self learner. As part of the exercises at the end of the topic this question was given. That is all. I did not know this "XY problem" theory before. Thanks I learned a new thing.

Thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have 'Failed: DID NOT RAISE <class 'Exception'>' williamKrakos 3 594 Feb-20-2024, 09:46 PM
Last Post: williamKrakos
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,301 Aug-11-2022, 08:43 PM
Last Post: deanhystad
Smile Exception with raise and input artemisellada 3 2,473 Apr-23-2021, 08:19 PM
Last Post: jefsummers
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 1,991 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 1,997 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,964 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,404 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  syntax error: can't assign to operator liam 3 4,030 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,233 Jan-10-2020, 02:30 AM
Last Post: ichabod801
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,422 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020