Okay...this is a strange question that maybe no one has really asked before, but we will see how it goes.
I was thinking about exceptions in C++ recently, because here in university in my programming course the professor recently covered them.
If you have worked with exceptions in C++ before, then you know that we can do a "catch-all" kind of thing, in which case, if a certain exception was not caught, there is a way we can catch any type of exception that maybe we weren't expecting. This is done like so:
Well, at the same time, we also know that in C++ we can have functions with variable argument lists (this might seem completely unrelated, but bear with me and I will get to my point). With variable argument lists, we can have a function with a dynamic number of arguments. One example is printf, which takes however many arguments you really feel like passing it.Code:catch ( ... ) { cout << "Unknown exception caught." << endl; }
A function is written so:
Okay, now I come to my question. I noticed that these two different things use the same operator ( the ... operator ). That spawned some thought.Code:void myFunction ( ... ) { ..code contained here to grab the arguments and do stuff... }
It came to my mind, without actually reading any official text about it, that the catch might work in the same way that a function using a VAL (Variable Argument List) works. Since the exception being caught at that point in time is technically unknown and unhandled, it would make sense that it would be passed to catch in the same manner that some variable number of arguments is passed to a function.
So, my questions is: can the unknown exception that is passed to catch be retrieved and used just like variables in a VAL can be retrieved and used? You might say: why would this be useful? But I see some usefulness in it.
Let's say you weren't expecting a certain type of exception to be thrown, but it gets thrown anyways, and so your "catch all" catch segment of code catches the thrown exception. You could just print out an informative error message and quit the program, but what if you wanted to actually see what the exception was that was thrown? Could you access the exception just like you access a variable in a VAL?
What are yalls thoughts on this? Or if you know of any authoritative text on this subject, lead me to it!