Exception Handling In Symbian C++

Login to reply to this topic.
Fri, 2006-05-19 06:21
Joined: 2006-01-09
Forum posts: 105
Hi Developers,

i know exception handling in Desktop C++...
e.g.

try
{
:
://some code causing error...such as Zero divide...
:
}

catch(...)
{
   cout<<"\n Exception occured !!";
}

i've read that in symbian C++ we do the same using.....


TRAPD(err,TestErrorL());

if(err)
{
   User::InfoPrint(_L("Exception Occured."));
}


my problem is that..as soon as i reach the exception causing statement inside TestErrorL() method....my program crashes.

so...i would like to know how to trap exceptions in Symbian C++. i want to trap any kind of exception as catch(...) does in C++.

please throw some light on it...



Cheers!!
CodePupil

Thanks and Regards
CodePupil
__________________________
You are I and I am he .. !!


Fri, 2006-05-19 07:55
Joined: 2004-06-08
Forum posts: 148
Re: Exception Handling In Symbian C++
In order to chatch something in C++ you have to throw it first. It is the same in Symbian. You should use a "User::Leave" statement in your TestErrorL(). One more thing. You can handle exceptions, but you cant handle program errors, like bad pointers and so on.
Fri, 2006-05-19 08:01
Joined: 2006-01-09
Forum posts: 105
Re: Exception Handling In Symbian C++
Hi..

but in C++...it is not required to use throw every time...
if..any exception occures inside try block...it will be catched by catch(...)

you can check it for yourself as well.

and also where to use..User::Leave()..because program crashes as soon as it reaches that particular statement...which causes the exception...

Can't we trap the Zero divide Exception in Symbian C++ as Normal C++ does ??


Cheers!!!!!
CodePupil

Thanks and Regards
CodePupil
__________________________
You are I and I am he .. !!

Fri, 2006-05-19 19:41
Guest (not verified)
Forum posts: 2043
Re: Exception Handling In Symbian C++
>>but you cant handle program errors, like bad pointers and so on.

You can, and you can catch divide by 0 exceptions, its possible to install an exception handler.
Sat, 2006-05-20 02:00
Joined: 2004-10-31
Forum posts: 92
Re: Exception Handling In Symbian C++
Actually, I think the RThread exception handling methods have been removed in v9.
Sat, 2006-05-20 05:47
Joined: 2006-01-09
Forum posts: 105
Re: Exception Handling In Symbian C++
Hi

hey...Guest...

please tell me how can it be done ??

any way.. i tell you the real problem...
my program crashes on real device giving panic : KERN-EXEC 3
and some times : KERN-EXEC 0

but it doesn't happen always.
most of the time it runs quiet well.....

so please tell me how can i trap those ....

and my previous question was regarding C++'s try/catch Vs Symbian's TRAP...

in C++... try block automatically throws exception like Zero divide..which can be caught by catch(...).

how can this same handle in Symbian Huh

although i dont know too much about symbian's error handling...yet hitherto i've come to this point that C++'s try/catch mechanism is far better than Symbian's......


Cheers!!
CodePupil

Thanks and Regards
CodePupil
__________________________
You are I and I am he .. !!

Sat, 2006-05-20 12:49
Joined: 2006-03-09
Forum posts: 9
Re: Exception Handling In Symbian C++
Code:
Actually, I think the RThread exception handling methods have been removed in v9.
No, it is moved to User::SetExceptionHandler

Hi CodePupil,
maybe you know Windows structured exception handling.
It is nearly the same here.

Use SetExceptionHandler with KExceptionInteger and
handle EExcIntegerDivideByZero in your ExceptionHandler()
Sat, 2006-05-20 13:04
Joined: 2006-01-09
Forum posts: 105
Re: Exception Handling In Symbian C++
Thanks for this Trotwa...

can you please provide me some example code/URL having some example regarding this...if you dont mind??


Cheers!!
CodePupil

