www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/04/07:59:17

Xref: news2.mv.net comp.os.msdos.djgpp:8249
From: Ian Miller <itmiller AT dra DOT hmg DOT gb>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: exception handling
Date: Tue, 03 Sep 1996 15:35:20 +0100
Organization: DRA HASN
Lines: 109
Message-ID: <322C4228.2781E494@dra.hmg.gb>
References: <mikeday DOT 8 DOT 00040420 AT melbpc DOT org DOT au> <32237837 DOT 794BDF32 AT ift DOT ulaval DOT ca> <32243622 DOT 167EB0E7 AT dra DOT hmg DOT gb>
NNTP-Posting-Host: 146.80.115.106
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I have taken another look at this, and I got it
working this time.

	I can confirm that the following program
*does* catch the int and write "Caught int 10":

--- Start of test.cc ---
#include <stdlib.h>
#include <iostream.h>

int
main(int, char**)
{
    try
    {
	throw 10;
    }
    catch (int i)
    {
	cerr << "Caught int " << i << "\n";
    }

    cerr << "Done\n";
    exit(0);
} // main(int, char**)
--- End of test.cc ---

And this more ambitious version works as well.
Uncomment whichever of the throw statements you like.

--- Start of test2.cc ---
#include <stdlib.h>
#include <iostream.h>

void fn();

int
main(int, char**)
{
    try
    {
	fn();
    }
    catch (double d)
    {
	cerr << "Caught double " << d << "\n";
    }
    catch (int i)
    {
	cerr << "Caught int " << i << "\n";
    }
    catch (char c)
    {
	cerr << "Caught char \'" << c << "\'\n";
    }
    catch (char* s)
    {
	cerr << "Caught string \"" << s << "\"\n";
    }

    cerr << "Done\n";
    exit(0);
} // main(int, char**)


void
fn()
{
    cerr << "Made it into fn()\n";
//    throw 12;
    throw 12.2;
//    throw "this is a sentence";
//    throw 'q';
}

--- End of test2.cc ---

	My problem was with optimisation switches.
The following definitely works:

gcc -fhandle-exceptions -o test.exe test.cc -lstdcx

and so does:

gcc -O2 -fhandle-exceptions -o test.exe test.cc -lstdcx

However, the -O3 optimisation switch (which I was using
by default) breaks the catch mechanism so that
terminate() is the result of every throw. I suppose this
to be because the exception handling code assumes things
about the stack that are not true after -O3 has done its
work (inlining functions, in particular, I think).

	Notice that I have had to specify only the
stdcx library on the gcc command line. It contains
sufficient (all?) iostream modules for the above
programs, and the terminate() function required as the
failsafe handler for unexpected C++ exceptions. (I don't
use gxx. It's just an alias for gcc that includes C++
libraries by default, but it actually does not include
stdcx - the only one you need! - in DJGPP v2.00.)

	Notice also that I have not had to specify any
special include files. This mechanism is part of the
language; not part of the library.

-- 
Ian Miller, Dorset, Africa
DJGPP 2.00, Win95 DOS box (LFN=n)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019