How do I set a Python builtin Exception's message attribute when I raise it

sirlark

I know I can specify an arbitrary number of arguments to an Exception (or any subclass thereof) when I raise it, but I see that Exceptions have a message attribute which isn't populated.

def throw():
    raise Exception("This is my message", 10)

try:
    throw()
except Exception as E:
    print "message = ", E.message
    print E.args

produces

message = 
("This is my message", 10)

What I would like to do is raise a variety ValueErrors depending on how or why a value in my function isn't valid. I would also like to attach a code, so something like

raise ValueError(1, "You need to supply a string value")
# or
raise ValueError(2, "Not a valid place name") #but it is a valid string

I'm writing a code that runs callbacks supplied by the user, and I expect the user's functions to raise ValueErrors in certain situations, but their callback can also call into some library functions I provide, and I want to be able distinguish between the user's ValueErrors and Mine.

I know I can subclass the ValueError and check for that exception type explicitly, but that seems like overkill. These are all errors with user supplied values after all, so I would like to use the first argument to the exception as a code, and then somehow set the message attribute explicitly

raise ValueError(10, message = "Error 10 is something quite specific")

The above doesn't work, because exceptions don't accept keyword arguments on init.

Lev Levitsky

The message attribute is set when the Exception instance is created with a single argument. Otherwise it is empty:

In [1]: Exception(42).message
Out[1]: 42

In [2]: Exception('foo').message
Out[2]: 'foo'

In [3]: Exception('foo', 42).message
Out[3]: ''

Of course you can create an instance of Exception and manually set its .message attribute before raising:

e = Exception()
e.message = 'test'
raise e

But it would probably make a lot of sense to use a custom exception class instead.

Also note that Exception does not have a message attribute in Python 3, only args.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Which exception should I raise on bad/illegal argument combinations in Python?

From Java

How do I return NotFound() IHttpActionResult with an error message or exception?

From Java

How do I set the driver's python version in spark?

From Dev

How to use Python Mock to raise an exception - but with Errno set to a given value

From Dev

How do I get the server-side exception message with GWT?

From Dev

How do I get an Mvc 5 Controller's ActionDescriptor when using Attribute Routing?

From Dev

How do I parse XML with attribute in python?

From Dev

How do I set a node attribute to use a function when calculating its value?

From Dev

In Python how can I raise a custom exception and do a trace back as well?

From Dev

How do I test an exception message containing localized dates in nunit

From Dev

How do I modify the exception message in a rescue clause?

From Dev

How do I escape colons in an attribute name with Python's ElementTree?

From Dev

How do I raise DeprecationWarnings in Python 2.7?

From Dev

How do I raise an exception in an asm block?

From Dev

How do I set a Python builtin Exception's message attribute when I raise it

From Dev

When exception occurs should I raise or bubble it up?

From Dev

How do I raise a FileNotFoundError properly?

From Dev

How do I raise an event when a method is called using Moq?

From Dev

How do I set a attribute to a specific element when using a for loop in javaScript?

From Dev

Why do i get "Exception:" when i receive the exception message in dart?

From Dev

When should I raise LookupError in python?

From Dev

How do I set up LAMP without the forbidden message when viewing my site?

From Dev

Django auth custom business logic - how do I raise custom error message?

From Dev

What Python Exception should I raise when illegal state is asked for?

From Dev

Why do I get an error for trying to access a builtin class method attribute but not the same builtin function attribute

From Dev

How do I convert MultipartEntityBuilder to inputStream when exception message goes to the deadLetterChannel in camel?

From Dev

How do I raise HttpResponseForbidden in CreateAPIView

From Dev

How do I raise an exception if an evaluation returns nil in Ruby?

From Dev

How do I set an attribute on an object in Groovy

Related Related

  1. 1

    Which exception should I raise on bad/illegal argument combinations in Python?

  2. 2

    How do I return NotFound() IHttpActionResult with an error message or exception?

  3. 3

    How do I set the driver's python version in spark?

  4. 4

    How to use Python Mock to raise an exception - but with Errno set to a given value

  5. 5

    How do I get the server-side exception message with GWT?

  6. 6

    How do I get an Mvc 5 Controller's ActionDescriptor when using Attribute Routing?

  7. 7

    How do I parse XML with attribute in python?

  8. 8

    How do I set a node attribute to use a function when calculating its value?

  9. 9

    In Python how can I raise a custom exception and do a trace back as well?

  10. 10

    How do I test an exception message containing localized dates in nunit

  11. 11

    How do I modify the exception message in a rescue clause?

  12. 12

    How do I escape colons in an attribute name with Python's ElementTree?

  13. 13

    How do I raise DeprecationWarnings in Python 2.7?

  14. 14

    How do I raise an exception in an asm block?

  15. 15

    How do I set a Python builtin Exception's message attribute when I raise it

  16. 16

    When exception occurs should I raise or bubble it up?

  17. 17

    How do I raise a FileNotFoundError properly?

  18. 18

    How do I raise an event when a method is called using Moq?

  19. 19

    How do I set a attribute to a specific element when using a for loop in javaScript?

  20. 20

    Why do i get "Exception:" when i receive the exception message in dart?

  21. 21

    When should I raise LookupError in python?

  22. 22

    How do I set up LAMP without the forbidden message when viewing my site?

  23. 23

    Django auth custom business logic - how do I raise custom error message?

  24. 24

    What Python Exception should I raise when illegal state is asked for?

  25. 25

    Why do I get an error for trying to access a builtin class method attribute but not the same builtin function attribute

  26. 26

    How do I convert MultipartEntityBuilder to inputStream when exception message goes to the deadLetterChannel in camel?

  27. 27

    How do I raise HttpResponseForbidden in CreateAPIView

  28. 28

    How do I raise an exception if an evaluation returns nil in Ruby?

  29. 29

    How do I set an attribute on an object in Groovy

HotTag

Archive