What is the SyntaxError builtin practically used for in python?












1















What is the point of the SyntaxError builtin in python? It doesn't seem to have any practical use because SyntaxErrors are found by the python interpreter before the code is run. Example:



try:
&@!5_+ #SyntaxError
except: pass


This fails with a SyntaxError because the SyntaxError is found before it can be handled. Now this works and fails silently, like intended:



try:
raise SyntaxError
except: pass


However, I have never seen a function or class or anything raise a SyntaxError. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError that it can be caught? Or does it just exits in the builtin scope just to be there?










share|improve this question


















  • 1





    It can be used to handle items parsed using ast.literal_eval for example.

    – Ashwini Chaudhary
    Jan 1 at 21:12
















1















What is the point of the SyntaxError builtin in python? It doesn't seem to have any practical use because SyntaxErrors are found by the python interpreter before the code is run. Example:



try:
&@!5_+ #SyntaxError
except: pass


This fails with a SyntaxError because the SyntaxError is found before it can be handled. Now this works and fails silently, like intended:



try:
raise SyntaxError
except: pass


However, I have never seen a function or class or anything raise a SyntaxError. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError that it can be caught? Or does it just exits in the builtin scope just to be there?










share|improve this question


















  • 1





    It can be used to handle items parsed using ast.literal_eval for example.

    – Ashwini Chaudhary
    Jan 1 at 21:12














1












1








1


0






What is the point of the SyntaxError builtin in python? It doesn't seem to have any practical use because SyntaxErrors are found by the python interpreter before the code is run. Example:



try:
&@!5_+ #SyntaxError
except: pass


This fails with a SyntaxError because the SyntaxError is found before it can be handled. Now this works and fails silently, like intended:



try:
raise SyntaxError
except: pass


However, I have never seen a function or class or anything raise a SyntaxError. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError that it can be caught? Or does it just exits in the builtin scope just to be there?










share|improve this question














What is the point of the SyntaxError builtin in python? It doesn't seem to have any practical use because SyntaxErrors are found by the python interpreter before the code is run. Example:



try:
&@!5_+ #SyntaxError
except: pass


This fails with a SyntaxError because the SyntaxError is found before it can be handled. Now this works and fails silently, like intended:



try:
raise SyntaxError
except: pass


However, I have never seen a function or class or anything raise a SyntaxError. So why does this exist so easy to use and catch when the only use seems to be raising it pointlessly? Is there someplace that python raises a SyntaxError that it can be caught? Or does it just exits in the builtin scope just to be there?







python error-handling syntax syntax-error






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 21:09









Ethan K888Ethan K888

563323




563323








  • 1





    It can be used to handle items parsed using ast.literal_eval for example.

    – Ashwini Chaudhary
    Jan 1 at 21:12














  • 1





    It can be used to handle items parsed using ast.literal_eval for example.

    – Ashwini Chaudhary
    Jan 1 at 21:12








1




1





It can be used to handle items parsed using ast.literal_eval for example.

– Ashwini Chaudhary
Jan 1 at 21:12





It can be used to handle items parsed using ast.literal_eval for example.

– Ashwini Chaudhary
Jan 1 at 21:12












3 Answers
3






active

oldest

votes


















3














I believe that documentation is self-explanatory:




Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).







share|improve this answer
























  • So import ..., __import__(...), exec(...) and eval(...)? Is that it?

    – Ethan K888
    Jan 1 at 21:14











  • @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

    – vishes_shell
    Jan 1 at 21:18











  • But can you catch them?

    – Ethan K888
    Jan 1 at 21:19






  • 1





    @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

    – vishes_shell
    Jan 1 at 21:21



















1














There's very rarely a use for using SyntaxError in your program. It's usually only used for the python interpreter when it encounters invalid syntax.



The only case I can think of that you would ever catch a SyntaxError is if you are using exec or eval on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError.






