Exception Specifications

closed account (NqG36Up4)
Hi,
I'm pretty new to C++ and to cpp as well. While studying exceptions , I ran into the exception specifications section, and to be honest I can't see a reason to use those, due to the fact that they are checked at runtime and not by the complier ( I see no reason why not by the complier?!) I could just at documntaion to my code and it will have the same effect.

and another question, is there a way to hanlde two exceptions that occuers the same time?

Thx
Last edited on
Let's take new for an example:

What happens if you can't allocate the memory? What do you do? Return NULL? Then they could get a runtime error if they don't check it. (if you throw an exception they can handle ANY error with catch (...)) Return as much memory as possible? What if it is an object, and they access stuff that isn't theirs because you couldn't get that memory?
closed account (NqG36Up4)
I meant using "exception specifications" and not exceptions in general :), for example:

int f() throw(int, bad_alloc); // may throw any of them (int / bad_alloc) or derived thereof
double g() throw(); // will throw none
void h(); // may throw anything

Exception specifications provide no benefit to your program, but they come with a maintainability cost. Simply put, do not use them.

The only way two exceptions can occur at literally the same time is if you have a multi-core machine running a multi-threaded app and two threads throw. In which case from your standpoint it is like two independent programs running since there are independent stacks.

If what you are asking is "what happens if my catch block throws?" then the answer is that the stack is further unwound to handle the second exception. (Note that the "current" try-catch block will not catch the second one.)
Topic archived. No new replies allowed.