1
%module csharp_exceptions
9
Ex(const char *msg) : message(msg) {}
10
const char *what() { return message; }
14
%exception ThrowByValue() {
18
SWIG_exception(SWIG_DivisionByZero, e.what());
22
%exception ThrowByReference() {
26
SWIG_exception(SWIG_DivisionByZero, e.what());
30
%csnothrowexception NoThrowException() {
40
void ThrowByValue() { throw Ex("ThrowByValue"); }
41
void ThrowByReference() { throw Ex("ThrowByReference"); }
42
// %csnothrowexception
43
void NoThrowException() { throw Ex("NoThrowException"); }
44
// exception specifications
45
void ExceptionSpecificationValue() throw(Ex) { throw Ex("ExceptionSpecificationValue"); }
46
void ExceptionSpecificationReference() throw(Ex&) { throw Ex("ExceptionSpecificationReference"); }
47
void ExceptionSpecificationString() throw(const char *) { throw "ExceptionSpecificationString"; }
48
void ExceptionSpecificationInteger() throw(int) { throw 20; }
51
// test exceptions in the default typemaps
53
// null reference exceptions
55
void NullReference(Ex& e) {}
56
void NullValue(Ex e) {}
61
enum TestEnum {TestEnumItem};
62
void ExceptionSpecificationEnumValue() throw(TestEnum) { throw TestEnumItem; }
63
void ExceptionSpecificationEnumReference() throw(TestEnum&) { throw TestEnumItem; }
67
%include "std_string.i"
69
void ExceptionSpecificationStdStringValue() throw(std::string) { throw std::string("ExceptionSpecificationStdStringValue"); }
70
void ExceptionSpecificationStdStringReference() throw(const std::string&) { throw std::string("ExceptionSpecificationStdStringReference"); }
71
void NullStdStringValue(std::string s) {}
72
void NullStdStringReference(std::string &s) {}
75
// Memory leak check (The C++ exception stack was never unwound in the original approach to throwing exceptions from unmanaged code)
76
%exception MemoryLeakCheck() {
77
Counter destructor_should_be_called;
81
SWIG_exception(SWIG_DivisionByZero, e.what());
88
Counter() { count++; }
89
~Counter() { count--; }
91
int Counter::count = 0;
93
void MemoryLeakCheck() {
94
throw Ex("testing memory leaks when throwing exceptions");
98
// test exception pending in the csconstruct typemap
101
constructor(std::string s) {}
102
constructor() throw(int) { throw 10; }
106
// test exception pending in the csout typemaps
107
%typemap(out, canthrow=1) unsigned short ushorttest %{
109
if ($result == 100) {
110
SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "don't like 100");
115
unsigned short ushorttest() { return 100; }
118
// test exception pending in the csvarout/csvarin typemaps and canthrow attribute in unmanaged code typemaps
119
%typemap(check, canthrow=1) int numberin, int InOutStruct::staticnumberin %{
121
SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too small");
125
%typemap(out, canthrow=1) int numberout, int InOutStruct::staticnumberout %{
128
SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too big");
138
static int staticnumberin;
139
static int staticnumberout;
141
int InOutStruct::staticnumberin;
142
int InOutStruct::staticnumberout;
145
// test SWIG_exception macro - it must return from unmanaged code without executing any further unmanaged code
146
%typemap(check, canthrow=1) int macrotest {
148
SWIG_exception(SWIG_IndexError, "testing SWIG_exception macro");
152
bool exception_macro_run_flag = false;
153
void exceptionmacrotest(int macrotest) {
154
exception_macro_run_flag = true;
158
// test all the types of exceptions
159
%typemap(check, canthrow=1) UnmanagedExceptions {
161
case UnmanagedApplicationException: SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "msg"); return $null; break;
162
case UnmanagedArithmeticException: SWIG_CSharpSetPendingException(SWIG_CSharpArithmeticException, "msg"); return $null; break;
163
case UnmanagedDivideByZeroException: SWIG_CSharpSetPendingException(SWIG_CSharpDivideByZeroException, "msg"); return $null; break;
164
case UnmanagedIndexOutOfRangeException: SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "msg"); return $null; break;
165
case UnmanagedInvalidOperationException: SWIG_CSharpSetPendingException(SWIG_CSharpInvalidOperationException, "msg"); return $null; break;
166
case UnmanagedIOException: SWIG_CSharpSetPendingException(SWIG_CSharpIOException, "msg"); return $null; break;
167
case UnmanagedNullReferenceException: SWIG_CSharpSetPendingException(SWIG_CSharpNullReferenceException, "msg"); return $null; break;
168
case UnmanagedOutOfMemoryException: SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "msg"); return $null; break;
169
case UnmanagedOverflowException: SWIG_CSharpSetPendingException(SWIG_CSharpOverflowException, "msg"); return $null; break;
170
case UnmanagedSystemException: SWIG_CSharpSetPendingException(SWIG_CSharpSystemException, "msg"); return $null; break;
171
case UnmanagedArgumentException: SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException, "msg", "parm"); return $null; break;
172
case UnmanagedArgumentNullException: SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "msg", "parm"); return $null; break;
173
case UnmanagedArgumentOutOfRangeException: SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, "msg", "parm"); return $null; break;
177
enum UnmanagedExceptions {
178
UnmanagedApplicationException,
179
UnmanagedArithmeticException,
180
UnmanagedDivideByZeroException,
181
UnmanagedIndexOutOfRangeException,
182
UnmanagedInvalidOperationException,
183
UnmanagedIOException,
184
UnmanagedNullReferenceException,
185
UnmanagedOutOfMemoryException,
186
UnmanagedOverflowException,
187
UnmanagedSystemException,
188
UnmanagedArgumentException,
189
UnmanagedArgumentNullException,
190
UnmanagedArgumentOutOfRangeException,
193
void check_exception(UnmanagedExceptions e) {
197
// exceptions in multiple threads test
198
%exception ThrowsClass::ThrowException(long long input) {
201
} catch (long long d) {
203
sprintf(message, "caught:%lld", d);
204
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, message, "input");
210
ThrowsClass(double d) : dub(d) {}
211
long long ThrowException(long long input) {