0

I am trying to catch an exception that occurs inside a c++ Programm which i call from a python script using subprocess.checkout() command, but it does'nt seem to work as i intend. Although the Exception is caught, it doesnt have the necessary Information about the cause of exception. I want to print out the exception trace that occurs at c++ Level (in below case a floatingpointexception or Division by Zero error). I am new to python, Any help in this regard is highly appriciated. Thanks.

Below is a sample Programm that i use to debug this issue.

TestCexception.cc

#include<iostream>
#include<exception>
#include <stdexcept>

using namespace std;
int main()
{
    int a=5;
    int b=0;
            try
            {   cout<< "I am a statement before division"<< endl;
                int c = a/b;
                cout<< "I am a statement after division"<< endl;
            }
            catch(exception &e)
            {
                cerr << "Cerr - To test exception \n" << endl;
            }

    return 0;
}

TestPython.py

import os
import socket
import subprocess
import sys
import traceback

print('Sample Python to simualte exception') 
TestOP = 'Default'

try:
    TestOP = subprocess.check_output(["/lhome/workspace/testException/testxptn"]) 
except subprocess.CalledProcessError as e :
    tb = traceback.format_exc()
    print(e)
    print(tb)
    print(e.output)

Current output:

Sample Python to simualte exception
I am a statement before division
Traceback (most recent call last):
  File "TestPython.py", line 14, in <module>
    TestOP = subprocess.check_output(["/lhome/workspace/testException/testxptn"])
  File "/usr/lib/python2.7/subprocess.py", line 223, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['/lhome/workspace/testException/testxptn']' returned non-zero exit status -8

Output that I want to see or something similar

Sample Python to simualte exception
I am a statement before division
Floating point exception
Cerr - To test exception 
Marek R
  • 32,568
  • 6
  • 55
  • 140
Be_bro
  • 15
  • 1
  • 5
  • Possible duplicate of [Catching exception: divide by zero](https://stackoverflow.com/questions/6121623/catching-exception-divide-by-zero) – Marek R Nov 29 '19 at 11:18
  • You should never divide by zero in the first place. Doing so causes undefined behavior, so you're not guaranteed to have an exception at all. You can't control what happens after you divide by zero so don't do it. – eesiraed Nov 30 '19 at 00:35
  • Hi Alexander and Maxim, Thank you for your suggestion. I have only used the above sample program to make my problem clear. In reality, I encounter a runtime exception from a c++ program which i call from python, only I cannot determine what caused the program to crash. As the developer of c++ program is not myself, I cannot explain him whats wrong with his program with out catching the exception from c++. I hope this clarifies the problem statement. – Be_bro Nov 30 '19 at 12:30

1 Answers1

1

I am trying to catch an exception that occurs inside a c++

Integer division by zero doesn't generate a C++ exception. On Linux it raises SIGFPE signal.

python3 formats subprocess.CalledProcessError exception better:

Command '['/lhome/workspace/testException/testxptn']' died with <Signals.SIGFPE: 8>.

In python2 you can do:

signo_to_string = dict((getattr(signal, s), s) for s in dir(signal) if s.startswith("SIG") and not s.startswith("SIG_"))

except subprocess.CalledProcessError as e:
    print(e)
    if os.WIFSIGNALED(e.returncode):
        print("Terminated by %s.\n" % signo_to_string[abs(e.returncode)])
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271