You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Wijnand Suijlen <wi...@gmail.com> on 2008/08/28 18:10:57 UTC

No exception thrown when opening non existing file

Hi all,

I have the the following program:

#include <iostream>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/PlatformUtils.hpp>

using namespace xercesc;

int main()
{
 XMLPlatformUtils :: Initialize();

 XercesDOMParser *parser = new XercesDOMParser;

 try {
   parser->parse("non existing file");
 }
 catch(...)
 {
   std::cout << "it threw an exception" << std::endl;
   delete parser;
   XMLPlatformUtils::Terminate();
   return 0;
 }

 std:: cout << "no exception was thrown" << std::endl;

 delete parser;
 XMLPlatformUtils::Terminate();
 return 0;
}


Since I don't have a file on my system which is called 'non existing
file', I expect that it would output 'it threw an exception', but it
doesn't: It outputs 'no exception was thrown'. To me this seems very
weird. Is this is a bug or a feature? How do I get to know whether the
file exists or not?

I am using gcc 4.2.1 with Xerces 2.8.

Thanks very much!
Wijnand

Re: No exception thrown when opening non existing file

Posted by Wijnand Suijlen <wi...@gmail.com>.
I assumed that the default behaviour was that exceptions are thrown
when a fatal error is encountered, which is IMHO normal C++ library
behaviour. That was of course a false assumption. I compiled and
executed the same code again, but now with an implementation of
ErrorHandler set, and everything works very well.

Thanks for your help,
Wijnand

2008/8/28 Roberto Rigamonti <ro...@gmail.com>:
> Good evening,
>             I think that this happens because the default error handler is
> invoked (you haven't set yours), and its behavior is: do nothing.
> To catch errors while parsing you need to install an error handler, the
> samples available in the Xerces package should help you in doing so.
> Hope this helps!
>               Roberto
>
> Wijnand Suijlen wrote:
>>
>> Hi all,
>>
>> I have the the following program:
>>
>> #include <iostream>
>> #include <xercesc/parsers/XercesDOMParser.hpp>
>> #include <xercesc/util/PlatformUtils.hpp>
>>
>> using namespace xercesc;
>>
>> int main()
>> {
>>  XMLPlatformUtils :: Initialize();
>>
>>  XercesDOMParser *parser = new XercesDOMParser;
>>
>>  try {
>>   parser->parse("non existing file");
>>  }
>>  catch(...)
>>  {
>>   std::cout << "it threw an exception" << std::endl;
>>   delete parser;
>>   XMLPlatformUtils::Terminate();
>>   return 0;
>>  }
>>
>>  std:: cout << "no exception was thrown" << std::endl;
>>
>>  delete parser;
>>  XMLPlatformUtils::Terminate();
>>  return 0;
>> }
>>
>>
>> Since I don't have a file on my system which is called 'non existing
>> file', I expect that it would output 'it threw an exception', but it
>> doesn't: It outputs 'no exception was thrown'. To me this seems very
>> weird. Is this is a bug or a feature? How do I get to know whether the
>> file exists or not?
>>
>> I am using gcc 4.2.1 with Xerces 2.8.
>>
>> Thanks very much!
>> Wijnand
>>
>

Re: No exception thrown when opening non existing file

Posted by Roberto Rigamonti <ro...@gmail.com>.
Good evening,
              I think that this happens because the default error 
handler is invoked (you haven't set yours), and its behavior is: do nothing.
To catch errors while parsing you need to install an error handler, the 
samples available in the Xerces package should help you in doing so.
Hope this helps!
                Roberto

Wijnand Suijlen wrote:
> Hi all,
> 
> I have the the following program:
> 
> #include <iostream>
> #include <xercesc/parsers/XercesDOMParser.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> 
> using namespace xercesc;
> 
> int main()
> {
>  XMLPlatformUtils :: Initialize();
> 
>  XercesDOMParser *parser = new XercesDOMParser;
> 
>  try {
>    parser->parse("non existing file");
>  }
>  catch(...)
>  {
>    std::cout << "it threw an exception" << std::endl;
>    delete parser;
>    XMLPlatformUtils::Terminate();
>    return 0;
>  }
> 
>  std:: cout << "no exception was thrown" << std::endl;
> 
>  delete parser;
>  XMLPlatformUtils::Terminate();
>  return 0;
> }
> 
> 
> Since I don't have a file on my system which is called 'non existing
> file', I expect that it would output 'it threw an exception', but it
> doesn't: It outputs 'no exception was thrown'. To me this seems very
> weird. Is this is a bug or a feature? How do I get to know whether the
> file exists or not?
> 
> I am using gcc 4.2.1 with Xerces 2.8.
> 
> Thanks very much!
> Wijnand
>