简体   繁体   English

用户定义的CORBA异常在编译后给出了错误

[英]User-defined CORBA exceptions gave me errors after compilation

I have some troubles with my own, user-defined exception in CORBA. 我在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时,它给了我一些错误( 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? 还有一个问题:我怎样才能在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: 继承我的代码: http ://speedy.sh/F5utX/user-defined-exception.tar我在java中做了同样的事情(代码也在user-defined-exception.tar中),但java代码给了我这个:

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. 顺便说一句,throw声明在大多数(读“all”)编译器实现中确实没有任何有用的效果,并且在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: 编辑:正如Johnny Willemsen所说,您可以将成员添加到这样的例外:

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). 在C ++中,您通常通过常量引用来捕获异常(这也取决于它是如何抛出的)。 I'm writing this from memory (CORBA stuff), so I hope I'm not missing nothing major. 我是从内存中写这个(CORBA的东西),所以我希望我没有遗漏任何重要内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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