How to get a useful exception message from decimal in python 3?How can I represent an 'Enum' in Python?How to round a number to n decimal places in JavaHow do you assert that a certain exception is thrown in JUnit 4 tests?How can I safely create a nested directory in Python?How to properly ignore exceptionsProper way to declare custom exceptions in modern Python?Manually raising (throwing) an exception in Pythonpython exception message capturingUnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>Python catching entry exception with decimal.InvalidOperation

Stark VS Thanos

Weird result in complex limit

How does a Swashbuckler rogue "fight with two weapons while safely darting away"?

Confused by notation of atomic number Z and mass number A on periodic table of elements

Please, smoke with good manners

How can I get precisely a certain cubic cm by changing the following factors?

Does jamais mean always or never in this context?

Build a trail cart

Binary Numbers Magic Trick

Single Colour Mastermind Problem

What does 「再々起」mean?

Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?

How to creep the reader out with what seems like a normal person?

How to replace the "space symbol" (squat-u) in listings?

What does "rf" mean in "rfkill"?

Can a creature tell when it has been affected by a Divination wizard's Portent?

Help, my Death Star suffers from Kessler syndrome!

Do I have an "anti-research" personality?

Why is the origin of “threshold” uncertain?

Asahi Dry Black beer can

Was it really necessary for the Lunar Module to have 2 stages?

Does a creature that is immune to a condition still make a saving throw?

Python "triplet" dictionary?

Is GOCE a satellite or aircraft?



How to get a useful exception message from decimal in python 3?


How can I represent an 'Enum' in Python?How to round a number to n decimal places in JavaHow do you assert that a certain exception is thrown in JUnit 4 tests?How can I safely create a nested directory in Python?How to properly ignore exceptionsProper way to declare custom exceptions in modern Python?Manually raising (throwing) an exception in Pythonpython exception message capturingUnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>Python catching entry exception with decimal.InvalidOperation






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















With Python 2, creating a Decimal with an invalid string produces a useful error message:



>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'spam'


While Python 3 produces a not-so-helpful message:



>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]


Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?



I'm using Python 2.7.15 and Python 3.7.2, both on darwin.



Addenda:



It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009



This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227










