-
March 13th, 2009, 05:16 AM
#1
try catch(...) problem in VC++
Hello everyone,
I have a VS solution with a C# windows application and a VC++ dll which exports a function.
In that function (from the dll) I have a try catch block like this:
try{
//do something
int a =0;
int b = 100/a;
//do something else
catch(...)
{
//Log this exception
}
the prblem is that the exception is caught from the C# application. The dll (which exports a function) is imported in the C# application like this:
[DllImport("Huffman", EntryPoint = "StartToArchive")]
private static extern void StartToArchive(string from, string to, StatusCallBackDelegate statusCallback);
Do you have any ideas how can the exception (error division by 0) can be managed by the try catch block from the C++ dll without throwing an error in C#?
This code is just an example. I don't look for answers like test before the third line if a != 0.
The C++ dll is unmanaged code.
Last edited by lunguIonut; March 13th, 2009 at 05:21 AM.
Reason: Forgot to say.
-
March 13th, 2009, 09:21 AM
#2
Re: try catch(...) problem in VC++
Try-catch doesn't catch the division by zero exception. For that you need to use structured exception handling (SEH).
From MSDN:
Code:
BOOL SafeDiv(INT32 dividend, INT32 divisor, INT32 *pResult)
{
__try
{
*pResult = dividend / divisor;
}
__except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
return FALSE;
}
return TRUE;
}
The C++ dll is unmanaged code.
Since when? I'm just kidding...
-
March 17th, 2009, 01:06 AM
#3
Re: try catch(...) problem in VC++
Thanks for your replay,
I wasn't clear enough. By unmanaged C++ dll I mean that when I declare my class I do not do it like this :"ref class MyClass{...". I do not declare my class as reference class. I read something like this in a book about C++/CLI where they use .Net objects in a C++ windows application. If I am wrong please correct me
And I don't want to use any additional libraries like MFC.
Related to the __try / __except I found another problem: "Cannot use __try in functions that require object unwinding". So if I am using any predefined objects (i.e. a fstream) or instances of my classes in this SEH it will not compile. Is there any way to solve this problem?
I try to make something like this:
void MyClass::myFunction()
{
__try
{
//place all my code here
//including new objects of my classes or predefined classes and other static methods
//defined by me
}
__except(.........)
}
I want to do something like try / catch in C# where Exception catches all errors. I try to make the Dll independent from the C# code, so I can use it later in any other application without worrying about any unexpected exception.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|