[Bada] Try Catch in Bada

Most of the methods in the bada API indicate if something goes wrong by a returning a “result” or by setting the result that is returned by GetLastResult, instead of using the C++ exceptions. According to bada’s documention, this is because C++ exceptions require larger runtimes. I used to use if statements to check for errors after each of these calls. The problem with this is that it would usually result in my code being cluttered with if statements. Another approach that I have across, is to use the try macros.

There are few different try macros. They are:

TryCatch(condition, cleanup, message)
TryReturn(condition, value, message)
TryReturnVoid(condition, message)

The conditions in each of the macros works in the same way as conditions for if statements. In other words a non-null or non-zero value will also be evaluated to be true and null or zero values are evaluated as false. When the condition is evaluated to true, the next line with code is executed. Otherwise, they will add the given message to the logs. What happens after that differs slightly between each of the macros.

TryCatch

When the condition is evaluated to false, this will goto CATCH. You could think of the macro like this:

if (!condition)
{
    goto CATCH;
}

For example, you can use it like this:

void
TryCatchDemo::UseTryCatch(void)
{
    TryCatch(1==2, , "1 is NOT 2");

    // This should not be called.
    AppLog("This should not appear");

    CATCH:
        AppLog("Catch block");
}

Calling the method will result in the following line being added to the console output:

0010.469,EXCEPTION,P26,T03,A93,TryCatchDemo::UseTryCatch (45) > 1 is NOT 2
0010.469,INFO,P26,T03,A93,TryCatchDemo::UseTryCatch (51) > Catch block

TryReturn

This is useful for methods that could just return some value, when an error occurs. It is like:

if (!condition)
{
    return value;
}

An example of the macro in use:

int
TryCatchDemo::UseTryReturn(void)
{
    TryReturn(false, 0, "Always false");
    return 1;
}

If this was invoked with:

    int result = UseTryReturn();
    AppLog("UseTryReturn returned %d", result);

the resulting output would be:

0010.469,EXCEPTION,P26,T03,A93,TryCatchDemo::UseTryReturn (57) > Always false
0010.469,INFO,P26,T03,A93,TryCatchDemo::OnAppInitializing (36) > UseTryReturn returned 0

TryReturnVoid

This is similar to TryReturn, but is used in void methods. Think of this one as:

if (!condition)
{
    return;
}

An example of using the macro:

void
TryCatchDemo::UseTryReturnVoid(void)
{
    TryReturnVoid(-1 > 0, "-1 is NOT GREATER THAN 0");
    AppLog("This should not appear");
}

Invoking this method will produce:

0010.469,EXCEPTION,P26,T03,A93,TryCatchDemo::UseTryReturnVoid (64) > -1 is NOT GREATER THAN 0

Assertion Macros

Finally, there are a couple of assert macros. Unlike, the above try macros, these will terminate the app if a condition is not satisfied. These macros are:

    AppAssert(condition) 
    AppAssertf(condition, message) 

It should be obvious that the only difference between the two variants is that AppAssertf will output also add a message to the log output.

Using a Format String with the Macros

Lastly, each of the macros that support a message parameters actually support format strings. A format string is similar to the format string of printf on systems that support it (bada does NOT support it). For example, the line:

AppAssertf(false, "result is %d", 3);

will produce:

0010.280,EXCEPTION,P26,T03,A93,../src/TryCatchDemo.cpp (36) > result is 3

Using the Standard Try Catch With Your Own Exceptions

While the bada API uses return codes instead of exceptions, you can still declare your own exceptions and use the standard try catch mechanism! For example, you can do this:

class CustomException
{
public:
    CustomException() {};
};

...
    try
{
    throw CustomException();
}
catch (CustomException e)
{
    AppLog("Caught Custom");
}
...

Executing this will produce:

0010.431,INFO,P26,T03,A93,TryCatchDemo::OnAppInitializing (45) > Caught Custom

If you are going to do this, just remember that the API does NOT use it for a reason!

Advertisements

2 Responses to [Bada] Try Catch in Bada

  1. Dmtiry says:

    This is just dumb – they threw away C++ exceptions and reinvented the wheel with that macros. Did samsung guys travel in time to get those solutions from first Symbian versions?

    Just ridiculous.

    • kahgoh says:

      The reason given in the bada documentation (bada developer guide -> Basics of bada programming -> Exception Handling):

      Samsung bada uses error results instead of C++ exceptions, since C++ exceptions require too large of a runtime for resource-constrained devices, and thus incur too much overhead.

      To be honest though, I’m not sure how much of a difference there is between run times using C++ exceptions and using their macros. I guess when they first started working on the platform (according to the What is bada page, it would have been at least 10 years ago), when the hardware had much smaller memory, this could have been important. However, this should be less of a problem with today’s technology.

      By the way, I probably should have mentioned that just because the bada API doesn’t use the standard C++ try catch mechanism doesn’t meant that you can’t use them! I’ve just added that to the post!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: