-1
if not(my_value < max_limit):
    print "value of is %g and hence invalid. It can be upto $g" % (my_value, max_limit)
    raise LimitFailureCheck("Failed due to Incorrect value")

I have defined my custom exception as LimitFailureCheck in other module. I want to raise it when my_value > max_limt. Hence I have coded like the above method. It works correctly. What I want to ask is to give user more info I have written print statement also which states what exactly the problem is. Can I do the same thing while raising my custom exception? I tried

raise LimitFailureCheck("Failed due to Incorrect %g value" % my_value)

But it raised the same statement when printed an output.

raise LimitFailureCheck("Failed due to Incorrect %g value" % my_value)

I was hoping to get

raise LimitFailureCheck("Failed due to Incorrect 99 value")

Output I received:


Traceback (most recent call last):
  File "runtest.py", line 69, in attempt
    func()
  File "c:\Users\pran\projects\check.py", line 66, in runmytest
    raise LimitFailureCheck('"Failed due to Incorrect %g value" % my_value
)
LimitFailureCheck
tryPy
  • 71
  • 1
  • 11
  • 3
    Are you sure it did not also print the message with the substitution? It will show the `raise` as the last line of the traceback, but then it should also print the exception message after that. – BrenBarn Sep 09 '14 at 18:52
  • 1
    This doesn't make any sense. The standard string-formatting operators (either `%` or `.format`) should work in any context, including created an exception. I suspect that you are looking at the wrong part of the output as @BrenBarn suggests. – Dan Lenski Sep 09 '14 at 18:53
  • This question appears to be off-topic because the problem isn't real. – Veedrac Sep 09 '14 at 18:54
  • How was it printed? - did other code catch the exception and print or did you get the standard python traceback message as the script terminated? Are you saying that it both cases "Failed due to Incorrect value" was printed? – tdelaney Sep 09 '14 at 18:57
  • Please provide the exception you received – notorious.no Sep 09 '14 at 19:12
  • @DanLenski I have edited the output I'm receiving. – tryPy Sep 09 '14 at 19:42
  • 2
    I think your code catches the exception, prints the traceback and then prints the type of the exception, not the exception message. So, the interesting code is the exception handler. – tdelaney Sep 09 '14 at 19:43
  • Either that, or your LimitFailureCheck class has an init but its not passing the message down to the original Exception. – tdelaney Sep 09 '14 at 19:45
  • What BrenBarn and @tdelaney said. Your code is catching the exception somewhere higher up and printing the exception, with something like: `try: ... except Exception as e: print type(e).__name__` – Dan Lenski Sep 09 '14 at 19:46

2 Answers2

0

when you run python on the command line and python exits due to a unhandled exception, python will show a traceback and the exception string.

When I run this test program

class LimitFailureCheck(Exception):
    pass

raise LimitFailureCheck('i am %s' % 'sad')

Python says

Traceback (most recent call last):
  File "sad.py", line 4, in <module>
    raise LimitFailureCheck('i am %s' % 'sad')
__main__.LimitFailureCheck: i am sad

Notice that python displayed the line that failed and the failure message. If you don't want that behavior, you can catch the exception and print your own message. Here's another script

class LimitFailureCheck(Exception):
    pass

try:
    raise LimitFailureCheck('i am %s' % 'sad')
except LimitFailureCheck, e:
    print 'raise LimitFailureCheck("%s")' % e

that displays

raise LimitFailureCheck("i am sad")
tdelaney
  • 73,364
  • 6
  • 83
  • 116
-1

You may want to try format.

raise LimitFailureCheck('Failed due to Incorrect {0} value'.format(my_value)
Dylan Lawrence
  • 1,503
  • 10
  • 32
  • That's another way to format strings, but it doesn't solve the question about why the formatted string didn't print. – tdelaney Sep 09 '14 at 18:58
  • @tdelaney How I read this was that it did print, it just didn't format, it printed the %g instead of its format value. format() is more consistent, but perhaps I interpreted the question wrong. – Dylan Lawrence Sep 09 '14 at 18:59
  • oh, I get it! We are both wrong. OP is complaining because the python traceback shows the line where the failure happened, not the formatted message. btw - i wasn't the downvoter so can't fix that. – tdelaney Sep 09 '14 at 19:01
  • @tdelaney ah well, I expect every answer I do to get downvoted, it is stack after all – Dylan Lawrence Sep 09 '14 at 19:03