In C ++, you can declare a function with an exception specification as follows:
int foo() const throw(Exception);
I found these two links:
But some things ultimately go unanswered ...
Question 1: why add an exception specification? Will this lead to increased productivity? What will be different for the compiler? Because it is like information for a programmer.
Question 2: what will happen (what should happen) if I drop something that is not specified in the specification? For instance:
int foo() throw(int) { throw char;
Question 3: function / method should not throw anything. I found at least two (three, alternative syntax for different compilers) ways to specify an exception exception:
int foo() throw();int foo() __attribute(nothrow)__ for gccint foo() nothrow for visual c ++
Which one is "correct"? Is there any difference? Which one should I use?
Question 4: "standard exceptions", bad_alloc , bad_cast , bad_exception , bad_typeid and ios_base::failure .
Ok bad_alloc explains itself, and I know how (and more importantly, when) to use it (add exceptions to the specification), but what about others? None of them really ring the bell ... What "code fragments" are they associated with? How bad_alloc is related to new char[500000] .
Question 5: If I have a hierarchy of exception classes, for example:
class ExceptionFileType { virtual const char * getError() const = 0; }; class ExceptionFileTypeMissing : public ExceptionFileType { virtual const char *getError() cosnt { return "Missing file"; } }
Should I use:
int foo() throw(ExceptionFileType);
Or:
int foo() throw(ExceptionFileTypeMissing,ExceptionFileTypeNotWritable,ExceptionFileTypeNotReadable,...)
Note: answers with links will be great. I am looking for good practice advice.