You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Ronald van Gorkum <ro...@trust2core.de> on 2014/02/17 09:17:32 UTC
Thrift custom exceptions C++
Hi all,
I defined some custom exceptions in the Thrift IDL. From this IDL I
generated C++ code. In the generated code the parent class (TException)
is inherited, but the constructor isn't called. When I receive an
exception I'm always receiving the default exception class instead of
the desired custom exception class. If I look into the sources the
parent class variable message_ is only set when the constructor of that
class is being called with the name of the custom exception (what is not
done in the generated code). Or, because the variable is protected, the
content is changed by the custom exception class, but also therefore is
no method generated. Who has also experiences with custom exceptions and
raise them in C++ code and has a proper solution? Or is this a Thrift
generator bug?
Thanks.
Ronald
Re: Thrift custom exceptions C++
Posted by Ben Craig <be...@ni.com>.
The custom exceptions don't modify message_. They do transfer user
defined state across the RPC boundary though. For example...
// example.thrift
exception MyException
{
1: i32 error_code
}
service MyService
{
void do_something() throw(1: MyException the_exception)
}
// server
void MyService::do_something(){
MyException exception;
exception.error_code = 42;
throw exception;
}
//client
try {
my_client_obj.do_something();
} catch (const MyException &e) {
std::cout<<e.error_code; //should print 42
}
Ronald van Gorkum <ro...@trust2core.de> wrote on 02/17/2014
02:17:32 AM:
> From: Ronald van Gorkum <ro...@trust2core.de>
> To: thriftuser <us...@thrift.apache.org>,
> Date: 02/17/2014 02:18 AM
> Subject: Thrift custom exceptions C++
>
> Hi all,
>
> I defined some custom exceptions in the Thrift IDL. From this IDL I
> generated C++ code. In the generated code the parent class (TException)
> is inherited, but the constructor isn't called. When I receive an
> exception I'm always receiving the default exception class instead of
> the desired custom exception class. If I look into the sources the
> parent class variable message_ is only set when the constructor of that
> class is being called with the name of the custom exception (what is not
> done in the generated code). Or, because the variable is protected, the
> content is changed by the custom exception class, but also therefore is
> no method generated. Who has also experiences with custom exceptions and
> raise them in C++ code and has a proper solution? Or is this a Thrift
> generator bug?
>
> Thanks.
>
> Ronald
Re: Thrift custom exceptions C++
Posted by Ronald van Gorkum <ro...@trust2core.de>.
Hi Jens and Ben,
thanks for your reactions. I regenerated the cpp thrift code using
Thrift 0.9.1 and the problem disappeared. As in the example from Ben I'm
able to raise Exceptions and to catch them on the client side. Perhaps
(but I have to reproduce the problem first again) it was because the
inconsistent build environment with version 0.8 and 0.9.1.
Anyway, thanks for your help.
Regards,
Ronald
On 02/17/2014 09:51 PM, Jens Geyer wrote:
> Hi Ronald,
>
> In my opinion, the behaviour is correct, as there is no inheritance of
> structural types with thrift, which includes exceptions. So the base
> class message_ field is not used with your custom exceptions, because
> it is not used :-) The string CTOR is solely (to be) used internally
> at various places in the C++ library code.
>
> Have fun,
> JensG
>
>
> -----Ursprüngliche Nachricht----- From: Ronald van Gorkum
> Sent: Monday, February 17, 2014 9:17 AM
> To: thriftuser
> Subject: Thrift custom exceptions C++
>
> Hi all,
>
> I defined some custom exceptions in the Thrift IDL. From this IDL I
> generated C++ code. In the generated code the parent class (TException)
> is inherited, but the constructor isn't called. When I receive an
> exception I'm always receiving the default exception class instead of
> the desired custom exception class. If I look into the sources the
> parent class variable message_ is only set when the constructor of that
> class is being called with the name of the custom exception (what is not
> done in the generated code). Or, because the variable is protected, the
> content is changed by the custom exception class, but also therefore is
> no method generated. Who has also experiences with custom exceptions and
> raise them in C++ code and has a proper solution? Or is this a Thrift
> generator bug?
>
> Thanks.
>
> Ronald
Re: Thrift custom exceptions C++
Posted by Jens Geyer <je...@hotmail.com>.
Hi Ronald,
In my opinion, the behaviour is correct, as there is no inheritance of
structural types with thrift, which includes exceptions. So the base class
message_ field is not used with your custom exceptions, because it is not
used :-) The string CTOR is solely (to be) used internally at various places
in the C++ library code.
Have fun,
JensG
-----Ursprüngliche Nachricht-----
From: Ronald van Gorkum
Sent: Monday, February 17, 2014 9:17 AM
To: thriftuser
Subject: Thrift custom exceptions C++
Hi all,
I defined some custom exceptions in the Thrift IDL. From this IDL I
generated C++ code. In the generated code the parent class (TException)
is inherited, but the constructor isn't called. When I receive an
exception I'm always receiving the default exception class instead of
the desired custom exception class. If I look into the sources the
parent class variable message_ is only set when the constructor of that
class is being called with the name of the custom exception (what is not
done in the generated code). Or, because the variable is protected, the
content is changed by the custom exception class, but also therefore is
no method generated. Who has also experiences with custom exceptions and
raise them in C++ code and has a proper solution? Or is this a Thrift
generator bug?
Thanks.
Ronald