473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert exceptions in wrapper without using macros?

Hi!

Do you know different alternatives to convert exceptions in many
methods of some wrapper classes.

User -Wrapper classes -LibraryClasses -...

Wrapper classes catch an exception of only one type, let's call it
e.g. "InternalException_c".
There are no other types of exceptions, which should be catched /
converted.

The caught exception object should be converted e.g. to type
"ConvertedException_c".

Quite many methods of the wrapper classes should make the same
conversion.

void Wrapper::method1 ()
{
try {
...
} catch (InternalException_c& ex)
{
throw ConvertedException_c(ex);
}
}

The same applies to quite many methods, method1, method2, method3 ...,
maybe tens of different kinds of methods and in different wrapper
classes.

Quite many of those methods are planned to be inline, it is quite
mandatory, because those are called very intensively. Inline methods
are defined in the header:

class Wrapper2_c
....
inline void method2 ()
{
try {
...
} catch (InternalException_c& ex)
{
throw ConvertedException_c(ex);
}
}
....

What are the alternatives to avoid copy pasting the same try catch
clauses to many methods?
Macros might be one "alternative", but I think it should not be
thought as a real alternative in the C++ world.

I have thought to use some typedefs (caller should catch the typedef
instead of the real object),
but I think the best solution would be to convert the exception in the
methods of the wrapper classes. The caller should not make the
conversion. The only place, where the conversion should be made, are
the wrapper classes.

I have used templates, so if there could be a solution by using
templates (is there?) or by using some other techiques, please share
your information!
Thank you for your answers already in advance!

Nov 15 '06 #1
4 2110
* a_*****@yahoo.com:
>
What are the alternatives to avoid copy pasting the same try catch
clauses to many methods [in order to convert exceptions]?
Macros might be one "alternative", but I think it should not be
thought as a real alternative in the C++ world.
Macros are the best alternative.

Theoretically you can use exception specifications and std::unexpected,
but since exception specifications are generally Bad in C++, they're not
used much (except the empty no-throw exception specification), and so
you can't really expect your compiler to implement them, even in 2006.

An exception specification based approach would go like this (off the
cuff, untried code):

typedef int QuaintException;

void myXTranslator()
{
try
{
throw;
}
catch( QuantException const& x )
{
throw std::runtime_exception( "Gnurk" ); // Translated.
}
catch( std::exception const& x )
{
throw; // As-is.
}
catch( ... )
{
std::terminate(); // Not supported.
}
}

class XTrans
{
private:
std::unexpected_handler myOldHandler;

public:
XTrans()
: myOldHandler( std::set_unexpected( ::myXTranslator ) )
{}

~XTrans()
{ std::set_unexpected( myOldHandler ); }
};

...

template< typename T >
void isRaiiObject( T& ) {} // Suppress compiler silly-warnings.

void SomeWrapperClass::someMemberFunc() throw( std::exception )
{
XTrans xt; isRaiiObject( xt );

SomeSillyVendorsApi::someEvenMoreSillyFuncWithQuai ntException();
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '06 #2
Thanks for the comments.

I thought about my question more,
and I think there could be at least one solution how to convert the
exception.

I could pass the method, which is wanted to be called as a pointer to
an intermediate method, which handles the conversion of the exceptions.

I could do this way:

class wrapper_c
{

int method1()
{
return intermediateMethod<int>(&wrapper_c::finalMethod1);
}

.... // more different kinds of methods...

private:

int finalMethod1()
{
// call to the library.

// IT may throw
//throw int(81);
// or return a value
//

return int (81);
}

.... // more different kinds of "final" methods...

// The intermediate method

template <class RETURN_CLASS>
RETURN_CLASS execute(RETURN_CLASS (wrapper_c::*my_memfunc_ptr)(void))
{

try {

return (this->*my_memfunc_ptr)();

} catch (int exc) {
// Convert the exception.
string excConverted;
excConverted += exc;
throw excConverted;
}
}; // wrapper_c

Or something like that.
(It would need to be implemented to every wrapper class somehow.)
Only problem is the paramaters / arguments, which are passed first to
method1, then to execute and then to finalMethod1 and from there to
library.
Does someone know how to handle the arguments?
(Some kind of a pointer to the arguments/parameters? Pass it through to
finalMethod1, and read arguments from the pointer? )

Of course I could make e.g. a struct to which I could store the values
and pass it from method1 through execute to the finalMethod1. Some sort
of templates could be used again.
But I would like to use something more convenient.
I would not like to create the structures.

I could also make several execute methods, which each take different
amount of arguments.
The types of the arguments could be handled with help of templates.
But I do not wish to create few different execute methods, because of
this.
I would just like to pass all of the arguments through one pointer, or
with some other technique, to the finalMethod1.

Therea are only void in the parameters/arguments in the code above,
but I would like to pass more parameters with different kinds of types.

Nov 15 '06 #3

And ofcourse,
method1 is/should be public:
class wrapper_c
{
public:
>
int method1()
{
return intermediateMethod<int>(&wrapper_c::finalMethod1);
}
Nov 15 '06 #4

I started a new discussion about the issue here:

http://groups.google.fi/group/comp.l...a94d35c7f1c51c

Topic: Take paramaters as one pointer?

I do not know is my question understandable.

Nov 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
8
by: Shane Groff | last post by:
I know this is a recurring discussion (I've spent the last 3 days reading through threads on the topic), but I feel compelled to start it up again. After reading through the existing threads, I...
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
8
by: Richard Collette | last post by:
When attempting to debug a webservice, I get the error: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary. In reading...
42
by: Jon Harrop | last post by:
Why are exceptions in C++ ~6x slower than exceptions in OCaml? -- Dr Jon D Harrop, Flying Frog Consultancy Objective CAML for Scientists...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
13
by: mike3 | last post by:
Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.