【问题标题】:User-defined CORBA exceptions gave me errors after compilation用户定义的 CORBA 异常在编译后给了我错误
【发布时间】:2012-05-05 07:58:58
【问题描述】:

我在 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.

【问题讨论】:

    标签: java c++ exception corba


    【解决方案1】:

    你应该创建一个异常类型的新实例,像这样:

    throw myOwnException();
    

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

    throw Interfface::myOwnException();
    

    顺便说一句,throw 声明在大多数(阅读“全部”)编译器实现中确实没有任何有用的效果,并且在 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 的东西),所以我希望我没有遗漏任何重要内容。

    【讨论】:

    • 很高兴知道;) 我在 *.h 和 *.cpp 文件中删除了“抛出声明”。虽然 throw myOwnException();给了我这个:|| Implementation.cpp:在成员函数'virtual void Implementation::ffunction(double)'中:Implementation.cpp:6:错误:'myOwnException'未在此范围内声明||该代码: throw Interface::myOwnException();帮助过我。所以我可以说,问题解决了。非常感谢! :)
    • 再问一个问题:我如何定义我的idl接口,以获得这样的东西:|| String cause = "某事出错:P"; /*一段代码*/;抛出 myOwnException(原因);||你知道吗?
    • 只需在异常声明中添加一个成员。例如字符串原因;。
    • @Brian Brown,我对您的后续问题进行了一些扩展。
    • @this: 是的,谢谢:) 我明天会尝试做一个例子,我会在这里发布它;D
    猜你喜欢
    • 2021-12-23
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多