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