C++/ToString method of class exception
Expert: Ralph McArdell - 10/10/2004
QuestionHi,
I am writing a C++ program using MS Visual Stodio .Net v. 1.0.3075.
I visited the msdn.microsoft.com 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.
Regards,
Adrien
Answer(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.