Does it make any sense to have a throw statement in a function without a catch? I found it in a solution set, but I'm getting "terminating with uncaught exception of type int" when I run the program, so I suspect it's wrong.
1 2 3 4 5 6 7 8 9
double nFacRobust(unsignedint n) {
if (n > NFAK_LIMIT) {
throw n;
}
else {
return (nFac(n)); //just a function calculating n factorial
}
}
> Does it make any sense to have a throw statement in a function without a catch?
That's the usual thing to do.
A function doing the throwing by definition can't deal with the error, so catching is pointless.
> but I'm getting "terminating with uncaught exception of type int"
Well somewhere, you need a catch in the call hierarchy all the way back to main.
The "uncaught" bit means that either you had no try/catch anywhere, or those try/catches you do have don't catch int exceptions.
Bingo. So long as you document your code properly, so that whoever is going to call on your functions knows that they need to be ready to catch exceptions, and you document what can be thrown and what it means, good to go.
#include <iostream>
constunsigned NFAK_LIMIT = 100;
unsigned nFac(unsigned n)
{
unsigned return_value = 0;
// do the factorial stuff and get an actual factorial
return return_value;
}
unsignedint nFacRobust(unsignedint n)
{
if (n > NFAK_LIMIT)
{
throw n;
}
else
{
return (nFac(n));
}
}
int main()
{
unsigned some_big_number = 125;
try
{
nFacRobust(some_big_number);
}
catch (...)
{
// dealing with the consequences of too big a number for your function to deal with
std::cout << "Hey, you wanted a bigger factorial than I can handle!\n";
}
}
Hey, you wanted a bigger factorial than I can handle!
If I were doing this I'd restructure it a bit so the error checking is done within the nFac() function:
#include <iostream>
constunsigned NFAK_LIMIT = 100;
unsigned nFac(unsigned n)
{
unsigned return_value = 0;
if (n > NFAK_LIMIT)
{
throw n;
}
else
{
// do the factorial stuff and get an actual factorial
}
return return_value;
}
int main()
{
unsigned some_big_number = 125;
try
{
nFac(some_big_number);
}
catch (...)
{
// dealing with the consequences of too big a number for your function to deal with
std::cout << "Hey, you wanted a bigger factorial than I can handle!\n";
}
}
#include <iostream>
constunsigned NFAK_LIMIT = 100;
unsigned nFac(unsigned n)
{
unsigned return_value = 0;
if (n > NFAK_LIMIT)
{
throw n;
}
else
{
// do the factorial stuff and get an actual factorial
}
return return_value;
}
int main()
{
unsigned some_big_number = 125;
try
{
nFac(some_big_number);
}
catch (unsigned e)
{
// dealing with the consequences of too big a number for your function to deal with
std::cout << "Hey, " << e << " would create a bigger factorial than I can handle!\n";
}
}
Hey, 125 would create a bigger factorial than I can handle!