Discussion:
How to treat a C++ Exception as a C# System.Exception?
Dave Hoffer
2005-01-08 04:13:43 UTC
Permalink
My C++ code has exception classes that all derive from Exception; these are all marshaled to C# with the same names. However C# requires that all exceptions derive from System.Exception.

How can I make my swig generated exceptions 'know' that the C++ Exception class should be the same as C#'s System.Exception?

P.S. Maybe I don't know how exceptions should be handled with swig. I assumed that my C++ exceptions are valid (and marshaled) to C# so I can use then in try -catch blocks.

-dh
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
m***@quantifisolutions.com
2005-01-10 02:03:35 UTC
Permalink
Post by Dave Hoffer
P.S. Maybe I don't know how exceptions should be handled with swig. I
assumed that my C++ exceptions are valid (and marshaled) to C# so I can
use then in try -catch blocks.
It's not quite that simple. There is no way to "marshal" exceptions across an
unmanaged/managed boundary. With the MS CLR and VC++ compiler for your
unmanaged DLL, *at best* the unmanaged exception would be treated as a
SEHException in C#, which means you will lose semantic info. If you are using
any other CLR, then all bets are off as there is no common exception handling
infrastructure similar to Windows SEH.

SWIG surmounts this by implementing a callback mechanism to allow you to pass
back an exception code and message. The SWIG-generated C# code uses this to
determine that an exception occurred on the C++ side and to throw a
corresponding C# exception with the message provided. There is a default
mapping between typical C++ exceptions and C# exceptions, although this is
something you can customize.

Attached is an article about .NET interop that covers exception propagation and
other topics.





_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
m***@quantifisolutions.com
2005-01-10 02:10:53 UTC
Permalink
Here is the link...

http://www.jprl.com/~jon/interop.html
Post by m***@quantifisolutions.com
Attached is an article about .NET interop that covers
exception propagation and other topics.
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
W***@ubs.com
2005-01-10 09:09:46 UTC
Permalink
-----Original Message-----
Sent: 10 January 2005 02:04
Subject: [Swig] How to treat a C++ Exception as a C# System.Exception?
Post by Dave Hoffer
P.S. Maybe I don't know how exceptions should be handled
with swig. I
Post by Dave Hoffer
assumed that my C++ exceptions are valid (and marshaled) to
C# so I can
Post by Dave Hoffer
use then in try -catch blocks.
It's not quite that simple. There is no way to "marshal"
exceptions across an
unmanaged/managed boundary. With the MS CLR and VC++ compiler for your
unmanaged DLL, *at best* the unmanaged exception would be treated as a
SEHException in C#, which means you will lose semantic info.
If you are using
any other CLR, then all bets are off as there is no common
exception handling
infrastructure similar to Windows SEH.
SWIG surmounts this by implementing a callback mechanism to
allow you to pass
back an exception code and message. The SWIG-generated C#
code uses this to
determine that an exception occurred on the C++ side and to throw a
corresponding C# exception with the message provided. There
is a default
mapping between typical C++ exceptions and C# exceptions,
although this is
something you can customize.
Attached is an article about .NET interop that covers
exception propagation and
other topics.
Yes, there is a mechanism to convert C++ exceptions into C# exceptions. 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.

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 the exception chain is unwound. The solution is to have a completely different exception handling mechanism which is currently under development.

William

Visit our website at http://www.ubs.com

This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses. The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission. If
verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.

_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
Dave Hoffer
2005-01-10 20:28:35 UTC
Permalink
William wrote...

.Yes, there is a mechanism to convert C++ exceptions into C# exceptions.
.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.

.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
.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
like 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?

-dh
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
William S Fulton
2005-01-10 23:16:54 UTC
Permalink
Post by Dave Hoffer
William wrote...
.Yes, there is a mechanism to convert C++ exceptions into C# exceptions.
.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.
.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
.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
like 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
Dave Hoffer
2005-01-12 22:03:16 UTC
Permalink
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 Hoffer
William 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 Hoffer
like 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
William S Fulton
2005-01-12 23:29:40 UTC
Permalink
Post by Dave Hoffer
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.)
Sounds like your methods are declared with exception specifications if
this is being added automatically. You can customise the code being
generated with the 'throws' typemap (use the default ones in csharp.swg
as a starting point).
Post by Dave Hoffer
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?
No examples that I know of. As this approach is about to be made
redundant, I'm reluctant to go and construct one. However, you need to
initiate the callback to the managed world (via a C callback mapped to a
C# delegate). You make the call to your callback by modifying the
'throws' typemap mentioned above to do this. So you have to add some
code into the _wrap.cxx file and add some C# delegate code somewhere (eg
into the intermediary class). The default ones use

%pragma(csharp) imclasscode=%{ ... %}
and
%insert(runtime) %{ ... %}

but you can see all this in csharphead.swg and examine the generated
code in the generated code for any example as this default exception
handling code is always generated.

William
Post by Dave Hoffer
Thanks,
David
-----Original Message-----
Sent: Monday, January 10, 2005 6:17 PM
To: Dave Hoffer
Subject: Re: [Swig] How to treat a C++ Exception as a C#
System.Exception?
Post by Dave Hoffer
William 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 Hoffer
like 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
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
Dave Hoffer
2005-01-13 13:15:22 UTC
Permalink
Thanks, I will look into this.

-dh

-----Original Message-----
From: William S Fulton [mailto:***@fultondesigns.co.uk]
Sent: Wednesday, January 12, 2005 6:30 PM
To: Dave Hoffer
Cc: ***@cs.uchicago.edu
Subject: Re: [Swig] How to treat a C++ Exception as a C#
System.Exception?
Post by Dave Hoffer
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.)
Sounds like your methods are declared with exception specifications if
this is being added automatically. You can customise the code being
generated with the 'throws' typemap (use the default ones in csharp.swg
as a starting point).
Post by Dave Hoffer
I have read the sections you mention and I understand how swig makes
the
Post by Dave Hoffer
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
Post by Dave Hoffer
you point me to an example of this?
No examples that I know of. As this approach is about to be made
redundant, I'm reluctant to go and construct one. However, you need to
initiate the callback to the managed world (via a C callback mapped to a

C# delegate). You make the call to your callback by modifying the
'throws' typemap mentioned above to do this. So you have to add some
code into the _wrap.cxx file and add some C# delegate code somewhere (eg

into the intermediary class). The default ones use

%pragma(csharp) imclasscode=%{ ... %}
and
%insert(runtime) %{ ... %}

but you can see all this in csharphead.swg and examine the generated
code in the generated code for any example as this default exception
handling code is always generated.

William
Post by Dave Hoffer
Thanks,
David
-----Original Message-----
Sent: Monday, January 10, 2005 6:17 PM
To: Dave Hoffer
Subject: Re: [Swig] How to treat a C++ Exception as a C#
System.Exception?
Post by Dave Hoffer
William 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 Hoffer
like 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
Post by Dave Hoffer
from the 'throws' typemap. If you are using %exception then the code
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#
Post by Dave Hoffer
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

Loading...