Why cannot an exception be thrown in a constructor?

I have this test code for handling exceptions in constructors. the f () function creates a division of the exception by zero, but this exception does not fall. Instead, if I throw a custom integer, an exception is thrown.

#include <iostream>
using namespace std;

class A
{
public:
  void f(){
    int x;
    x=1/0;
    //throw 10;
  }

 A(){
   try{
     f();
     }
     catch(int e){
       cout << "Exception caught\n";
       }
   }
 };

int main (int argc, const char * argv[])
{

   A a;
  return 0;
}

why can I catch the custom of throwing 10; not x = 1/0;

+3
source share
2 answers

, , ++. , , . - ( ), . , .

, , ( ) .

class A
{
    public:
         void f()
         {
             int x;
             //For illustration only
             int a = 0;
             if(a == 0)
                  throw std::runtime_error( "Divide by zero Exception"); 
             x=1/a;
         }

         A()
         {
              try
              {
                   f();
              }
              catch(const std::runtime_error& e)
              {
                   cout << "Exception caught\n";
                   cout << e.what(); 
              }
         }
 }; 
+7

++.

: ++: , ++ () .

+4

All Articles