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
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