_matherr does not get called when built into a DLL

I have a basic solution file (.sln) where I was able to reproduce a problem I have been facing recently.

It contains 3 projects: 1.) MathTest.lib - containing methods that might cause a mathematical error, like acos(1.1). 2.) MathTestDll.dll - calls the methods from the above lib. 3.) UnitTest.exe - calls the exported method in the DLL that should cause the error.

What I'm trying to do is fairly simple: The following code contains the _matherr() routine and should ideally link fine. The call to acos() with a value of 1.1 is invalid (invalid input) and should cause an error which should be handled by the implemented _matherr() handler. I hope I'm right about the behavior of _matherr(). Please let me know.

MathTest.lib

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "MathTest.h" 
#include <iostream> 
#include <math.h> 
 
int _matherr(_exception* _Except) 
{ 
    std::cout << _Except->name; 
    return -1; 
} 
 
void MathTest::ThrowMatherr(float par) 
{ 
    float result = acos(par); 
    std::cout << result; 
} 


This 'ThrowMatherr()' method will be called by the DLL as follows:
MathTestDll.dll

1
2
3
4
5
void MatherrCaller::CauseMatherr() 
{ 
    MathTest* mathtest = new MathTest(); 
    mathtest->ThrowMatherr(1.1); 
} 


which is then exported as:

1
2
3
4
5
6
7
extern "C" __declspec(dllexport) void CallThisToCauseMatherr(); 
 
void CallThisToCauseMatherr() 
{ 
    MatherrCaller* caller = new MatherrCaller(); 
    caller->CauseMatherr(); 
} 


This exported method will be called by a simple test.

UnitTest.exe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h> 
 
typedef void (*METHODTOCALL)();  
 
int main() 
{ 
    HMODULE module = LoadLibrary((LPCSTR)"..\\Debug\\MatherrTestDll.dll"); 
    if(module != NULL) 
    {  
        METHODTOCALL ProcAdd = (METHODTOCALL) GetProcAddress(module, (LPCSTR)"CallThisToCauseMatherr");  
        if (NULL != ProcAdd) 
        { 
            (ProcAdd)(); 
        } 
 
        FreeLibrary(module);  
    } 
 
    return 0; 
} 


All methods get called fine. But the acos() method which has been passed invalid input never calls the _matherr() error handler. Please let me know how I can fix this.

I had to make the question detailed to get my point through. Please don't mind.
Topic archived. No new replies allowed.