How to capture error message from exception in call to integrate?
Sometimes when I call integrate using different algorithms, I get TypeError
exception. (or any other, but this one seems to be the most common)
I'd like to capture into a variable, the message that the other CAS could have generated also, this way I would know if the exception was due to sagemath interface to the other CAS, or if it is due to the other CAS generating internal error on its own.
An example will make it easy to explain. In the following, I can capture the exception name itself. But not the rest of the message that normally show on the terminal.
var('x')
try:
integrate(log(1+x)/x/(1+(1+x)^(1/2))^(1/2),x, algorithm="fricas")
except Exception as ee:
print("Exception is ",ee)
the_exception = ee
The above prints on the screen the following
Exception is An error occurred when FriCAS evaluated 'integrate(sage2,sage1)':
>> Error detected within library code:
integrate: implementation incomplete (constant residues)
I can capture into a variable, the name of the exception
sage: name_of_exception = the_exception.__class__.__name__
'TypeError'
But how to capture into a variable the rest of error message that shows on the screen? In particular the message
>> Error detected within library code:
integrate: implementation incomplete (constant residues)
This will help me later know if the error was due to interface error, or due to internal error in the other CAS being used to do the integration. I looked at Python documentation for Exception, but so far did not see anything. There might be a sagemath specific way to do this?
Does
the_exception.args[0]
do what you want? (I don't have Fricas installed, so I can't test this.)@john. Great, yes, this did it. If you like to make this as answer, will be happy to accept it.