The syntax for a throw is:
throw classname(args);This creates an instance of this class and effectively returns it from the function, even if that isn't the return type of the function. Usually the context of thrown errors is a try block. The code looks like:
try { // some code that generates errors } catch(thrown_type1 var) { // catcher code } catch(thrown_type2 var) { // other catcher code }
Only one catch clause is needed for each exception type, even if there are multiple throw statements in the try block. C++ supports the termination model. The assumption that the code throws an exception because things are so bad there is no help. If you want to construct the catchers so they try to fix the problem, put the try block inside a while loop.
To tell the user what the function is likely to throw as exceptions, there is an extention of the function definition syntax.
void foo() throw( toobig,badpointer);
function definition without a throw clause indicates that any exception can be thrown.
void foo() throw();
Means that no execptions will be thrown. Specifying the execeptions helps the user know what to do if they want to put a try loop around your code. generic catchers can be used.
catch(...) { }
This should be put at the end of the catcher list to pick up any you missed. If you can't figure out what to do, putting a call to throw with no arguments in a catcher will cause the exception to be re-raised to be picked up in the next outer try block. To make sure everything is clean, when you leave the scope of the code that threw the exception, all objects that have been succesfully constructed, are destructed.
Use them for non-local errors. If you can handle it, do it. Use heirarchies of exceptions to help classify the error. Use reference parameters in catchers. Wrap a simple try block with a generic catcher around code and refine it as needed. Good to throw exceptions during construction so execution doesn't continue with incomplete code. Don't use exceptions in destructors. Scope changes call destructors and you might get in a loop.
exception | ||
bad_alloc (thrown by new) | ||
bad_cast (thrown by dynamic_cast when fails with a referenced type) | ||
bad_exception (thrown when an exception doesn't match any catch) | ||
bad_typeid (thrown by typeid) | ||
logic_error | ||
domain_error | ||
invalid_argument | ||
length_error | ||
out_of_range | ||
runtime_error | ||
overflow_error | ||
range_error | ||
underflow_error | ||
ios_base::failure (thrown by ios::clear) |