How to throw standard exceptions ?

T

Timothy Madden

Hello

Almost every program I write starts by checking the number
of arguments and throws an exception if the syntax is not
right. And every time I get the same error: the exception
is not caught with its type, but with the last catch (...)
handler !

Here's an almost minimal example.
Can anyone please tell me what is wrong with my code ?


#include <stdexcept>
#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, const char *argv[])
try
{
if (argc != 3)
throw
new
runtime_error
(
"Processes a Filemon log file and re-creates "
"the directories and files listed\n"
"into the given path.\n"
"Syntax:\n"
"\tclassify LogFile.LOG \\root_path\n"
"\n"
);
else
{
// Some processing
// ...

return EXIT_SUCCESS;
}
}
catch (const runtime_error &e)
{
cerr << "Error:\n";
cerr << e.what();
cerr << endl;

return EXIT_FAILURE;
}
catch(const exception &e)
{
cerr << "Error:\n";
cerr << e.what();
cerr << endl;

return EXIT_FAILURE;
}
catch (...)
{
cerr << "General error.\n";

return EXIT_FAILURE;
}

When I compile and run my program I get:

adrian@darkstar: g++ -o command command.cc
adrian@darkstar: ./command
General failure.

I have tried with Slackware Linux 2.6.14 gcc 4.2.4
and mingw on Win32, gcc 3 ...


Thank you,
Timothy Madden
 
T

Timothy Madden

peter said:
Hello

Almost every program I write starts by checking the number
of arguments and throws an exception if the syntax is not
right. And every time I get the same error: the exception
is not caught with its type, but with the last catch (...)
handler !
[...]

int main(int argc, const char *argv[])
try
{
if (argc != 3)
throw
new
runtime_error
(
"Processes a Filemon log file and re-creates "
"the directories and files listed\n"
"into the given path.\n"
"Syntax:\n"
"\tclassify LogFile.LOG \\root_path\n"
"\n"
);
[...]

You are throwing a pointer to a runtime_error, not a runtime_error.

Oh damn ... !
Of course I am. :)

Thank you
Timothy Madden
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top