Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Try and catch exception handling in Open C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: exception, try, catch, throw
Created: aknyman (29 Sep 2008)
Last edited: lpvalente (23 Sep 2014)



This code snippet shows how to use the try and catch exception handling mechanism in Open C++. The traditional Symbian C++ cleanup mechanism handles exceptions in a completely different way when compared to the standard C++ try and catch mechanism. From Symbian v9.1 onwards also the try and catch mechanism has been supported, although Symbian has recommended to use the cleanup mechanism. However, when working with Open C++, the try and catch mechanism can be used.

The try and catch mechanism allows one section of an application to detect and dispatch an error condition and another section to handle it. These two sections are represented by reserved words try and catch. The throw statement with the data type that matches the parameter of the proper catch handler is used to jump to a catch section. The catch section will catch every type of exception if the ellipsis (...) is used. It is usually preferable to catch exceptions by reference to avoid the thrown variable to be copied onto the stack.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Header file

#include <exception>
#include <string>
class ExampleException : public std::exception
ExampleException(const std::string& name, const std::string & msg):
message(name + "[" + msg + "]"){;}
virtual ~ExampleException(){;}
virtual const char * what() const {return message.c_str();}
std::string message;
class ExampleInputException : public ExampleException
ExampleInputException(const std::string & msg)
: ExampleException("InputException",msg){;}
virtual ~ExampleInputException(){;}
class ExampleArgumentException : public ExampleException
ExampleArgumentException(const std::string & msg)
: ExampleException("ArgumentException",msg){;}
virtual ~ExampleArgumentException(){;}

Source file

#include "ExampleException.h"
#include <iostream> //cout, cin
#include <stdio.h> //atoi()
using namespace std;
//the example function that can throw an exception
void throw_exception_if_null(const string* text)
throw ExampleArgumentException("A null pointer given as a parameter");
cout << *text << endl;
int main()
string* str = NULL;
int number = 0;
string input;
cout<<"Please, enter the number between 1 and 10)";
getline(cin, input);
number = atoi(input.c_str());
if(number < 1 || number > 10)
throw ExampleInputException("The given number is not between 1 and 10");
if(number > 5)
str = new string ("Hello!");
//the str pointer refers still to NULL if the given
//number is 1-5 -> the exception will be thrown
//throw exception here anyway (values 6-10)
throw number;
catch(int e) //catch by value
cout << "Exception with the number: " << e << endl;
catch (const ExampleException& e) //catch by reference
//...or catch (const exception& e)
cout << e.what() << endl;
catch (...)
cout << "Unhandled exception";
delete str;
str = NULL;
return 0;


The example application has prompted the user to enter a number between 1 and 10 and depending on the input, an exception has been thrown and caught.

This page was last modified on 23 September 2014, at 19:59.
233 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.