When working with C# or Java, I used to create exception classes that included other exceptions as class members. For instance:
public class MyException : Exception {
private MyException ex;
private String message;
public String what() {
return this.message;
}
public String stack() {
if(ex != null) {
StringBuilder s;
s.append(this.what());
s.append(this.ex.stack());
return s.toString();
}
return this.what();
}
}
I was looking for examples on the same subject but for C++ and I couldn't find any (maybe I didn't search for the right terms, as you can see the title of this question is not very fancy).
Anyway, what is the correct way of doing that in C++? Is it to store the inner exception as a pointer or a reference? (I think I might need a pointer so it can be null when it's the first exception). When I raise the exception, should it be as a pointer created with new?
EDIT: Maybe what I wrote was a little confusing or not a well-known (-accepted) programming practice. So I'll specify how I intended to use this class with a snippet:
try {
// Code that throws an exception of type MyException
} catch(MyException ex) {
MyException another = new MyException();
another.setEx(ex);
another.setMessage("A message that explains where the other exception was caught and on what conditions");
throw another;
}