简体   繁体   中英

User-defined CORBA exceptions gave me errors after compilation

I have some troubles with my own, user-defined exception in CORBA. Here's my very simple code:

interface Interfface
{
    exception myOwnException {};

    void ffunction(in double arg) raises (myOwnException);
};

#include "Interfface.hh"

class Implementation : public POA_Interfface
{
    public :
        virtual void ffunction(double arg) throw (myOwnException);
};

#include "Implementation.h"

void Implementation::ffunction(double arg) throw (myOwnException)
{   
    arg ++;    
    throw (myOwnException);
}

And when I compiled Implementation.cpp, it gave me some errors ( http://pastie.org/private/a22ikk09zkm9tqywn37w ):

Implementation.cpp: In member function ‘virtual void Implementation::ffunction(double)’:
Implementation.cpp:5: error: ‘myOwnException’ was not declared in this scope
In file included from Implementation.cpp:1:
Implementation.h:6: error: expected type-specifier before ‘myOwnException’
Implementation.h:6: error: expected ‘)’ before ‘myOwnException’
Implementation.h:6: error: expected ‘;’ before ‘myOwnException’
Implementation.cpp:3: error: expected type-specifier before ‘myOwnException’
Implementation.cpp:3: error: expected ‘)’ before ‘myOwnException’
Implementation.cpp:3: error: expected initializer before ‘myOwnException’

What's wrong with this code? And one more question: how can I do the same stuff in Java?

Heres my code: http://speedy.sh/F5utX/user-defined-exception.tar I did the same in java (code also in user-defined-exception.tar) but java code gave me this:

Note: InterffacePOA.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

You're supposed to create new instance of exception type, like this:

throw myOwnException();

You also may need to qualify namespace:

throw Interfface::myOwnException();

By the way, throw declarations really don't have any useful effect in most (read "all") compiler implementations and are deprecated in C++11. I know they probably were autogenerated here, but it's still good to know. I find that in practice these declarations tend to cease to be accurate with subsequent source changes. Don't drag them to your implementation files. On a second side note, you use uninformative variable and type names, and have no consistent naming convention.

EDIT: As Johnny Willemsen said, you can add members to exceptions like this:

exception myOwnException {
    string reason;
};

Each exception member will be represented as a public class member. Necessary constructors will be generated, so you can throw such exception like this:

throw Interfface::myOwnException("Wrong polarity!");

When exception is thrown, if it is not catched locally, then it gets serialized and propagates to client (remote procedure caller). There it will be deserialized, so you can catch it and access its members kinda like that:

try
{
    server->ffunction(0);
}
catch(const Interfface::myOwnException &ex)
{
    std::cout << ex.reason;
}

In C++ you usually catch exceptions by constant reference (that also depends on how it was thrown). I'm writing this from memory (CORBA stuff), so I hope I'm not missing nothing major.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM