boost-ext / di

C++14 Dependency Injection Library
https://boost-ext.github.io/di
1.16k stars 140 forks source link

Cannot catch exception from submodule #389

Closed JanValiska closed 4 years ago

JanValiska commented 5 years ago

Hello, i don't know if i use modules correctly, but anyway in my use case the exception is thrown by constructor in module and cannot be caught(terminate is called instead).

Minimal example

#include <iostream>                             
#include "di.hpp"                               
namespace di = boost::di;                       

struct Foo {                                    
  Foo() { throw std::runtime_error("No way"); } 
};                                              

struct Bar {                                    
  Bar(Foo& foo) {}                              
};                                              

int main(int argc, char *argv[]) {              
  try {                                         
    auto module1 = []() -> di::injector<Foo&> { 
      return di::make_injector();               
    };                                          

    auto module2 = [&]() -> di::injector<Bar&> {
      return di::make_injector(module1());      
    };                                          

    auto injector = module2();                  
    injector.create<Bar&>();                    
  } catch (...) {                               
    std::cerr << "Never caught" << std::endl;   
  }                                             

  return 0;                                     
}

Run result:

terminate called after throwing an instance of 'std::runtime_error'
  what():  No way
[1]    2568 abort (core dumped)  ./test

Excepted behavior

No terminate is called and exception should be catched by catch block.

Specifications

kanstantsin-chernik commented 4 years ago

The PR is merged. You are welcome to check.