Re: exceptions in gnu c++
Hi Paul,
On Sun, 2006-11-05 at 14:29 -0700, Paul E Condon wrote:
> I've written a simulation program in c++. It works for
> small problems of its class, but fails during initialization
> on large problems. I suspect I need more RAM, but would
> like confirmation, so ...
>
> I changed the main program to include a everything in a
> try block with a catch block, etc. But it still dies in
> the same way. Where can I find an example of exception
> handling code that works with gcc 3.3?
Can you switch to 3.4+? 3.3 is buggy.
> My code is:
> using namespace std;
>
> #include "Sys.hh"
> #include "nmlib.hh"
> #include "rgen2.hh"
> #include "nmsim.hh"
>
> int main( int argc, char* argv[] ){
> cout<<"Starting nmmain..."<<endl;
> try {
> NMsim X; // instantiate the simulation object
> X.nminit( argc, argv ); // read in parameters and generate starting conditions
> X.nmcore( ); // do a simulation run
> } catch (const exception caught) {
You should catch a reference to an exception object - not a real object.
The reason is, that you'd only get a copy, and that may not be done
right. So you should have
} catch (const exception& caught) {
> cout << "caught exception: "<<caught.what() <<"!!!"<< endl;
Perhaps you should return a non-null value here. Also, perhaps you want
to dump error messages to `std::cerr' and debug messages to
`std::clog'.
> }
> cout<<" After catch."<<endl;
> return 0;
> };
>
> NMsim::NMsim(){};
>
> </end>
>
> The output is:
> gq:/da4/wdl/H$ nmmain --steps 200 --stpsiz 200 --kernparm 1024. vaa.a
> Starting nmmain...
> init set to 255
> done with tbl/abn/sid preparation
> start dispersal calc with rx=5418
> row: 21100 128 Killed
> gq:/da4/wdl/H$
> </end>
>
> Notice the 'debug' statements being sent to cout.
> 'Killed' comes from some gcc library, not from my code,
> especially not from my catch block.
I think the reference will fix it. If not, try running the program
through GDB and set a break-point in `std::terminate' (the function
called for un-caught exceptions) and then back-trace.
> The detailed
> syntax that I'm attempting to use is taken from
> Josuttis, p. 15.
Perhaps a URL would be good?
> He footnotes it with a caveat that
> it is not in the Standard, only proposed. Looks like
> something in the c++ library is catching whatever
> is being thrown (if anything) and killing nmmain
> before control gets to my catch and/or return stmnts.
Ye, that would be `std::terminate'. However, the caveat is probably no
longer valid. The `std::terminate' function will be called if your
catch blocks does not match the exception thrown. The C++ library only
throws exceptions that inherit from std::exception, so that should be
caught. However, if some code throws a std::string or something, that
will not be caught by your catch block. If you want to catch all
exceptions, no matter the type, you could append _another_ catch block
like
catch (...) {
std::cerr << "Caught unknown exception" << std::endl;
}
> suggestions? (like maybe a more appropriate list to ask?)
A list dedicated to C++ questions would be an obvious choice :-)
Yours,
--
___ | Christian Holm Christensen
|_| | -------------------------------------------------------------
| | Address: Sankt Hansgade 23, 1. th. Phone: (+45) 35 35 96 91
_| DK-2200 Copenhagen N Cell: (+45) 24 61 85 91
_| Denmark Office: (+45) 353 25 404
____| Email: cholm@nbi.dk Web: www.nbi.dk/~cholm
| |
Reply to: