使用swig自带的exception

in #cn6 years ago
%include "exception.i"
%exception std::vector::getitem {
    try {
        $action
    } catch (std::out_of_range& e) {
        SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
    }
}

可以自定义(仅仅在有catch或throw情况下有用)throws="java.io.IOException" 是在Java中抛出异常。

%typemap(throws, throws="java.io.IOException") std::out_of_range {
  // custom exception handler
}
//
int foo(int x) throw(std::out_of_range);

使用异常是可用变量


$action The actual operation to be performed (a function call, method invocation, variable access, etc.)
$name   The C/C++ symbol name for the function.
$symname    The symbol name used internally by SWIG
$overname   The extra mangling used in the symbol name for overloaded method. Expands to nothing if the wrapped method is not overloaded.
$wrapname   The language specific wrapper name (usually a C function name exported from the shared object/dll)
$decl   The fully qualified C/C++ declaration of the method being wrapped without the return type
$fulldecl   The fully qualified C/C++ declaration of the method being wrapped including the return type
$parentclassname    The parent class name (if any) for a method.
$parentclasssymname The target language parent class name (if any) for a method.