geekbrains_oop_cpp/lesson_8/exercise_1.hpp

41 lines
591 B
C++
Raw Normal View History

2021-07-14 21:29:27 +00:00
#ifndef EXERCISE_1_HPP_
#define EXERCISE_1_HPP_
class DivisionByZero
{
private:
std::string m_message;
public:
DivisionByZero(std::string message = "") : m_message(message) {}
const std::string& getMessage() const { return m_message; }
};
namespace my
{
template <typename T>
double div(T a, T b)
{
if (b == 0)
throw DivisionByZero("Деление на ноль!");
return a / b;
}
}
void exercise_1()
{
try
{
double result = my::div(10, 0);
std::cout << result << std::endl;
}
catch (const DivisionByZero& e)
{
std::cerr << e.getMessage() << std::endl;
}
}
#endif