When throwing exceptions in a class, is it the programmer's job to ensure this exception is caught in the class? Or is it best practice to assume this exception will be caught in the main file / runner program? I believe it is the latter, but I'm somewhat unsure.... take the following code:
#include <stdexcept>
#include <iostream>
using namespace std; // supposedly trustworthy duck
class Duck
{
public:
Duck(int quack_nominee);
private:
int quack_strength;
};
Duck::Duck(int quack_nominee)
{
if (quack_nominee < 0)
{
throw runtime_error("A quack having a negative value produces undefined behavior on reality.");
}
quack_strength = quack_nominee;
}
Is the above code good practice? I'm assuming the runtime_error thrown will be caught somewhere. If the programmer using my class did not implement a try-catch block, the .exe file will crash (at least on my IDE and compiler).
Also, if I should have a try-catch block in the class itself, is it even possible to legally implement this?