Is there a way to determine the exception type even know you caught the exception with a catch all?
Example:
try
{
SomeBigFunction();
}
catch(...)
{
//Determine exception type here
}
Is there a way to determine the exception type even know you caught the exception with a catch all?
Example:
try
{
SomeBigFunction();
}
catch(...)
{
//Determine exception type here
}
If you're using Visual C++ (managed), you can use the GetType() method to get the type of exception and handle it from there.
E.g.
The string will contain something like "System.ArgumentOutOfRangeException".
Short Answer: No.
Long Answer:
If you derive all your exceptions from a common base type (say std::exception) and catch this explicitly then you can use this to get type information from your exception.
But you should be using the feature of catch to catch as specific type of exception and then working from there.
The only real use for catch(...) is:
Edited: You can extract type information via dynamic_cast<>() or via typid() Though as stated above this is not somthing I recomend. Use the case statements.
I've tried various ways; this works for me:
Begin by subclassing runtime_error :
Then you may create some instances of your exceptions.
Explicitly notify the compiler that your function may throw an exception or the program will probably terminate at the point thrown, and data could be lost or corrupted if resources are in use at the time.
"Make sure you can and do catch anything that you can throw."
(I use the generic runtime_error because throwing and catching it covers all of my exceptions plus the systems' ones as well.)
then in your try/catch
The runtime-error class has good support in the c++ standard libraries, and compilers know about it internally, and how to optimize memory and dispatch, so you can use them over different code bases safely and confidently. The code is portable and compatible with many different compilers and architectures.
It may be preferable and somewhat faster to catch each error separately in a catch clause, from more specific to more generic,if you feel a series of string matches is a terrible waste of cpu and memory (the compiler optimizes these though ).
<stdexcept>
gives you several kinds of exceptions in 2 groups:Logic errors:
Runtime errors:
usage syntax is slightly different for some of them.
Conventional Wisdom in C++ says that your exceptions should be relatively "flat", meaning that large hierarchies of specific categories of exceptions should be eschewed in favor of short generic but informative ones for general programming tasks. Domain specific tasks like network system logic, higher maths, etc. may benefit from specificity, but that can be achieved handily by making intelligent error strings with generic runtime/logic exceptions.
Lastly, My Point is: You can achieve all of this by throwing and catching only runtime_error.
You don't have to create a whole trick-bag of highly specific exceptions (like java does) for each class, each handling one specific error.