繁体   English   中英

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

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

我在CORBA中遇到了自己的用户定义异常。 这是我非常简单的代码:

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);
}

当我编译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’

这段代码出了什么问题? 还有一个问题:我怎样才能在Java中做同样的事情?

继承我的代码: 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.

您应该创建异常类型的新实例,如下所示:

throw myOwnException();

您还可能需要限定命名空间:

throw Interfface::myOwnException();

顺便说一句,throw声明在大多数(读“all”)编译器实现中确实没有任何有用的效果,并且在C ++ 11中已弃用。 我知道它们可能是自动生成的,但它仍然很好知道。 我发现在实践中,这些声明在随后的源更改中往往不再准确。 不要将它们拖到您的实现文件中。 在第二方面,您使用无信息的变量和类型名称,并且没有一致的命名约定。

编辑:正如Johnny Willemsen所说,您可以将成员添加到这样的例外:

exception myOwnException {
    string reason;
};

每个例外成员都将表示为公共类成员。 将生成必要的构造函数,因此您可以抛出这样的异常:

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

抛出异常时,如果它没有在本地捕获,那么它将被序列化并传播到客户端(远程过程调用者)。 在那里它将被反序列化,所以你可以捕获它并访问其成员有点像:

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

在C ++中,您通常通过常量引用来捕获异常(这也取决于它是如何抛出的)。 我是从内存中写这个(CORBA的东西),所以我希望我没有遗漏任何重要内容。

暂无
暂无

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

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