0
print ("enter 2 numbers to divide ")
x = input ()
y = input ()

try :
    div = int (x)/ int (y)

    print ( x + ' / ' +y +' = ' +str(div))


    mul = int (x)* int (y)

    print ( x + ' * ' +y +' = ' +(str(mul)))


except ZeroDivisionError:
    print ("2nd number can not be 0")

#except SyntaxError:
    print ("try again inputs are wrong")

except ValueError:
    print ("try again inputs are wrong Value error")

except:
    print ("number can not be blank")

This is an example code. I want to understand , what ever error it is , it does not go to default if ValueError is specified. To test it I added the SyntaxError exclussively , even then when a sysntax error occures (like . / ` ) where divition can not happen and in compiler it is a syntax error still in goes under value error block.

One more example,

>>> s/a
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    s/a
NameError: name 's' is not defined

This inputs also take the output to "Value Error " instead of default.

Even ,

>>> `/`
SyntaxError: invalid syntax

In this case even though syntax error is defined , it takes the output to "valueerror" (if defined ) or default rather than going inside "SyntaxError" error block of the code.

Can any one tell me why ? I checked in hierarchy and a few example website, but nothing I understood what is causing this.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Can you please post the full stacktrace? And also maybe format your question a little bit nicer/more consistent? thanks! – WiseDev Jul 09 '19 at 10:33
  • 1
    You are running on shell where you are not catching any `execptions`. You need to write try and except in shell, while executing to catch errors. – shaik moeed Jul 09 '19 at 10:39
  • Check [this](https://stackoverflow.com/a/25049535/8353711) for more understanding. – shaik moeed Jul 09 '19 at 10:43

1 Answers1

0

A syntax error occurs at compile time, because Python can't parse the code. You can't catch it at runtime because the code is never even executed.

That said, nothing in your first code would cause a syntax error anyway; I'm not sure why you think it would. Entering \ in the input means you are effectively doing int("\"), which as you say is a ValueError.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895