share|improve this answer































    1














    The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did





    find . -type f -name "*.py" | xargs grep "raise SyntaxError"


    We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml library uses them in parsing XPath expressions.



    This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.



    I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval etc. have to raise something, as the docs and the previous answer state.






    share|improve this answer


























    • Nice technical answer! Thanks for doing so much looking around!

      – Ethan K888
      Jan 8 at 21:51











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998960%2fwhat-is-the-syntaxerror-builtin-practically-used-for-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    I believe that documentation is self-explanatory:




    Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).







    share|improve this answer
























    • So import ..., __import__(...), exec(...) and eval(...)? Is that it?

      – Ethan K888
      Jan 1 at 21:14











    • @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

      – vishes_shell
      Jan 1 at 21:18











    • But can you catch them?

      – Ethan K888
      Jan 1 at 21:19






    • 1





      @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

      – vishes_shell
      Jan 1 at 21:21
















    3














    I believe that documentation is self-explanatory:




    Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).







    share|improve this answer
























    • So import ..., __import__(...), exec(...) and eval(...)? Is that it?

      – Ethan K888
      Jan 1 at 21:14











    • @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

      – vishes_shell
      Jan 1 at 21:18











    • But can you catch them?

      – Ethan K888
      Jan 1 at 21:19






    • 1





      @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

      – vishes_shell
      Jan 1 at 21:21














    3












    3








    3







    I believe that documentation is self-explanatory:




    Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).







    share|improve this answer













    I believe that documentation is self-explanatory:




    Raised when the parser encounters a syntax error. This may occur in an import statement, in a call to the built-in functions exec() or eval(), or when reading the initial script or standard input (also interactively).








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 1 at 21:13









    vishes_shellvishes_shell

    10.7k34046




    10.7k34046













    • So import ..., __import__(...), exec(...) and eval(...)? Is that it?

      – Ethan K888
      Jan 1 at 21:14











    • @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

      – vishes_shell
      Jan 1 at 21:18











    • But can you catch them?

      – Ethan K888
      Jan 1 at 21:19






    • 1





      @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

      – vishes_shell
      Jan 1 at 21:21



















    • So import ..., __import__(...), exec(...) and eval(...)? Is that it?

      – Ethan K888
      Jan 1 at 21:14











    • @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

      – vishes_shell
      Jan 1 at 21:18











    • But can you catch them?

      – Ethan K888
      Jan 1 at 21:19






    • 1





      @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

      – vishes_shell
      Jan 1 at 21:21

















    So import ..., __import__(...), exec(...) and eval(...)? Is that it?

    – Ethan K888
    Jan 1 at 21:14





    So import ..., __import__(...), exec(...) and eval(...)? Is that it?

    – Ethan K888
    Jan 1 at 21:14













    @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

    – vishes_shell
    Jan 1 at 21:18





    @EthanK you can also encounter errors with encoding such as SyntaxError: Non-ASCII character ...

    – vishes_shell
    Jan 1 at 21:18













    But can you catch them?

    – Ethan K888
    Jan 1 at 21:19





    But can you catch them?

    – Ethan K888
    Jan 1 at 21:19




    1




    1





    @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

    – vishes_shell
    Jan 1 at 21:21





    @EthanK i believe that you can catch any exception if it's a product of some operation/computation/process.

    – vishes_shell
    Jan 1 at 21:21













    1














    There's very rarely a use for using SyntaxError in your program. It's usually only used for the python interpreter when it encounters invalid syntax.



    The only case I can think of that you would ever catch a SyntaxError is if you are using exec or eval on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError.






    share|improve this answer




























      1














      There's very rarely a use for using SyntaxError in your program. It's usually only used for the python interpreter when it encounters invalid syntax.



      The only case I can think of that you would ever catch a SyntaxError is if you are using exec or eval on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError.






      share|improve this answer


























        1












        1








        1







        There's very rarely a use for using SyntaxError in your program. It's usually only used for the python interpreter when it encounters invalid syntax.



        The only case I can think of that you would ever catch a SyntaxError is if you are using exec or eval on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError.






        share|improve this answer













        There's very rarely a use for using SyntaxError in your program. It's usually only used for the python interpreter when it encounters invalid syntax.



        The only case I can think of that you would ever catch a SyntaxError is if you are using exec or eval on an unknown value. For example, if you were executing user input, then the input may be invalid and you would need to catch the SyntaxError.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 21:57









        Supa Mega Ducky Momo da WaffleSupa Mega Ducky Momo da Waffle

        2,01351229




        2,01351229























            1














            The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did





            find . -type f -name "*.py" | xargs grep "raise SyntaxError"


            We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml library uses them in parsing XPath expressions.



            This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.



            I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval etc. have to raise something, as the docs and the previous answer state.






            share|improve this answer


























            • Nice technical answer! Thanks for doing so much looking around!

              – Ethan K888
              Jan 8 at 21:51
















            1














            The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did





            find . -type f -name "*.py" | xargs grep "raise SyntaxError"


            We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml library uses them in parsing XPath expressions.



            This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.



            I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval etc. have to raise something, as the docs and the previous answer state.






            share|improve this answer


























            • Nice technical answer! Thanks for doing so much looking around!

              – Ethan K888
              Jan 8 at 21:51














            1












            1








            1







            The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did





            find . -type f -name "*.py" | xargs grep "raise SyntaxError"


            We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml library uses them in parsing XPath expressions.



            This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.



            I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval etc. have to raise something, as the docs and the previous answer state.






            share|improve this answer















            The easy way to attack this question is to search for how this exception gets raised in practice. I have the CPython source code already downloaded, so I did





            find . -type f -name "*.py" | xargs grep "raise SyntaxError"


            We see that there is a utility script that does file conversions that raises a syntax error if it doesn't see the magic byte in the correct place. Additionally, the xml library uses them in parsing XPath expressions.



            This suggests that it is meant to be used by user code that deals with parsing other things as well. A Google search for "raise SyntaxError" shows many results of this being used in this way.



            I'm not terribly familiar with the Python internals, but I assume being able to handle an error in paring source code the same way they handle other errors makes things nicer. Plus eval etc. have to raise something, as the docs and the previous answer state.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 8 at 21:54









            Ethan K888

            563323




            563323










            answered Jan 1 at 21:29









            JETMJETM

            2,04341629




            2,04341629













            • Nice technical answer! Thanks for doing so much looking around!

              – Ethan K888
              Jan 8 at 21:51



















            • Nice technical answer! Thanks for doing so much looking around!

              – Ethan K888
              Jan 8 at 21:51

















            Nice technical answer! Thanks for doing so much looking around!

            – Ethan K888
            Jan 8 at 21:51





            Nice technical answer! Thanks for doing so much looking around!

            – Ethan K888
            Jan 8 at 21:51


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998960%2fwhat-is-the-syntaxerror-builtin-practically-used-for-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules