William,
Thanks for the reply.
Yes, I have defined a throws clause in all my h files and swig is
handling this part correctly by added the try/catch for these custom
exceptions in the C++ shim code. (Therefore, I am not using
%exception.)
I have read the sections you mention and I understand how swig makes the
callbacks/delegates for its exceptions.
However, I still do not understand how to make swig use this same
pattern to create the callbacks/delegates for my custom exceptions. Can
you point me to an example of this?
Thanks,
David
-----Original Message-----
From: William S Fulton [mailto:***@fultondesigns.co.uk]
Sent: Monday, January 10, 2005 6:17 PM
To: Dave Hoffer
Cc: ***@cs.uchicago.edu; ***@ubs.com
Subject: Re: [Swig] How to treat a C++ Exception as a C#
System.Exception?
Post by Dave HofferWilliam wrote...
.Yes, there is a mechanism to convert C++ exceptions into C#
exceptions.
Post by Dave Hoffer.This is documented and the best place to read about this is in the
.Java.html chapter, but note the differences documented in
CSharp.html.
Post by Dave Hoffer.There is one caveat and that is the SWIG exception logic is flawed.
When
.a C# exception is thrown, the C++ destructors are not always called
as
Post by Dave Hoffer.the exception chain is unwound. The solution is to have a completely
.different exception handling mechanism which is currently under
.development.
I read the Java docs on exception handling but I am having trouble
understanding how to get my C++ propagated to C#. I have C++
exceptions
Post by Dave Hofferlike this...
Exceptions...
InstrumentException (derives from Exception)
SARException (derives from InstrumentException)
SCException (derives from InstrumentException)
In my .i file I have added...
%typemap(csbase) InstrumentException "System.Exception";
%typemap(csbase) SARException "InstrumentException";
%typemap(csbase) SCException "InstrumentException";
%include "InstrumentException.h"
%include "SARException.h"
%include "SCException.h"
The C++ shim code does...
try {
(arg1)->init();
}
catch(SARException &_e) {
(void)_e;
SWIG_CSharpThrowException(SWIG_CSharpException, "C++ SARException
Exception thrown");
}
catch(SCException &_e) {
(void)_e;
SWIG_CSharpThrowException(SWIG_CSharpException, "C++ SCException
exception thrown");
}
It seems to me the C# wrapper code registers the following method and
calls it when any of my C++ exceptions are thrown.
static void ThrowSystemException(string message) {
throw new System.SystemException(message);
}
What am I doing wrong? How do I get it to call my exceptions?
You havn't provided your source code. If your methods have a C++
exception specification then the generated exception handling code comes
from the 'throws' typemap. If you are using %exception then the code
comes from %exception. Make sure you read the Features chapter:
http://www.swig.org/Doc1.3/Customization.html#Customization and the
chapter on typemaps. If you want to convert your C++ exception into a C#
exception, you need to follow the same pattern that SWIG uses which is
all in the generated code - a delegate is mapped onto a C callback.
William
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig