You are here:

C++/ToString method of class exception



I am writing a C++ program using MS Visual Stodio .Net v. 1.0.3075.

I visited the and there it mentions that the class exception has a public method called toString which is described as "Overriden. Creates and returns a string representation of the current exception."

But, in my code, after including <exception> and after catching an exception, e, when I try something like:

cout << e.toString();

it gives me an error saying that method toString is not a member of exception.

What am I missing here?

Thank you for your help.


(At the time of writing - Monday 11th October 2004 - I do not know when I can post this answer to you as the AllExperts site has been returning a “Server Too Busy” error to me for the last three days. I have now posted an email to their support but I am not holding my breath!)

I think you mean Visual Studio not Stodio <grin>.

I also think you have confused standard C++ with the .NET framework and C++ usage with C# usage. Please note that I am not a huge expert on .NET, C# or the .NET managed C++ extensions.

However I do think you need to differentiate between what is standard C++ and what are the Microsoft only parts. If you are writing standard C++ then you are best off using standard C++ references such as The C++ Programming Language 3rd edition by Bjarne Stroustrup, The Standard C++ Library A Tutorial and Reference by Nicolai M. Josuttis and other texts such as Effective C++, More Effective C++ and Effective STL by Scott Meyers and even the C++ standard itself as a starting points and then noting specifics for the compiler in question (for example VC++ for .NET 2002 - which I think is the  version you are using - aka VC 7.0 - has no support for partial template specialisation as this came with the next release).

If you do use the MSDN library as you primary source of standard C++ documentation then you should check the exact location within the article tree of the MSDN library documentation you read: all the standard C++ stuff will be in a couple of places while the .NET and MS only libraries will be some place else. The exception is the MS C/C++ runtime library which has both standard and non-standard elements. However the documentation for each function usually has an indication as to compatibility (e.g. ANSI, Win 98 ...). ANSI implies standard here.

Now, if I have interpreted what you are doing correctly, for .NET the member function is called ToString not toString. Second the class is called Exception not exception.  This is the .NET exception class and resides in the (.NET) namespace System. This has little or nothing to do with standard C++. You have to use the managed extensions to C++ for it to work (this includes setting the build options correctly as far as I am aware, but there are also several Visual Studio .NET-and-C++ projects types). For example you need to start off by using a dll (according to the example in the MS documentation):

       #using <mscorlib.dll>

       using namespace System;

The using directive is optional but saves typing System:: to qualify names from this namespace, but may have other problems if two names exist in different namespaces that are in scope at the same time. You have to use the likes of:

       public __gc class MyClass {};

to define a class with which .NET can work with.

All of which contains a lot of MS specific gunk some of which is likely to change with the next version (2005) of VC++.NET.

For standard C++ you can use the class std::exception (that is the class exception in the std namespace) if you wish - or one of your own devising. To use std::exception as a base exception class you need to include the standard C++ header <exception>. It has no ToString member function, but rather has a what member function which is meant to be overridden by particular exception classes to return a meaningful text string as a pointer to a C-style zero-terminated array of (const) char - i.e. const char *. To use other pre-defined standard exceptions you can include the header <stdexcept>, or <new> for std::bad_alloc or <typeinfo> for std::bad_cast. The class std::ios::failure is defined in <ios>.

Next let us move onto C# and C++ exception catching usage. In C# all objects such as exceptions are heap objects which are handled (and therefore caught correctly) by reference by saying:

       catch (Exception e)

In C++ however saying:

       catch (std::exception e)

catches the exception by value - that is a copy is made of the exception and at this point you slice the actual exception back to its base type - i.e. you end up with a real base exception class instance and not a reference-through-base-class-reference to your derived exception class instance (note: you can do the same thing (slicing an object) in other ways most notably by passing objects by value as a base type to functions). In C++ the usual semantics are to explicitly catch by reference:

       catch (std::exception & e)

However the MSDN library C++ example for the .NET System::Exception.ToString member function implies you should catch .NET Exceptions by pointer and not by reference:

       catch (Exception * e)

See the MSDN library article accessed by the document path .NET Development / .NET Framework SDK / .NET Framework / Reference / Class Library / System / Exception Class / Methods / ToString. I used the CD-ROM edition from July this year so maybe the online version's path to the article is a little different - I notice this happens from time-to-time.

Note in that last example the lack of namespace qualification (no System:: prefix to Exception). This is because if we follow on from the previous managed C++ example code, then the line:

       using namespace System;

has already brought all known names from the .NET system namespace into scope.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.