Question: I've a few Classes from a assembly which is .Net 2.0. These Classes are marked with Serializable.

In my project, I use these classes in my Classes, which are marked with DataContract(IsReference=true) and DataMember.

Now I have the problem, with DataContractSerialiser that it serializes the private fields of my .Net 2.0 Classes, which will not work. But when I use XMLSerialiser, I cannot use IsReference, and so I can also not do this.

Is there a easy (simple) Solution for this? Maybe a someone has a own XMLSerializer which supports references?

Here is a bit of my code:

```
[DataContract(IsReference = true)]
public class ConnectionConfig: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
            PropertyChanged(this, new PropertyChangedEventArgs("ObjectAsString"));
        }
    }

    private PLCConnectionConfiguration _configuration;
    [DataMember]
    public PLCConnectionConfiguration Configuration
    {
        get { return _configuration; }
        set { _configuration = value; NotifyPropertyChanged("Configuration"); }
    }
}

```

where PLCConnectionConfiguration comes from a .NET 2.0 assembly, and is decorated with [Serializeable]

Answer: 
You have a far easier solution:

```
struct MyExcept : public std::exception {
   std::string errstr;
   const char* what() const throw() {return errstr_.c_str();}
   MyExcept(int errno, std::string prefix = "") : errstr (prefix + LibErrString(errno)) {}
};
...
throw MyExcept(err, "In Foo::bar... ");

```

With C strings, you have to worry a lot more about scopes and manual memory managament. You correctly noticed that you exit a few scopes (pop variables and functions off the stack), so C strings are even worse there. C++ strings on the other hand behave much more like integers. Memory management is an integrated feature.

For the same reason, I've moved the `LibErrString` call inside your exception class. Error handling code fits naturally in exception classes, and should not clutter business code.

---

So much for practical code, back to theory. What happens when an exception is thrown and caught? C++ first determines *where* the exception will be caught. There must be an enclosing `try{ }` scope, but possibly there are a lot more scopes in between: function scopes, for-scopes, if-scopes, or just plain block scopes. 

These scopes are then exited from the inside out. As each scope is exited, the variables local to that block are destroyed. When a function scope is exited, the next scope to consider is of course the caller; for the other scopes the next scope is the surrounding scope.

You see that variables are destroyed, and functions are exited in a LIFO order. That means that a stack structure is a natural fit. You could also use two stacks, keeping return addresses and variables separate. Or three, to avoid big buffers on the stack. Since there are many reasonable implementations, C++ doesn't actually describe a precise implemenation, just the behavior.
First, your exception class should look like this

```
class MyExcept : public std::runtime_error {
    MyExcept(const std::string & message) : std::runtime_error(message) {}
};

```

That is really all that is needed. You can change the base-class to `std::logic_error` if that suits your needs better (see [here](https://stackoverflow.com/questions/2924058/confused-about-stdruntime-error-vs-stdlogic-error) for an explanation of the distinction, and [here](http://www.cplusplus.com/reference/std/stdexcept/) for a list of other predefined exception classes). You can then build the error message using string concatenation:

```
void Foo::bar() {
    int err = SomeAPIRoutine(...);
    if (err != SUCCESS) {
       throw MyExcept("Error: " + LibErrString(err));
   }
   // otherwise...
}

```

[This FAQ](http://www.parashift.com/c++-faq-lite/exceptions.html) contains more useful information about exception handling, especially how to correctly catch exceptions:

```
try {
    // fail here
}
catch (std::exception & e) { // NOTE: catch by reference!!!
}

```

As to your stack question, the current stackframe is unwound, and all objects in it are destroyed in reverse order of their construction. This is done until the exception is caught. This is the reason why [RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) works.