Disable exception chaining in python 3











up vote
10
down vote

favorite
1












There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.



Here is sample code:



try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))


what I see:



Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero


what I want to see:



Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero


I tried to use sys.exc_clear(), but this mehtod is removed from python 3 too.
I can use workaround that works



exc = None
try:
print(10/0)
except ZeroDivisionError as e:
exc = e
if exc:
raise AssertionError(str(exc))


but I believe that there is better solution.










share|improve this question




























    up vote
    10
    down vote

    favorite
    1












    There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.



    Here is sample code:



    try:
    print(10/0)
    except ZeroDivisionError as e:
    sys.exc_info()
    raise AssertionError(str(e))


    what I see:



    Traceback (most recent call last):
    File "draft.py", line 19, in main
    print(10/0)
    ZeroDivisionError: division by zero

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "draft.py", line 26, in <module>
    main()
    File "draft.py", line 22, in main
    raise AssertionError(str(e))
    AssertionError: division by zero


    what I want to see:



    Traceback (most recent call last):
    File "draft.py", line 26, in <module>
    main()
    File "draft.py", line 22, in main
    raise AssertionError(str(e))
    AssertionError: division by zero


    I tried to use sys.exc_clear(), but this mehtod is removed from python 3 too.
    I can use workaround that works



    exc = None
    try:
    print(10/0)
    except ZeroDivisionError as e:
    exc = e
    if exc:
    raise AssertionError(str(exc))


    but I believe that there is better solution.










    share|improve this question


























      up vote
      10
      down vote

      favorite
      1









      up vote
      10
      down vote

      favorite
      1






      1





      There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.



      Here is sample code:



      try:
      print(10/0)
      except ZeroDivisionError as e:
      sys.exc_info()
      raise AssertionError(str(e))


      what I see:



      Traceback (most recent call last):
      File "draft.py", line 19, in main
      print(10/0)
      ZeroDivisionError: division by zero

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
      File "draft.py", line 26, in <module>
      main()
      File "draft.py", line 22, in main
      raise AssertionError(str(e))
      AssertionError: division by zero


      what I want to see:



      Traceback (most recent call last):
      File "draft.py", line 26, in <module>
      main()
      File "draft.py", line 22, in main
      raise AssertionError(str(e))
      AssertionError: division by zero


      I tried to use sys.exc_clear(), but this mehtod is removed from python 3 too.
      I can use workaround that works



      exc = None
      try:
      print(10/0)
      except ZeroDivisionError as e:
      exc = e
      if exc:
      raise AssertionError(str(exc))


      but I believe that there is better solution.










      share|improve this question















      There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.



      Here is sample code:



      try:
      print(10/0)
      except ZeroDivisionError as e:
      sys.exc_info()
      raise AssertionError(str(e))


      what I see:



      Traceback (most recent call last):
      File "draft.py", line 19, in main
      print(10/0)
      ZeroDivisionError: division by zero

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
      File "draft.py", line 26, in <module>
      main()
      File "draft.py", line 22, in main
      raise AssertionError(str(e))
      AssertionError: division by zero


      what I want to see:



      Traceback (most recent call last):
      File "draft.py", line 26, in <module>
      main()
      File "draft.py", line 22, in main
      raise AssertionError(str(e))
      AssertionError: division by zero


      I tried to use sys.exc_clear(), but this mehtod is removed from python 3 too.
      I can use workaround that works



      exc = None
      try:
      print(10/0)
      except ZeroDivisionError as e:
      exc = e
      if exc:
      raise AssertionError(str(exc))


      but I believe that there is better solution.







      python python-3.x exception-handling






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '15 at 0:06









      Trey Hunner

      6,24333153




      6,24333153










      asked Nov 19 '15 at 16:53









      Rugnar

      6741716




      6741716
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          17
          down vote



          accepted










          Simple Answer



          try:
          print(10/0)
          except ZeroDivisionError as e:
          raise AssertionError(str(e)) from None


          However, you probably actually want:



          try:
          print(10/0)
          except ZeroDivisionError as e:
          raise AssertionError(str(e)) from e


          Explanation



          __cause__



          Implicit exception chaining happens through __context__ when there isn't an explicit cause exception set.



          Explicit exception chaining works through __cause__ so if you set __cause__ to the exception itself, it should stop the chaining. If __cause__ is set, Python will suppress the implicit message.



          try:
          print(10/0)
          except ZeroDivisionError as e:
          exc = AssertionError(str(e))
          exc.__cause__ = exc
          raise exc


          raise from



          We can use "raise from" to do the same thing:



          try:
          print(10/0)
          except ZeroDivisionError as e:
          exc = AssertionError(str(e))
          raise exc from exc


          None __cause__



          Setting __cause__ to None actually does the same thing:



          try:
          print(10/0)
          except ZeroDivisionError as e:
          exc = AssertionError(str(e))
          exc.__cause__ = None
          raise exc


          raise from None



          So that brings us to the most elegant way to do this which is to raise from None:



          try:
          print(10/0)
          except ZeroDivisionError as e:
          raise AssertionError(str(e)) from None


          But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:



          try:
          print(10/0)
          except ZeroDivisionError as e:
          raise AssertionError(str(e)) from e


          This will give us a slightly different message that states that the first exception was the direct cause of the second:



          Traceback (most recent call last):
          File "<stdin>", line 2, in <module>
          ZeroDivisionError: division by zero

          The above exception was the direct cause of the following exception:

          Traceback (most recent call last):
          File "<stdin>", line 4, in <module>
          AssertionError: division by zero





          share|improve this answer





















            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',
            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%2f33809864%2fdisable-exception-chaining-in-python-3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            17
            down vote



            accepted










            Simple Answer



            try:
            print(10/0)
            except ZeroDivisionError as e:
            raise AssertionError(str(e)) from None


            However, you probably actually want:



            try:
            print(10/0)
            except ZeroDivisionError as e:
            raise AssertionError(str(e)) from e


            Explanation



            __cause__



            Implicit exception chaining happens through __context__ when there isn't an explicit cause exception set.



            Explicit exception chaining works through __cause__ so if you set __cause__ to the exception itself, it should stop the chaining. If __cause__ is set, Python will suppress the implicit message.



            try:
            print(10/0)
            except ZeroDivisionError as e:
            exc = AssertionError(str(e))
            exc.__cause__ = exc
            raise exc


            raise from



            We can use "raise from" to do the same thing:



            try:
            print(10/0)
            except ZeroDivisionError as e:
            exc = AssertionError(str(e))
            raise exc from exc


            None __cause__



            Setting __cause__ to None actually does the same thing:



            try:
            print(10/0)
            except ZeroDivisionError as e:
            exc = AssertionError(str(e))
            exc.__cause__ = None
            raise exc


            raise from None



            So that brings us to the most elegant way to do this which is to raise from None:



            try:
            print(10/0)
            except ZeroDivisionError as e:
            raise AssertionError(str(e)) from None


            But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:



            try:
            print(10/0)
            except ZeroDivisionError as e:
            raise AssertionError(str(e)) from e


            This will give us a slightly different message that states that the first exception was the direct cause of the second:



            Traceback (most recent call last):
            File "<stdin>", line 2, in <module>
            ZeroDivisionError: division by zero

            The above exception was the direct cause of the following exception:

            Traceback (most recent call last):
            File "<stdin>", line 4, in <module>
            AssertionError: division by zero





            share|improve this answer

























              up vote
              17
              down vote



              accepted










              Simple Answer



              try:
              print(10/0)
              except ZeroDivisionError as e:
              raise AssertionError(str(e)) from None


              However, you probably actually want:



              try:
              print(10/0)
              except ZeroDivisionError as e:
              raise AssertionError(str(e)) from e


              Explanation



              __cause__



              Implicit exception chaining happens through __context__ when there isn't an explicit cause exception set.



              Explicit exception chaining works through __cause__ so if you set __cause__ to the exception itself, it should stop the chaining. If __cause__ is set, Python will suppress the implicit message.



              try:
              print(10/0)
              except ZeroDivisionError as e:
              exc = AssertionError(str(e))
              exc.__cause__ = exc
              raise exc


              raise from



              We can use "raise from" to do the same thing:



              try:
              print(10/0)
              except ZeroDivisionError as e:
              exc = AssertionError(str(e))
              raise exc from exc


              None __cause__



              Setting __cause__ to None actually does the same thing:



              try:
              print(10/0)
              except ZeroDivisionError as e:
              exc = AssertionError(str(e))
              exc.__cause__ = None
              raise exc


              raise from None



              So that brings us to the most elegant way to do this which is to raise from None:



              try:
              print(10/0)
              except ZeroDivisionError as e:
              raise AssertionError(str(e)) from None


              But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:



              try:
              print(10/0)
              except ZeroDivisionError as e:
              raise AssertionError(str(e)) from e


              This will give us a slightly different message that states that the first exception was the direct cause of the second:



              Traceback (most recent call last):
              File "<stdin>", line 2, in <module>
              ZeroDivisionError: division by zero

              The above exception was the direct cause of the following exception:

              Traceback (most recent call last):
              File "<stdin>", line 4, in <module>
              AssertionError: division by zero





              share|improve this answer























                up vote
                17
                down vote



                accepted







                up vote
                17
                down vote



                accepted






                Simple Answer



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from None


                However, you probably actually want:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from e


                Explanation



                __cause__



                Implicit exception chaining happens through __context__ when there isn't an explicit cause exception set.



                Explicit exception chaining works through __cause__ so if you set __cause__ to the exception itself, it should stop the chaining. If __cause__ is set, Python will suppress the implicit message.



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                exc.__cause__ = exc
                raise exc


                raise from



                We can use "raise from" to do the same thing:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                raise exc from exc


                None __cause__



                Setting __cause__ to None actually does the same thing:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                exc.__cause__ = None
                raise exc


                raise from None



                So that brings us to the most elegant way to do this which is to raise from None:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from None


                But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from e


                This will give us a slightly different message that states that the first exception was the direct cause of the second:



                Traceback (most recent call last):
                File "<stdin>", line 2, in <module>
                ZeroDivisionError: division by zero

                The above exception was the direct cause of the following exception:

                Traceback (most recent call last):
                File "<stdin>", line 4, in <module>
                AssertionError: division by zero





                share|improve this answer












                Simple Answer



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from None


                However, you probably actually want:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from e


                Explanation



                __cause__



                Implicit exception chaining happens through __context__ when there isn't an explicit cause exception set.



                Explicit exception chaining works through __cause__ so if you set __cause__ to the exception itself, it should stop the chaining. If __cause__ is set, Python will suppress the implicit message.



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                exc.__cause__ = exc
                raise exc


                raise from



                We can use "raise from" to do the same thing:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                raise exc from exc


                None __cause__



                Setting __cause__ to None actually does the same thing:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                exc = AssertionError(str(e))
                exc.__cause__ = None
                raise exc


                raise from None



                So that brings us to the most elegant way to do this which is to raise from None:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from None


                But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:



                try:
                print(10/0)
                except ZeroDivisionError as e:
                raise AssertionError(str(e)) from e


                This will give us a slightly different message that states that the first exception was the direct cause of the second:



                Traceback (most recent call last):
                File "<stdin>", line 2, in <module>
                ZeroDivisionError: division by zero

                The above exception was the direct cause of the following exception:

                Traceback (most recent call last):
                File "<stdin>", line 4, in <module>
                AssertionError: division by zero






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '15 at 9:00









                Trey Hunner

                6,24333153




                6,24333153






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f33809864%2fdisable-exception-chaining-in-python-3%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...