Symbian
Symbian OS Library

[Index][spacer]



 

Classification: C++ Category: Base
Created: 01/07/99 Modified: 07/22/2003
Number: FAQ-0185
Platform: Not Applicable

Question:
The definitions of TRAP and TRAPD are:
#define TRAP(_r,_s) {TTrap __t;if (__t.Trap(_r)==0){_s;TTrap::UnTrap();}}
#define TRAPD(_r,_s) TInt _r;{TTrap __t;if (__t.Trap(_r)==0){_s;TTrap::UnTrap();}}

Why is there an if() if the return value is always 0? The three assembler versions of TTrap::Trap() that I can find all return 0. Is it legacy code?


Answer:
TTrap::Trap() saves the execution context (registers etc) onto the stack, adds this context to the list of trap frames in the executive and returns zero. This ensures that the TRAP'd statement is executed.
If an error occurs during this code: User::Leave() pops the top trap frame from the list, saves the error value and finally restores the execution context from the TTrap structure, and then returns 1. This "returns" not to the caller of User::Leave() but to the last caller of TTrap::Trap(), but this time the conditional expression is false and so execution drops through past the TRAP harness as desired.

If User::Leave() is not called then TTrap::UnTrap() just removes the trap frame from the list.