Quantcast

throwing C# exceptions from C

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

throwing C# exceptions from C

Gonzales, Jeff

Hi everyone,

 

I have SWIGged some C code and am successfully using it with my C# app.  Can anyone send me some examples of how I can throw C# exceptions from the C code that I can catch in my app?

 

Thanks,

Jeff


------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwing C# exceptions from C

Andrew Haining

C doesn’t support exceptions, if you pass the c++ flag and the exceptions flag to swig it should generate exception handling code if I’m not mistaken, but the exceptions would have to be thrown from c++, not c.

Hope that helps,

Andy.

 

From: Gonzales, Jeff [mailto:[hidden email]]
Sent: 24 October 2016 21:41
To: [hidden email]
Subject: [Swig-user] throwing C# exceptions from C

 

Hi everyone,

 

I have SWIGged some C code and am successfully using it with my C# app.  Can anyone send me some examples of how I can throw C# exceptions from the C code that I can catch in my app?

 

Thanks,

Jeff


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________



Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. 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. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwing C# exceptions from C

Andrew Haining

Hey Jeff, yeah you can use C style error codes and either throw exceptions in the C# when you get one or write SWIG typemaps to throw exceptions when getting error codes. The SWIG documentation (http://www.swig.org/Doc1.1/HTML/Exceptions.html#n2) talks about using longjmp to implement C exceptions but I personally think that’s a terrible idea. You can also write small C++ functions callable from the C which will throw C++ exceptions right through to the C#. The simplest way is probably to use C style error codes all the way, even in the C#, this is the simplest approach but will probably be frowned upon by C# devs. I would personally just call a C++ throw function from the C which would propagate up to the C#.

Cheers,

Andy.

 

From: Gonzales, Jeff [mailto:[hidden email]]
Sent: 25 October 2016 13:40
To: Andrew Haining <[hidden email]>
Subject: RE: throwing C# exceptions from C

 

Hi Andy,

 

Thanks for the rapid response.  If I had to use C, could I just catch errors in the normal “C” way (errors as return values, etc.) and use SWIG to propagate them up to C# as exceptions?  If not, can I generate a SWIG wrapper that doesn’t have any of that exception code in it?

 

Thanks,

Jeff

 

From: Andrew Haining [mailto:[hidden email]]
Sent: Tuesday, October 25, 2016 3:21 AM
To: Gonzales, Jeff <[hidden email]>; [hidden email]
Subject: RE: throwing C# exceptions from C

 

C doesn’t support exceptions, if you pass the c++ flag and the exceptions flag to swig it should generate exception handling code if I’m not mistaken, but the exceptions would have to be thrown from c++, not c.

Hope that helps,

Andy.

 

From: Gonzales, Jeff [[hidden email]]
Sent: 24 October 2016 21:41
To: [hidden email]
Subject: [Swig-user] throwing C# exceptions from C

 

Hi everyone,

 

I have SWIGged some C code and am successfully using it with my C# app.  Can anyone send me some examples of how I can throw C# exceptions from the C code that I can catch in my app?

 

Thanks,

Jeff


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

 


Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. 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. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________



Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. 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. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwing C# exceptions from C

William S Fulton
On 25 October 2016 at 13:55, Andrew Haining
<[hidden email]> wrote:

> Hey Jeff, yeah you can use C style error codes and either throw exceptions
> in the C# when you get one or write SWIG typemaps to throw exceptions when
> getting error codes. The SWIG documentation
> (http://www.swig.org/Doc1.1/HTML/Exceptions.html#n2) talks about using
> longjmp to implement C exceptions but I personally think that’s a terrible
> idea. You can also write small C++ functions callable from the C which will
> throw C++ exceptions right through to the C#. The simplest way is probably
> to use C style error codes all the way, even in the C#, this is the simplest
> approach but will probably be frowned upon by C# devs. I would personally
> just call a C++ throw function from the C which would propagate up to the
> C#.
>
You should not throw C++ exceptions across any C boundary.

Exceptions are documented in the C# chapter at
http://swig.org/Doc3.0/CSharp.html#CSharp_exceptions. The exceptions.i
library and SWIG_exception is a target language neutral method to
throw exceptions and is easy to use.

William

------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwing C# exceptions from C

Andrew Haining
"You should not throw C++ exceptions across any C boundary."
I don't agree, there's nothing wrong with throwing through C or no exception C++ as long as you know you've cleaned up all your exceptionless resources between the try and catch before you throw.

Having looked at the exceptions.i implementation, I agree with William, while it doesn't behave like an exception until it hits the target language, it is non-intrusive and should provide a level of correctness in the C you couldn't achieve without modifying it otherwise.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of William S Fulton
Sent: 29 October 2016 13:12
To: Andrew Haining <[hidden email]>
Cc: Gonzales, Jeff <[hidden email]>; [hidden email]
Subject: Re: [Swig-user] throwing C# exceptions from C

On 25 October 2016 at 13:55, Andrew Haining <[hidden email]> wrote:

> Hey Jeff, yeah you can use C style error codes and either throw
> exceptions in the C# when you get one or write SWIG typemaps to throw
> exceptions when getting error codes. The SWIG documentation
> (http://www.swig.org/Doc1.1/HTML/Exceptions.html#n2) talks about using
> longjmp to implement C exceptions but I personally think that’s a
> terrible idea. You can also write small C++ functions callable from
> the C which will throw C++ exceptions right through to the C#. The
> simplest way is probably to use C style error codes all the way, even
> in the C#, this is the simplest approach but will probably be frowned
> upon by C# devs. I would personally just call a C++ throw function
> from the C which would propagate up to the C#.
>
You should not throw C++ exceptions across any C boundary.

Exceptions are documented in the C# chapter at http://swig.org/Doc3.0/CSharp.html#CSharp_exceptions. The exceptions.i library and SWIG_exception is a target language neutral method to throw exceptions and is easy to use.

William

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com ______________________________________________________________________

________________________________
Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. 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. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: throwing C# exceptions from C

Vadim Zeitlin-13
On Mon, 31 Oct 2016 08:44:40 +0000 Andrew Haining <[hidden email]> wrote:

AH> "You should not throw C++ exceptions across any C boundary."
AH> I don't agree

 You may agree or not, but doing this is undefined behaviour and results in
big problems (i.e. crashes) in practice under some Unix platforms,
including Linux, unless C code was compiled using special compiler flags,
which is not the case by default.

 Regards,
VZ

------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

attachment0 (203 bytes) Download Attachment
Loading...