share|improve this question






























    0















    With Python 2, creating a Decimal with an invalid string produces a useful error message:



    >>> import decimal
    >>> decimal.Decimal('spam')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
    "Invalid literal for Decimal: %r" % value)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
    raise error(explanation)
    decimal.InvalidOperation: Invalid literal for Decimal: 'spam'


    While Python 3 produces a not-so-helpful message:



    >>> import decimal
    >>> decimal.Decimal('spam')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]


    Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?



    I'm using Python 2.7.15 and Python 3.7.2, both on darwin.



    Addenda:



    It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009



    This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227










    share|improve this question


























      0












      0








      0








      With Python 2, creating a Decimal with an invalid string produces a useful error message:



      >>> import decimal
      >>> decimal.Decimal('spam')
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
      "Invalid literal for Decimal: %r" % value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
      raise error(explanation)
      decimal.InvalidOperation: Invalid literal for Decimal: 'spam'


      While Python 3 produces a not-so-helpful message:



      >>> import decimal
      >>> decimal.Decimal('spam')
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]


      Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?



      I'm using Python 2.7.15 and Python 3.7.2, both on darwin.



      Addenda:



      It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009



      This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227










      share|improve this question
















      With Python 2, creating a Decimal with an invalid string produces a useful error message:



      >>> import decimal
      >>> decimal.Decimal('spam')
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
      "Invalid literal for Decimal: %r" % value)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
      raise error(explanation)
      decimal.InvalidOperation: Invalid literal for Decimal: 'spam'


      While Python 3 produces a not-so-helpful message:



      >>> import decimal
      >>> decimal.Decimal('spam')
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]


      Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?



      I'm using Python 2.7.15 and Python 3.7.2, both on darwin.



      Addenda:



      It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009



      This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227







      python-3.x exception decimal invalidoperationexception






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 19:14







      heckman

















      asked Mar 20 at 21:11









      heckmanheckman

      13




      13






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You could monkey-patch the decimal module.



          import decimal


          def safe_decimal(something):
          try:
          funct_holder(something)
          except Exception as e:
          new_errror = Exception("Hey silly that's not a decimal, what should I do with this? ".format(something))
          raise new_errror from None


          funct_holder = decimal.Decimal
          decimal.Decimal = safe_decimal


          Then you could use the monkey patched version as so



          >>> decimal.Decimal('hello')
          Traceback (most recent call last):
          File "<input>", line 12, in <module>
          File "<input>", line 6, in safe_decimal
          Exception: Hey silly that's not a decimal, what should I do with this? hello





          share|improve this answer























          • I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

            – heckman
            Mar 21 at 1:10











          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%2f55270258%2fhow-to-get-a-useful-exception-message-from-decimal-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









          1














          You could monkey-patch the decimal module.



          import decimal


          def safe_decimal(something):
          try:
          funct_holder(something)
          except Exception as e:
          new_errror = Exception("Hey silly that's not a decimal, what should I do with this? ".format(something))
          raise new_errror from None


          funct_holder = decimal.Decimal
          decimal.Decimal = safe_decimal


          Then you could use the monkey patched version as so



          >>> decimal.Decimal('hello')
          Traceback (most recent call last):
          File "<input>", line 12, in <module>
          File "<input>", line 6, in safe_decimal
          Exception: Hey silly that's not a decimal, what should I do with this? hello





          share|improve this answer























          • I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

            – heckman
            Mar 21 at 1:10















          1














          You could monkey-patch the decimal module.



          import decimal


          def safe_decimal(something):
          try:
          funct_holder(something)
          except Exception as e:
          new_errror = Exception("Hey silly that's not a decimal, what should I do with this? ".format(something))
          raise new_errror from None


          funct_holder = decimal.Decimal
          decimal.Decimal = safe_decimal


          Then you could use the monkey patched version as so



          >>> decimal.Decimal('hello')
          Traceback (most recent call last):
          File "<input>", line 12, in <module>
          File "<input>", line 6, in safe_decimal
          Exception: Hey silly that's not a decimal, what should I do with this? hello





          share|improve this answer























          • I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

            – heckman
            Mar 21 at 1:10













          1












          1








          1







          You could monkey-patch the decimal module.



          import decimal


          def safe_decimal(something):
          try:
          funct_holder(something)
          except Exception as e:
          new_errror = Exception("Hey silly that's not a decimal, what should I do with this? ".format(something))
          raise new_errror from None


          funct_holder = decimal.Decimal
          decimal.Decimal = safe_decimal


          Then you could use the monkey patched version as so



          >>> decimal.Decimal('hello')
          Traceback (most recent call last):
          File "<input>", line 12, in <module>
          File "<input>", line 6, in safe_decimal
          Exception: Hey silly that's not a decimal, what should I do with this? hello





          share|improve this answer













          You could monkey-patch the decimal module.



          import decimal


          def safe_decimal(something):
          try:
          funct_holder(something)
          except Exception as e:
          new_errror = Exception("Hey silly that's not a decimal, what should I do with this? ".format(something))
          raise new_errror from None


          funct_holder = decimal.Decimal
          decimal.Decimal = safe_decimal


          Then you could use the monkey patched version as so



          >>> decimal.Decimal('hello')
          Traceback (most recent call last):
          File "<input>", line 12, in <module>
          File "<input>", line 6, in safe_decimal
          Exception: Hey silly that's not a decimal, what should I do with this? hello






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 20 at 21:34









          JBirdVegasJBirdVegas

          6,26312643




          6,26312643












          • I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

            – heckman
            Mar 21 at 1:10

















          • I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

            – heckman
            Mar 21 at 1:10
















          I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

          – heckman
          Mar 21 at 1:10





          I've already wrapped the decimal creation in a function that does something very similar to your function (but using raise ValueError(something))... I was hoping I could extract something directly from [<class 'decimal.ConversionSyntax'>] to avoid the work-around.

          – heckman
          Mar 21 at 1:10



















          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%2f55270258%2fhow-to-get-a-useful-exception-message-from-decimal-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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현