Hello!
I’ve added sometimes ago a loadUrl in the excellent ofxXmlSettings addon.
It’s working as expected but it seems that the HTTPStreamFactory does not throw the exceptions he’s supposed to. It’s kind of a big problem in my case because I really need to be able to detect when there’s a problem with a potential unknown url or even more important when the computer has lost his internet connection…
I can’t find anything about that in poco documentation/forum so I guess it has to do with the of build of the library!
Here’s the method it self, I’ve add every try/catch I could just to be sure that every exception is catched, but well, no success with that:
bool ofxXmlSettings::loadUrl(string url){
bool loadOkay = false;
std::string xmlString;
// Register Poco HTTPStreamFactory
if(!HTTPStreamFactoryLoaded) {
HTTPStreamFactory::registerFactory();
HTTPStreamFactoryLoaded = true;
}
// URIStreamOpener may throw an exception
try {
// Open URI Stream
std::istream* streamOpener;
try{
URIStreamOpener::defaultOpener();
try {
streamOpener = URIStreamOpener::defaultOpener().open(url);
}
catch (Poco::Exception& e){
std::string what = e.displayText();
printf("%s\n", what.c_str());
}
}
catch (Poco::Exception& e){
std::string what = e.displayText();
printf("%s\n", what.c_str());
}
std::auto_ptr<std::istream> pStr(streamOpener);
// Copy Stream to string
StreamCopier::copyToString(*pStr.get(), xmlString);
// Load string
loadOkay = loadString(xmlString);
}
catch (Poco::Exception& e){
std::string what = e.displayText();
printf("%s\n", what.c_str());
throw;
}
return loadOkay;
}
You can try by disabling your internet connection and try to load any xml from an url or trying to load a false url.
Does someone has come to a solution for this problem?
Thanks a lot!
Simon.
PS: Here’s a more readable code (without the unnecessary try/catch) :
bool ofxXmlSettings::loadUrl(string url){
bool loadOkay = false;
std::string xmlString;
// Register Poco HTTPStreamFactory
if(!HTTPStreamFactoryLoaded) {
HTTPStreamFactory::registerFactory();
HTTPStreamFactoryLoaded = true; // this is a global var
}
// URIStreamOpener may throw an exception
try {
// Open URI Stream
std::istream* streamOpener;
URIStreamOpener::defaultOpener();
streamOpener = URIStreamOpener::defaultOpener().open(url);
std::auto_ptr<std::istream> pStr(streamOpener);
// Copy Stream to string
StreamCopier::copyToString(*pStr.get(), xmlString);
// Load string
loadOkay = loadString(xmlString);
}
}
return loadOkay;
}
damian
January 9, 2010, 6:42pm
#2
hi Simon,
it’s possible that C++ exceptions are disabled in your project settings. create a basic try/catch block and force-throw an exception yourself - see if it gets caught.
hth,
d
Hey Thanks Damian!
Didn’t knew it was possible to deactivate Exceptions!
Actually I switched to the last release of Openframeworks, and I don’t know if it’s because Poco has been updated or because the project’s settings has been changed but now it’s working perfectly, throwing exceptions as expected!
Anyway thanks a lot! It’s a good thing to know!
Take care,
Simon.
Hey,
I’ve got a very similar problem as you and don’t really know what to do. My app crashes when there is no Internet connection instead of catching the Exception. I’m pretty sure this is the issue as it matches the stack trace:
#0 005177C3 _Unwind_SjLj_RaiseException() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#1 00517D4A _Unwind_SjLj_Resume_or_Rethrow() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#2 0050C60E __cxa_rethrow() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#3 004B699E Poco::Net::SocketImpl::connect() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#4 004B52D4 Poco::Net::StreamSocket::connect() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#5 004A4CA6 Poco::Net::HTTPSession::connect() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#6 0049526B Poco::Net::HTTPClientSession::reconnect() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#7 004944DE Poco::Net::HTTPClientSession::sendRequest() (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iostream:77)
#8 00452993 scPulse::threadedFunction(this=0x1b597fc) (C:/Documents and Settings/Elie Zananiri/My Documents/of_preRelease_v0061_win_cb_FAT/apps/1TSQ/OneTSQ/src/scPulse.cpp:45)
#9 005388CA ofxThread::thread(objPtr=0x1b597fc) (C:/Documents and Settings/Elie Zananiri/My Documents/of_preRelease_v0061_win_cb_FAT/addons/ofxThread/src/ofxThread.h:37)
#10 77C3A3B0 msvcrt!_endthreadex() (C:\WINDOWS\system32\msvcrt.dll:??)
#11 01B597FC ??() (??:??)
#12 003F01C8 ??() (??:??)
#13 7C927784 ntdll!RtlPcToFileHeader() (C:\WINDOWS\system32\ntdll.dll:??)
#14 01B58A10 ??() (??:??)
#15 00000000 ??() (??:??)
I think I have exceptions turned on (although I can’t seem to find that setting in Code::Blocks) because I see the expected output with the following code:
try {
throw 20;
} catch (int e) {
cout << "An exception occurred. Exception Nr. " << e << endl;
}
Here is the full code of the function in question (it’s basically code from the SimpleWebScraper):
while (isThreadRunning()) {
// ping the server
try {
URI uri(url);
string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
ofLog(OF_LOG_NOTICE, "Pulse HTTP Request Status: " + uri.getHost() + " " + ofToString(uri.getPort()));
session.sendRequest(req);
HTTPResponse res;
std::istream& rs = session.receiveResponse(res);
ofLog(OF_LOG_NOTICE, "Pulse HTTP Response Status: " + ofToString(res.getStatus()) + " " + res.getReason());
} catch (Exception& exc) {
ofLog(OF_LOG_ERROR, exc.displayText());
}
ofSleepMillis(interval);
}
I’m on Code::Blocks under Windows XP. Any help is appreciated!
Thanks,
-Elie