03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

User::Leave(KErrNone). This appears to work because you don't get<br />

the error message. But in fact the error handler isn't called at all, so you don't<br />

get some other useful things either.<br />

So use iEikonEnv->LeaveWithInfoMsg() or, if you don't need an infomessage,<br />

use the same function but specify a resource ID of zero.<br />

In <strong>Chapter</strong> 13, the streams program includes an example of this pattern.<br />

6.7 More on Panics<br />

So far, I've dealt with how you can respond to errors that are generated by the environment<br />

your program runs in – whether out-of-memory errors, files not being there, or bad user<br />

input.<br />

One type of error that can't be handled this way is programming errors. These have to be<br />

fixed by rewriting the offending program. During development, that's usually your program<br />

(though, like the rest of us, you probably start by blaming the compiler!). The best thing<br />

Symbian OS can do for you here is to panic your program – to stop it from running as soon<br />

as the error has been detected, and to provide diagnostic information meaningful enough for<br />

you to use.<br />

The basic function here is User::Panic(). Here's a panic function I use in my Battleships<br />

game, from \scmp\battleships\control-ler.cpp:<br />

static void Panic(TInt aPanic)<br />

{<br />

_LIT(KPanicCategory, "BSHIPS-CTRL");<br />

User::Panic(KPanicCategory, aPanic);<br />

}<br />

User::Panic() takes a panic category string, which must be 16 characters or less<br />

(otherwise, the panic function gets panicked!), and a 32-bit error code.<br />

On the emulator debug build, we've seen what this does – the kernel's panic function<br />

includes a DEBUGGER() macro that allows<br />

the debugger to be launched with the full context from the function that called panic. That<br />

gives you a reasonable chance of finding the bug.<br />

On a release build, or on real hardware, a panic simply displays a dialog titled Program<br />

closed, citing the process name, and the panic category and the number you identified.<br />

Typically, it's real users who see this dialog, though you might be lucky enough to see it<br />

during development, before you release the program. To find bugs raised this way, you<br />

essentially have to guess the context from what the user was doing at the time, and the<br />

content of the Program closed dialog. You'll need inspiration and luck.<br />

You can shorten the odds by being specific about the panic category and number and by<br />

good design and testing before you release.<br />

Although technically it's a thread that gets panicked, in fact Symbian OS will close the entire<br />

process. On a real machine, that means your application will get closed. On the emulator,<br />

there is only one Windows process, so the whole emulator is closed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!