Exception in C++
how to write code for exception handling in C++, what need to be take care, sorry i am not writing each every kind of code, just only some points.
Exception do and don't uses
try{
....
....
throw ex;
throw (new GFException());
}
catch(rutime_exception& ex) // always use by referance.
{
.........
throw; // rethorow excpetion, no change in type of original exception
}
catch(excpetion ex) // bad, twice copy, object slicing.
{
.........
throw ex; // rethrow, but static type changed , if handling dynamic type exception
}
catch(GFException* exGF) // bad who will take ownership of exGF
{
.................
}
exception specifications
void f1(); // might throw any exception.
void f1() thorw(int); // only throw int exception
void f1() thorw(); // it's not throw any exception.
Exception do and don't uses
1. use smart pointer to utilze destructor call and prevent resource leak when excpetion occured.
2. handle exception in constructor.
1. constructor use try{......}catch(...){cleanup; throw;}
2. use smart pointer this is best solution.
3.prevent throwing exception from destructor exception. if destructor called by stack unwinding, mean one exception allready active and now if destructor throw exception, C++ calls terminates.
terminate function : it terminates execution of your program. Furthermore, it terminates it immediately; not even local objects are destroyed
4. regardless of whether the exception is caught by value or by reference, throw make a copy of thrown object and passed to catch clause.C++ specifies that an object thrown as an exception is copied.Copying is always based on an object’s static type.
5. throw;
this statment rethrow the current exception, it's not change the type of the exception being propagated. efficient, not generate a new exception object.
6. catch(MyException ex){ throw ex;}
this change type of original exception when propagating. original exception can derived type of exception from MyException.
7. never throw a pointer to a local object, because local object destroyed before catch use object.
8. catch can do 2 kind of conversion for parameters.
1. derived to base.
2. typed to an untyped pointer. catch(const void*) can handle any pointer type.
9. catch are "first fit" but virutal function are "best fit" algorithems. never put a base class catch clause before derived class catch clause.
10. never return or throw local object as pointer. return object destroyed before used by pointer. Pointer aha, who will take ownership for deleting from heap.
11. catch expection by value problems, twice copy and object slicing.
12. best option of catch exception use by referance[no copy, no object slicing, no ownership], just remember this no value, no pointer in catch only referance.
13. if a function throws an exception not listed in its exception specification, this call runtime special function unexpected. unexpected->termincate->abort. what your program aborted!!!!!
14. Templates don't use exception specifications. we don't know what will be type of template, is that type follow our exception specifications.
---------This is Draft Version, still learning----------------
No comments:
Post a Comment
would you like it. :)