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

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript