Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
#2650 Initialise detailMessage for PyException #97
I think this approach to exceptions, deferring the creation of a message, is deliberate.
Exceptions in Python are a normal means of control transfer, whereas in Java they're expicitly not. (I could find the refs, I think.) Once we come to print the thing out, of course, then we know it's not just a transfer of control, but I think we're putting it off until then.
Your use case is good, however. My instinct is that the exception ought not to escape an embedded interpreter without a proper message.
We get to do that for the JSR223 engine here, by wrapping:
Would it work to define
@jeff5 Thanks for the comments. Yes overriding
I implemented it in a Java way i.e. exceptions shouldn't be used for flow control, therefore if you are creating one you should make it contain all the information it needs and ideally make it immutable.
My only question would be is making the detail message noticeable when you consider that your about to create the Java stack trace anyway? I would have guessed that took much longer? But for now I will change this to lazily build the message.
Yes, it's more natural for Java, but the (C)Python way is influenced by good old
The Java stack trace might be no more than a pointer to the last frame at this stage. The Python one is a linked list of names, and no-one does any further text formatting until asked:
The most wasteful part may be the way we seem to re-throw the (Java) exception to work our way our of each (Python) frame, but I guess in that case you probably are heading for a stack trace not a control transfer.