2000

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                            FILE: MARCH.CPP */
/*                                                                            */
/*  Exceptional "Hello World"                                                 */
/*  =========================                                                 */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   18-MAR-2000   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream.h>
#include <stdlib.h>
#include <eh.h>

void print()
{
  cout << "Hello World" ;
  exit( 0 ) ;
}

class HelloWorld
{
 public:
   HelloWorld()
   {
     set_terminate( print ) ;
   }

   ~HelloWorld()
   {
     throw HelloWorld() ;
   }
} ;


int main( void )
{
  throw HelloWorld() ;
  return 0 ;
}

Update 11. March 2020

I had to make the obvious changes: replace iostream.h and add the using declaration.

es.h seems to be a Microsoft header, so I simply removed it.

After that, the program compiled and run. However the compiler warned that destructors are not supposed to throw any exceptions.

Because the main function throws a HelloWorld object, the throw in the destructor is not needed and I removed it from the class.

To further improve the code endl was added to the print function.

/******************************************************************************/
/*                                                                            */
/*                                                            FILE: MARCH.CPP */
/*                                                                            */
/*  Exceptional "Hello World"                                                 */
/*  =========================                                                 */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   18-MAR-2000   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/*  Compiled and tested with g++ V8.3.0                                       */
/*                                                                            */
/*  V2.00   11-MAR-2020   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>
#include <stdlib.h>

using namespace std ;

void print()
{
  cout << "Hello World" << endl ;
  exit( 0 ) ;
}

class HelloWorld
{
 public:
   HelloWorld()
   {
     set_terminate( print ) ;
   }
} ;


int main( void )
{
  throw HelloWorld() ;
  return 0 ;
}