is there a reason that most/all try-catch examples are only using void-sub-functions for the throw statement

50 Views Asked by At

all around I am seeing sample code like this (not tested for compile) on the try-except and throw statement as present in most c++ implementations:

void subfunction(int a)
{
  throw;
}
void function(int a)
{
  try
  {
    subfunction(a)
  }
  catch(...)
  {
  }
}

I am now wondering if there is any good reason why the equivalent of function() or subfunction() is (nearly) always in this samples realized as a void in this samples. is there a particular reason or impact with respect to the three mentioned special c++ keywords? what would the implication on warnings like "function is missing a return statement", "not all control paths return a value" and likes be? is the keyword "throw" somewhere internally label'ed as noreturn or similar (as can be found on "exit()" for some compilers)?

PSI i am currently on MSVS 2012 with according MSVC but I am using other compilers as well such as GNUC in its various current versions.

1

There are 1 best solutions below

0
On BEST ANSWER

throw can be used in non void-function.

throw "has" the noreturn attribute so should not provoke warning about "not all control paths return a value" for code similar to

double my_div(double a, double b)
{
    if (b == 0.) {
         throw std::runtime_error("division by zero");
         // No warning here
    } else {
         return a / b;
    }
}