0

I have a function that uses boost::bind to move function evaluation into a try/catch wrapper, based on this question.

Problem is, boost::bind doesn't seem to work with SEH - and worse, it returns a garbage value, which is exactly what I don't want. Have I made a botch of it somewhere? Even stranger, the switch /EHsc or /EHa doesn't actually seem to matter to the behavior of the program. The output you get is:

Calling
-2147483648
Done.
Press any key to continue . . .

How do I even start to figure out where things have gone wrong?


#pragma once
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <windows.h>
#include <eh.h>


using boost::function;
using boost::bind;


int potato(int q) 
{
    double r = 0;
    return q / r;
}

template<typename T>
T exception_wrapper(boost::function<T()> func)
{
    try 
    {
        std::cout << "Calling" << std::endl;
        return func();
        std::cout << "After Call" << std::endl;
    }
    catch (...) {
        std::cout << "Ex Caught" << std::endl;
        return 45;
    }
}

void trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
{
    throw std::runtime_error("huh");
}

int main() 
{
    int result = exception_wrapper<int>(bind(potato, 123));
    std::cout << result << std::endl;
    std::cout << "Done." << std::endl;
}
Carbon
  • 3,828
  • 3
  • 24
  • 51
  • Are you expecting `return q / r;` to throw an exception? – NathanOliver Oct 02 '17 at 18:44
  • Yeah, specifically, it should generate a SEH error, trapped by the dots. – Carbon Oct 02 '17 at 18:50
  • Just covering all the bases: If you replace `return func();` with `return 1.0/0.0;` does it then generate and catch an exception? I'm not sure what all SEH does but I know as far as the standard is concerned it is UB – NathanOliver Oct 02 '17 at 18:53
  • 1
    @ProbablyAStupidQuestion this code looks a lot like the Microsoft example when looking up the /EH compiler option. Are your boost libraries compiled with the same option as you code above? – Anon Mail Oct 02 '17 at 19:05
  • OH now there's a good question, anon. Let me see. – Carbon Oct 02 '17 at 20:00
  • Unfortunately, both bind and function are header-only. – Carbon Oct 02 '17 at 20:05
  • 1
    Floating point (as opposed to integral) division by zero usually doesn’t trap. Are you sure that division operation actually causes an SEH exception? – T.C. Oct 02 '17 at 20:14
  • TC, you're right. Double R should have been int r. I'm an idiot. Closing. – Carbon Oct 02 '17 at 20:21

0 Answers0