About C++ exception and call stack
- An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime
- In C++, a variable or class object that represents an exceptional event.
- If without handling, Program crashes, Falls into unknown state
- An exception handler is a section of program code that is designed to execute when a particular exception occurs
- Resolve the exception, Lead to known state, such as exiting the program

If no exception occur then most right hand path is followed.

If an exception occurs in Func4() then it directly goes to catch block. If no exception occur then next statement after Func4() is executed.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <exception> // for standard library base class exception.
using namespace std;
#define KErrNoMemory -5
#define KErrNotFound -1
// Throw an int or string
int main()
{
int *iMyData = NULL;
try
{
iMyData = new int;
if( iMyData == NULL )
{
throw KErrNoMemory;
//throw KErrNoMemory should be here
}
}
catch( char * str ) // catch string type
{
cout << "Exception raised: " << str << '\n';
}
catch(int i)
{
cout << "Exception raised: " << i << '\n';
}
getchar();
return 0;
}
>> If we run this program in PC then probably we don't get exception. Normally inside a try block we try to do something. If the desired action can't be performed for some reasons then we execute throw statement. And in the catch block we catch it. If nothing were thrown then we don't catch anything. In the above example if we put this statement iMyData = NULL; after iMyData = new int; then we see that following statements are executed.
catch(int i)
{
cout << "Exception raised: " << i << '\n';
}
>
Stack rewind
When we have nested function call then if some exception occurs in the in one of the nested function then rest statements in the chain are not executed. Lets see the following example.
Exception occurs in function3() and 2nd cout will not be executed, in function2() 2nd cout will not be executed and so on. void function3( )
{
cout << "Throwing exception in function3\n";
throw runtime_error( "runtime_error in function3" );
cout << "Nothing will print";
}
void function2( )
{
cout << "Calling function3 from function2\n";
function3( );
cout << "Nor here";
}
void function1( )
{
cout << "Calling function2 from function1\n";
function2( );
cout << "Nor here, either";
}
int main( )
{
try
{
cout << "Calling function1 from main\n";
function1( );
cout << "Not even here";
}
catch ( runtime_error e )
{
cout << "Exception: " << e.what() << endl;
}
cout << "Execution continues in main" << endl << endl;
system("pause");
return 0;
}
>>>>>>>>>>
Stack variable and heap variables
Though this does not belongs to this category, we can introduce it here. Stack variables and heap variables are defined where they are located in memory. Stack variable is related your function (method). When we declare a variable inside a function then OS allocates some space (from stack) for that variable.
See this example
void CMyClass::MytestMethod()
{
TInt i = 0;
// Compiler allocates a space for i and initialize it to 0, that space comes from Stack (reserved memory for each function)
//allocated space will be cleaned up when we return from this function to caller.
TInt *pi = new TInt;
// We allocate a space by compiler for pointer pi that space is coming from heap (free memory in the system)
// allocated space will not be cleaned up if we don't use (directly/indirectly) the delete operator.
*pi = 0;
detele pi;
}
If we declare a variable on Stack that is Stack variable (for example i in above example, in this case you don't allocate memory but compiler does). If you allocate memory (directly/indirectly) for a pointer that we should say heap object (some people might say it differently)
By just watching how it has been created then we can tell what kind of object it is. By default Symbian OS has only 8 K stack size. As you can see if you declare several variables (Like TBuf <1000> buf(0);) you stack will be overflow. You can increase the stack size as well by MMP file.
Benefits of stack and heap can be seen from the MytestMethod(). By stack variable and heap variable we are doing same thing but for stack it is straight forward as compared to heap variables. But this is not always the case. If we are talking about big object it is always allocated on heap, the reason is memory.