Thanks and Regards
CodePupil
__________________________
You are I and I am he .. !!

Sat, 2006-05-20 16:00
Joined: 2006-03-09
Forum posts: 9
Re: Exception Handling In Symbian C++
This is not tested and not compiled, but maybe a good starting point. (I have currently no other sources)

void PupilsExcHandler(TExcType aExc)
{
  if(aExc == EExcIntegerDivideByZero)
  {
     return; // ignore   
     //User::Leave(KErrDivideByZero);
  }
  else
  {
     _LIT(KPanicCategory,"Exception");
     User::Panic(KPanicCategory, TExcType(aExc));
  }

}

As soon as possible in your current thread:

#ifdef EKA2
   User::SetExceptionHandler(PupilsExcHandler, KExceptionInteger);
#else
        RThread().SetExceptionHandler(PupilsExcHandler, KExceptionInteger);
#endif
Tue, 2007-05-08 11:39
Joined: 2007-02-11
Forum posts: 8
Re: Exception Handling In Symbian C++
This code is not working on Symbian 3rd edition. Panics are caught on the emulator (KERN-EXEC3 for example) but any exception handler is completely ignored if the very same code is run on an actual phone.

Any idea what's going on?
Tue, 2007-05-08 12:34
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Exception Handling In Symbian C++
Please note that panics are not the same as exceptions. You might have come to the same conclusion, however, I thought it was worth making it clear. So if your intent is to catch panics in general, then I tell you that an exception handler might not be the perfect tool for this. There are, however, some exceptions to this: a KERN-EXEC 3 panic can be fairly mapped to an EExcAccessViolation exception that your exception handler can catch.

It's worth noting that in case you want to catch each and every panic in your (single threaded) program, then you might want to run your code from within a separate thread so that when that thread exits for some reason (panics, for example) your main thread takes the control back and can evaluate the reason of exiting.

Cheers,

Tote

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Wed, 2007-05-09 09:58
Joined: 2007-02-11
Forum posts: 8
Re: Exception Handling In Symbian C++
This is my exception handler:

User::SetExceptionHandler(handler, KExceptionFault|KExceptionInteger|KExceptionAbort|KExceptionKill|KExceptionFpe|KExceptionUserInterrupt);

My problem is that whenever a KERN-EXEC 3 happens, it is caught by the "handler" function, but only when running on the emulator. If I run the very same code on a mobile phone, specifically the N93, the "handler" function is never called; on the emulator I would never see the KERN-EXEC 3 message, but on the phone I would see it.
Wed, 2007-05-09 10:12
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Exception Handling In Symbian C++
Well, I'm afraid this is such a "feature" that you have to live with. Unless you go for the separate thread solution I've just described below.

Also note that it's the last resort for the kernel to call a user-defined exception handler. Based on what you wrote it seems that the kernel has already shown the dialog by the time it calls your function.

Tote

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Wed, 2007-05-09 14:47
Joined: 2007-02-11
Forum posts: 8
Re: Exception Handling In Symbian C++
Quote from: tote
Well, I'm afraid this is such a "feature" that you have to live with. Unless you go for the separate thread solution I've just described below.
But this thread solution you described, it's basically calling the faulty code from inside a thread correct? This still doesn't prevent the KERN-EXEC 3 dialog from appearing, or am I missing something?
Wed, 2007-05-09 14:50
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Exception Handling In Symbian C++
Quote from: mserougi
Quote from: tote
Well, I'm afraid this is such a "feature" that you have to live with. Unless you go for the separate thread solution I've just described below.
But this thread solution you described, it's basically calling the faulty code from inside a thread correct? This still doesn't prevent the KERN-EXEC 3 dialog from appearing, or am I missing something?

The advantage of the thread solution is that it won't make your program collapse. So basically although the dialog is still shown your program continues to execute. That's sometimes very valuable, but I admit that it doesn't entirely solve your problem.

Tote

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

  • Login to reply to this topic.