www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/01/08:18:37

From: mrkite AT cyberjunkie DOT com (Marco Salvalaggio)
Newsgroups: comp.os.msdos.djgpp
Subject: Serious djgpp problem with exceptions ?
Date: Sat, 01 Feb 1997 12:04:11 GMT
Lines: 89
Message-ID: <32f93129.6685902@news.ind.mh.se>
NNTP-Posting-Host: ppp-165.mclink.it
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi all,
    this is a second post, since nobody replied to the first posted a week
ago. Unless I'm doing some stupid mistake, this can be a serious djgpp
problem. I'm trying to port an app that works fine with BC++ 4.5 and,
though it compiles ok with djgpp, it doesn't behave as I expect at run-
time. In this app I have a hierarchy of exception classes so that I can
detect different error conditions and/or exploit the power of polymorphism.
The problem is that trying to catch an exception object of base class type
when a derived exception obj was thrown causes the prog to terminate().
To simplify the question here comes a little program that behaves the same
way.
--------------------------------------------------------------------------------------------------
// Except.cc
//
//	Testing djgpp exception handling.
//
#include <iostream.h>
#include <string.h>

class MyExcept
{
   char msg[80];

public:
   MyExcept(const char *s) {  strcpy(msg, s); }

   virtual void why() { cout << endl << msg << flush; }
};

class MyOtherExcept : public MyExcept
{
   int dummy;

public:
   MyOtherExcept( const char *s, int n ) : MyExcept(s), dummy(n) {}
   void why() { MyExcept::why(); cout << ' ' << dummy << flush; }
};

void f1();
void f2();

void main()
{
   try
   {
      f1();
   }
   catch( MyExcept& x )	// This one ok MyExcept thrown, MyExcept catched
   {
      x.why();
   }

   try
   {
      f2();
   }
   catch( MyExcept& x )	// This one not ok. The program aborts.
   {
      x.why();
   }
}

void f1()
{
   throw(MyExcept("Error in f1()"));
}

void f2()
{
   throw( MyOtherExcept("Error in f2()", 16) );
}
--------------------------------------------------------------------------------------------------

Compiled with BC++ 4.5 this one gives the correct output:

Error in f1()
Error in f2() 16

compiled with djgpp with the following command:

	gcc -fhandle-exceptions except.cc -lstdcx -liostr

it gives:

Error in f1()Abort !

Thanks in advance to anyone that will reply me.

Marco.

- Raw text -


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