www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/27/00:56:20

From: mrkite AT cyberjunkie DOT com (Marco Salvalaggio)
Newsgroups: comp.os.msdos.djgpp
Subject: catching derived exceptions
Date: Sun, 26 Jan 1997 22:55:38 GMT
Lines: 88
Message-ID: <32f6e0e5.4832112@news.ind.mh.se>
NNTP-Posting-Host: 193.76.234.167
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi all,
      i'm an happy user of Borland compilers (since Turbo C, without the ++
part) and i'm taking in consideration passing to djgpp. So, after various
MB of downloading, i'm trying to port some DOS applications i've made. Most
of them worked pretty fine but one has gone in big trouble. Actually it
compiles with no errors but shows a strange behavior 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 when i try to catch an exception object of base class type and a
derived exception obj was thrown the prog just terminate().
To simplify the question here comes a little program that behaves the same
way.
--------------------------------------------------------------------------------------------------
#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; }
};

class MyOtherExcept : public MyExcept
{
   int dummy;

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

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 MyOtherExcept thrown
   {
      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 output:

Error in f1()
Error in f2() 16

compiled with djgpp gives:

Abort !

As a secondary problem if i comment out the throw in f2() (or i change it
to be of MyExcept type) the program works ok showing me the messages i
expect. This makes me think that at least the first catch is correct, so
for what reason i don't get at least the message 'Error in f1()' in the
example above ?

Thanks in advance to anyone that will tell me what's wrong with that (or
with me :) )
Marco.

- Raw text -


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