Symbian learning Summary-basics-Exception Handling in Symbian C ++

Source: Internet
Author: User

 

This article was originally prepared by Yang. If you need to reselect the article, please indicate the source. Thank you!

In modern advanced languages, most languages have keyword corresponding to exception handling, such as try/catch in C ++, Try/try/catch in Delphi/Finally, these keywords are directly supported by the compiler.

However, when Symbian OS C ++ was invented, the keyword try/catch/finally in C ++ was not yet supported by the standard. In addition, according to official Symbian, the exception handling mechanism of C ++ is too cumbersome and lightweight. They have designed a set of higher efficiency, it is more suitable for the exception handling mechanism of small and memory-limited devices.

I. Review history
In standard C ++, we use the following method to handle exceptions:

123456789
void DoTest() {try {int n = 1 / 0;}catch (...) {// do something...throw;}}

Ps: Standard C ++ does not support finally.
Apparently, an exception is thrown back when the program runs to row 3, and then jumps to row 3 to execute Exception Handling. In row 3, throw is used to throw the exception to the outside.

Ii. Change concepts
Exception Handling in Symbian OS C ++ is similar to that in Standard C ++. First, write the exception handling code in Standard C ++ in the following way:

12345678910111213
void DoDiv() {int n = 1 / 0;}
void DoTest() {try {DoDiv();}catch(...) {// do Something...throw;}}

First, extract the statements that may cause errors to the DoDiv, and call them in the exception handling capture statement when calling DoDiv in DoTest.

Iii. Exception Handling in Symbian
Here, we translate the above error handling code in the form of Symbian C ++:

12345678910
void DoDivL() {int n = 1 / 0;}
 
void DoTest() {TRAPD(error, DoDivL());if (error) {User::Leave();}}

The first line of the function DoDivL, the suffix "L" means: This function may throw an exception. This is just a naming convention for Symbian C ++, so that programmers will think of capturing exceptions as soon as they see L. If you do not write the suffix "L", the program execution will not be affected.

The TRAPD of Row 3 is the error handling macro of Symbian C ++, and the call prototype is TRAPD (_ r, _ s ). The first parameter _ s in TRAPD is a statement that may throw an exception. The first parameter _ r is the error code returned. If an error occurs when _ s is called, the error code is stored in _ r, and the program continues to execute 7th lines.

User: Leave () is equivalent to throw in c ++, and an exception is thrown out.
Symbian C ++ provides many User: Leave ** methods, such as: User: LeaveIfError (), User: LeaveNoMemory (), User: LeaveIfNull (), where, user: LeaveIfError () is equivalent to 7th to 9th rows of the above Code.

Iv. Differences between macro TRAP and TRAPD:
"D" indicates the meaning of Declare. The macro TRAP call prototype is TRAP (_ r, _ s ). From the code line 7th above, we can see that the variable error has been defined by TRAPD and can be directly used. The TRAP macro does not define the _ r parameter variable. Using the TRAP macro, The DoTest function will be written in the following way:

1234567
void DoTest() {TInt error;TRAP(error, DoDivL());if (error) {User::Leave();}}

For programs with a default stack size of 8 K, we should prioritize the use of TRAP macros.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.