So, I'm trying to write a simple Exception base class for C ++, based on the Java Exception class. I am sure that there are already excellent libraries there, but I do it for practice, and not for production code, and I am curious and always want to learn. One of the things that Java Exception does that I would like to implement is the concept of "cause." In Java, a new exception with a reason is as follows:
Exception cause = new Exception(); Exception newExcept = new Exception(cause);
However, in C ++, passing exceptions as an argument to the constructor is a call to the copy constructor. So, there is a conceptual disconnect between copying an Exception and creating a new Exception with a reason. Obviously, this is not a problem in Java.
I think I'm just wondering what is the best way to handle this. I had several ideas:
- Differentiate using a dummy variable
- Just create a new exception and call the setCause () method
- Something like an
Exception(Exception &) copy constructor Exception(Exception &) , and a constructor with a reason is Exception(Exception *)
thanks
source share