Ask Your Question
1

How to capture error message from exception in call to integrate?

asked 2021-05-27 05:39:37 +0200

Nasser gravatar image

updated 2021-05-27 05:48:43 +0200

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?

edit retag flag offensive close merge delete

Comments

1

Does the_exception.args[0] do what you want? (I don't have Fricas installed, so I can't test this.)

John Palmieri gravatar imageJohn Palmieri ( 2021-05-27 07:30:20 +0200 )edit

@john. Great, yes, this did it. If you like to make this as answer, will be happy to accept it.

Nasser gravatar imageNasser ( 2021-05-27 07:40:23 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-05-27 17:06:21 +0200

Try the_exception.args[0].

In case anyone is interested, I found this by using your code and then first evaluating the_exception. It printed some relevant information, which was a good sign, so I typed the_exception.[TAB], and args was one of the few options that appeared. the_exception.args is a tuple containing the information you want in the zeroth entry.

edit flag offensive delete link more

Comments

1

str(the_exception) also works.

mwageringel gravatar imagemwageringel ( 2021-05-27 19:55:48 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-05-27 05:39:37 +0200

Seen: 38 times

Last updated: May 27