www.digitalmars.com         C & C++   DMDScript  

c++ - How to catch runtime errors of the C++ standard library with DigitalMars

reply Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

How to catch runtime errors with DigitalMars C++ ?
There is no
#include <stdexcept>
and nothing similar, but just
#include <eh.h>

The problem is
"Error: undefined identifier 'runtime_error' catch( runtime_error e)"

So the exceptions usually defined in <stdexcept> are not defined for 
this compiler at all ?!

Is this a lack of feature ? Any workaround to work with standard runtime 
exceptions ?

See this german example,
if you allocate "1000" MBytes, the runtime error should be catched, by 
standard exceptions.

Of course I MIGHT throw a user-defined exception.

But if there are no system exceptions with DMC, means that I cannot use 
portable code for other compilers which depend on this feature ! So I 
must know for my own development.

Btw, some other C++ exception features like

set_terminate()
and
set_unexpected()

are just defined in
#include <eh.h>
, not in
#include <exception>
:-(
Any reason for that ?

Sincerely
Rolf

// 6. Beispiel Fehlerbehandlung / Exceptionhandling
// - vorgegebene Exceptions aus der C++-Standardbibliothek
//   definiert in <stdexept>
// Klassenhierarchie:
//  exception <--- bad_cast
//                 bad_alloc
//                 bad_exception
//                 bad_typeid     <--- __non_rtti_object
//                 logic_error    <--- domain_error
//                                     invalid_argument
//                                     length_error
//                                     out_of_range
//                 runtime_error <---- overflow_error
//                                     range_error
//                                     underflow_error

#include <iostream>
#include <stdexcept>
using namespace std;

int main()
{
    char *buffer;
    int mb;

     // Zu überwachender Block
     try
     {
        cout << "Wieviel MB soll reserviert werden? ";
        cin >> mb;
        buffer = new char[1024*1024*mb];
        if (buffer==NULL)
           throw runtime_error("Geht nicht");
        cout << "Programm läuft weiter ..." << endl;
     }

     // Fehlerbehandlungsroutinen
     catch(runtimeerror e)
     {
        cout << "Exception: " << e.what() << endl;
        return 1;
     }
     cout << "Programm fehlerfrei beendet." << endl;
     return 0;
}
-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 21 2004
next sibling parent reply Scott Michel <scottm cs.ucla.edu> writes:
Most of DMC++'s "Standard C++ compliance" comes from the STLport package. If
you add "-I\dm\stlport\stlport" to your compile command line, your problems
should be solved.

Note: change "\dm" to the top directory where you installed DMC.


-scooter

Rolf Hemmerling wrote:

 Hello !
 
 How to catch runtime errors with DigitalMars C++ ?
 There is no
 #include <stdexcept>
 and nothing similar, but just
 #include <eh.h>
 
 The problem is
 "Error: undefined identifier 'runtime_error' catch( runtime_error e)"
 
 So the exceptions usually defined in <stdexcept> are not defined for
 this compiler at all ?!
 
 Is this a lack of feature ? Any workaround to work with standard runtime
 exceptions ?
 
 See this german example,
 if you allocate "1000" MBytes, the runtime error should be catched, by
 standard exceptions.
 
 Of course I MIGHT throw a user-defined exception.
 
 But if there are no system exceptions with DMC, means that I cannot use
 portable code for other compilers which depend on this feature ! So I
 must know for my own development.
 
 Btw, some other C++ exception features like
 
 set_terminate()
 and
 set_unexpected()
 
 are just defined in
 #include <eh.h>
 , not in
 #include <exception>
 :-(
 Any reason for that ?
 
 Sincerely
 Rolf
 
 // 6. Beispiel Fehlerbehandlung / Exceptionhandling
 // - vorgegebene Exceptions aus der C++-Standardbibliothek
 //   definiert in <stdexept>
 // Klassenhierarchie:
 //  exception <--- bad_cast
 //                 bad_alloc
 //                 bad_exception
 //                 bad_typeid     <--- __non_rtti_object
 //                 logic_error    <--- domain_error
 //                                     invalid_argument
 //                                     length_error
 //                                     out_of_range
 //                 runtime_error <---- overflow_error
 //                                     range_error
 //                                     underflow_error
 
 #include <iostream>
 #include <stdexcept>
 using namespace std;
 
 int main()
 {
     char *buffer;
     int mb;
 
      // Zu überwachender Block
      try
      {
         cout << "Wieviel MB soll reserviert werden? ";
         cin >> mb;
         buffer = new char[1024*1024*mb];
         if (buffer==NULL)
            throw runtime_error("Geht nicht");
         cout << "Programm läuft weiter ..." << endl;
      }
 
      // Fehlerbehandlungsroutinen
      catch(runtimeerror e)
      {
         cout << "Exception: " << e.what() << endl;
         return 1;
      }
      cout << "Programm fehlerfrei beendet." << endl;
      return 0;
 }
Jan 21 2004
parent reply Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

This missing feature is a feature of the standard library!
So the STL **Template** library does not help.

I just found out that other "old" compilers lack of the feature, too., 
like "Microsoft VC++ 4.0" ( which has basic support for exceptions, in 
opposite yet older 16-bit versions ).

In opposite, OpenWatcom and BCC 5.5 support the feature.

Any other suggestions ?

Sincerely
Rolf

-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 21 2004
next sibling parent Jan Knepper <jan smartsoft.us> writes:
Rolf Hemmerling wrote:
 Hello !
 
 This missing feature is a feature of the standard library!
 So the STL **Template** library does not help.
I think STLport is quite a bit more than just STL.. www.stlport.org -- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Jan 21 2004
prev sibling parent reply Scott Michel <scottm cs.ucla.edu> writes:
Rolf Hemmerling wrote:

 Hello !
 
 This missing feature is a feature of the standard library!
 So the STL **Template** library does not help.
The STLport is a ***LOT*** more than just STL templates. The STLport library has a lot of "extra" headers that help the compiler environment conform to standard C++. The current DMC++ design relies on STLport to provide this additional layer of conformity, rather than reinvent the compiler's header files to achieve conformity. I could be wrong, but that's my impression based on compiling my code that relies on standard C++ iostream. -scooter
Jan 21 2004
parent reply Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

I found out

"set_terminate()"
is mentioned in the file

dm/stlport/stlport/exception

and "runtime_error"

is mentioned in the file

(installpath)dm/stlport/stlport/stdexecpt

but if I try to compile with "(installpath)dm\stlport\stlport",
I still get the error messages that the compiler does not find 
"set_terminate()" and "runtime_error" !

Any suggestions ?

Sincerely
Rolf

-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 22 2004
parent reply Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

I wanted to say, that including

#include <stdexcept>
#include <exception>
did not help, although these are "the" exception header files for 
STLport !! :-(.

Sincerely
Rolf

-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 22 2004
parent reply Rolf Hemmerling <hemmerling gmx.net> writes:
MOre clearly:

I used the example from the tread start and added
#include <stdexcept>
#include <exception>

in the file,
while I put the directory of STLPORT at the first position of the 
include path string, and the compiler even did not find

set_terminate()
runtime_error

:-(

So what's wrong with me ?

Must I use special namespaces for use with STL ??

Please help, if you can!
I am a newbie to C++, btw ( but with some C and some Java experience ).

Sincerely
Rolf
-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 22 2004
parent reply Jan Knepper <jan smartsoft.us> writes:
Can you post the message? I wonder whether it is the compiler or the 
linker that can not find the stuff you mention.



Rolf Hemmerling wrote:
 MOre clearly:
 
 I used the example from the tread start and added
 #include <stdexcept>
 #include <exception>
 
 in the file,
 while I put the directory of STLPORT at the first position of the 
 include path string, and the compiler even did not find
 
 set_terminate()
 runtime_error
 
 :-(
 
 So what's wrong with me ?
 
 Must I use special namespaces for use with STL ??
 
 Please help, if you can!
 I am a newbie to C++, btw ( but with some C and some Java experience ).
 
 Sincerely
 Rolf
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Jan 22 2004
parent reply Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

At first, my starting-C++ project is a multi-compiler project.
So with the "same" very-complicated makefile and a common header file,

I succeeded with OTHER compilers to include those header files which 
contain the definitions for "set_terminate()" and "runtime_errror".

For example, with OpenWatcom, this

*****
class _WPRTLINK runtime_error : public exception {
public:
     runtime_error( string const& what_arg ) _WCTHROWS(())
         : exception( what_arg ) {
     }
};
****

defines the "runtime_error" in the proper header files,
which I did not find with MingW, MS VC++4.0 and DMC !

So if I include
#include <stdexcept>
#include <exception>
, these files ARE found by the compiler, nethertheless they don´t 
contain the proper information to support "set_terminate()" and 
"runtime_error" ( as I said, this is **standard library stuff", as the 
actions of the standard libarary operations are controlled by try..catch 
with "runtime_error" ect.

here are the error messages:

*********************
     set_terminate( exitus );
                             ^
..\..\src\data\exception\Exception.cpp(153) : Error: undefined 
identifier 'set_terminate'
     set_unexpected( wasIstLos );
                                 ^
..\..\src\data\exception\Exception.cpp(217) : Error: undefined 
identifier 'set_unexpected'
           throw runtime_error ("Geht nicht");
*********************

So if I setup the right include path, the include files are found, but 
are not sufficent.

So I think that the STLlib does NOT help with "set_terminate()" and 
"runtime_error" :-(.

Support for "set_terminate()" is by
#include <eh.h>
of the standard non-STL include files,
, but there is no support for "runtime_error" ? :-(.

Any suggestions ?

Sincerely
Rolf

-- 
       /   /    / Alone on the data highway...
     /        /   like on an allee in Hannover-Herrenhausen
   /    /   /     The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/    /   /       http://www.hemmerling.com/
Jan 22 2004
next sibling parent reply Jan Knepper <jan smartsoft.us> writes:
Rolf Hemmerling wrote:
     set_terminate( exitus );
                             ^
 ...\..\src\data\exception\Exception.cpp(153) : Error: undefined 
 identifier 'set_terminate'
     set_unexpected( wasIstLos );
                                 ^
 ...\..\src\data\exception\Exception.cpp(217) : Error: undefined 
 identifier 'set_unexpected'
Both are defined in eh.h as it seems... I just hope they exist in the library. So, indeed for these you would have to include eh.h
           throw runtime_error ("Geht nicht");
 *********************
runtime_error is not defined anywhere as far as I can tell. Including stlport indeed probably will not help you a lot. For the time being you could implement a class runtime_error yourself...
 So if I setup the right include path, the include files are found, but 
 are not sufficent.
 
 So I think that the STLlib does NOT help with "set_terminate()" and 
 "runtime_error" :-(.
 
 Support for "set_terminate()" is by
 #include <eh.h>
 of the standard non-STL include files,
 , but there is no support for "runtime_error" ? :-(.
 
 Any suggestions ?
 
 Sincerely
 Rolf
 
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Jan 22 2004
parent Rolf Hemmerling <hemmerling gmx.net> writes:
Hello !

Jan Knepper wrote:

           throw runtime_error ("Geht nicht");
 *********************
runtime_error is not defined anywhere as far as I can tell. Including stlport indeed probably will not help you a lot. For the time being you could implement a class runtime_error yourself...
So you see ALL the standard exceptions exception <--- bad_cast // bad_alloc // bad_exception // bad_typeid <--- __non_rtti_object // logic_error <--- domain_error // invalid_argument // length_error // out_of_range // runtime_error <---- overflow_error // range_error // underflow_error are all not available with oldfashioned MS VC++ 4.0 and with DMC, and this makes ( the standard library of ) DMC "oldfashioned". And so even though I am just a beginner with C++, does not give arguments to support DMC with my projects. Or, vice-versa, I must avoid "standard" c++ features with my project, to support DMC. As self-written exceptions are supported, probably its a lack of the system library of the compiler ? Any other suggestions ? Sincerely Rolf -- / / / Alone on the data highway... / / like on an allee in Hannover-Herrenhausen / / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germany / / / http://www.hemmerling.com/
Jan 23 2004
prev sibling parent Scott Michel <scottm cs.ucla.edu> writes:
Rolf Hemmerling wrote:

 Hello !
 
 At first, my starting-C++ project is a multi-compiler project.
 So with the "same" very-complicated makefile and a common header file,
 
 I succeeded with OTHER compilers to include those header files which
 contain the definitions for "set_terminate()" and "runtime_errror".
 
 For example, with OpenWatcom, this
 
 *****
 class _WPRTLINK runtime_error : public exception {
 public:
      runtime_error( string const& what_arg ) _WCTHROWS(())
          : exception( what_arg ) {
      }
 };
 ****
 
 defines the "runtime_error" in the proper header files,
 which I did not find with MingW, MS VC++4.0 and DMC !
 
 So if I include
 #include <stdexcept>
 #include <exception>
 , these files ARE found by the compiler, nethertheless they don´t
 contain the proper information to support "set_terminate()" and
 "runtime_error" ( as I said, this is **standard library stuff", as the
 actions of the standard libarary operations are controlled by try..catch
 with "runtime_error" ect.
 
 here are the error messages:
 
 *********************
      set_terminate( exitus );
                              ^
 ..\..\src\data\exception\Exception.cpp(153) : Error: undefined
 identifier 'set_terminate'
      set_unexpected( wasIstLos );
                                  ^
 ..\..\src\data\exception\Exception.cpp(217) : Error: undefined
 identifier 'set_unexpected'
            throw runtime_error ("Geht nicht");
 *********************
 
 So if I setup the right include path, the include files are found, but
 are not sufficent.
 
 So I think that the STLlib does NOT help with "set_terminate()" and
 "runtime_error" :-(.
1. Make sure your IDDE include path is set as follows: \dm\stlport\stlport;\dm\include;\dm\include\win32 or the includes to the command line version look like: -I\dm\stlport\stlport -I\dm\include -I\dm\include\win32 2. It's "std::set_terminate", not "set_terminate". Likewise, it's "std::set_unexpected". 3. There does seem to be a bug in the DMC runtime library, because set_unexpected and std::set_unexpected are both undefined. Work around this problem as follows: #if !defined(__SC__) && !defined(__DMC__) set_unexpected(wasIstLos); #endif Welcome to the wonderful world of porting code! -scooter
Jan 22 2004
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Rolf Hemmerling" <hemmerling gmx.net> wrote in message
news:bulkfe$501$1 digitaldaemon.com...
 Hello !

 How to catch runtime errors with DigitalMars C++ ?
What's happening is the global new operator is not throwing an exception when it fails to allocate the memory, it returns NULL instead. This is a bug in the runtime library.
Jan 26 2004