Traps and Leaves

| 4 Comments | No TrackBacks

Before explaining what's inside CommonFramework.h, I will explain three things: Traps and Leaves, Class Types, and the Clean up stack. This posting will only explain the Trap and Leave mechanism.

Symbian doesn't use the try ..catch which is a C++ standard because of the large overhead, instead it uses its own mechanism which is Trap and Leave.


A function usually leaves if an error occurs (similar to throwing exception), and the error code can be catched using a trap (similar to the except part).

You leave a from a function using User::Leave with the error number as its parameter, like this:


int InsertL()
{
//... other codes ...
if (!myObject) User::Leave(KErrNoMemory);
//... more codes...
}

When User::Leave is called, it will return (or you migh say jump) to the instruction after the one that traps the leave.

By convention, the name of the function that leaves ends with an L. This will make it easier for you decide whether to trap a leave or not.

Trapping a leave is simple:


TInt errcode;
TRAP(errcode, InsertL());

or


TRAPD(errcode, InsertL());

The TRAPD macro saves you a line by defining the error code variable, it also means that you can not use TRAPD more than once in a code block using same variable (errcode will be redefined), use TRAP() for next trap code if you use the same variable.


TRAPD(err, TestLeaveL(0));
TRAPD(err, TestLeaveL(1)); //error, redefinition of err
TRAPD(err2, TestLeaveL(1)); //OK
TRAP(err, TestLeaveL(1)); //OK

The TRAP macro will trap any statement on the second parameter that leaves. If the function leaves, the error code will be filled with a value, if the function does not leave (returning without error), the error code would be KErrNone a constant with a value of 0 (zero).

If your function name ends with an L (for example when you are implementing an interface), and you call a function with the name also ends with an L, you don't have to trap the leave:


void MyLeaveL()
{
//.. code ..
//if TestL() leaves, it will also leaves from this function
//with the error code from the TestL()
TestL();
//..code..
}

But if your function name doesn't end with an L, you must trap the leave:


void TestFunction()
{
//..code..
TRAPD(err, TestL());
//..code..
}


This following code might give you a better understanding of how traps and leaves work (this is a modification from the console based hello world):


#include "CommonFramework.h"
//test function that leaves
void TestLeaveL(int leave)
{
console->Printf(_L("Test Leave\n"));
if (leave) {
console->Printf(_L("Leaving\n"));
User::Leave(1);
}
console->Printf(_L("Not Leaving\n"));
}
//calling a function that leaves
void CallingLeaveL()
{
TestLeaveL(1);
//the following code will never be executed
//because the above line will leave
console->Printf(_L("Not Printed\n"));
}
// do the example
LOCAL_C void doExampleL()
{
TRAPD(err, TestLeaveL(0));
console->Printf(_L("Leave code is: %d\n"), err);
TRAP(err, TestLeaveL(1));
console->Printf(_L("Leave code is: %d\n"), err);
TRAP(err, CallingLeaveL());
console->Printf(_L("Leave code is: %d\n"), err);
}

The output will be:


Test Leave
Not Leaving
Leave code is: 0
Test Leave
Leaving
Leave code is: 1
Test Leave
Leaving
Leave code is: 1
ok [press any key]

I'll talk more about traps an leaves when discussing about the C-type Class.

No TrackBacks

TrackBack URL: http://symbian.compactbyte.com/mt/mt-tb.cgi/5

4 Comments

Hi all!

As newly registered user i just wanted to say hi to everyone else who uses this bbs ;-)

Aloha forum!

I'm a newbie here.

So i'd like to ask you if someone of you or your frineds was fired because of a financial crisis?

adultism www blacksfilmed com big black ass swot analysis oracle corporation

not clear ... confused in example

Leave a comment

About this Entry

This page contains a single entry by Yohanes Nugroho published on June 3, 2004 1:41 PM.

Hello World explained was the previous entry in this blog.

Symbian GCC Improvement Project is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.