Re: Handling Crashes in C++ program internally
thanks all :)
i have started implementing RAII in my code .
can someone share some good link or document on using auto_ptr ?
"Ben Voigt [C++ MVP]" wrote:
Now looking at the nested function calls i have adding try catch is
mandatory at all level so that i can free memory and resources in
respective
function.
Do you still think there is better alternative?
RAII and smart pointers.
Absolutely. Let's take a look at that error handling code again, shall we?
void func2()
{
// open some file and allocate some memory
FILE* file = fopen("dummy.txt", "w+");
char* mem = new char[10];
now do somthing which will cause WIN32 Exception
try
{
char* ch = NULL;
strcpy(ch, "msdn");
}
catch(SE_Exception)
{
printf("\n translated WIN32 exception caught here");
fclose(file);
delete[]mem ;
}
fclose(file);
delete[]mem ;
}
Can anyone say "closed invalid FILE* and double freed mem"?
try/catch is not for cleanup. Destructors are for cleanup. Learn RAII.