/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab7/task_3.cpp
67 строк
2 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include <iostream> #include <string> class LoggerGuard { private: std::string message; std::ostream& out; public: // ��������� �ਭ����� ᮮ�饭�� � ��⮪ �뢮�� LoggerGuard(const std::string& message, std::ostream& out = std::cout) : message(message), out(out) {} // �������� - ��뢠���� �� ��室� �� ������ �������� ~LoggerGuard() { out << message << "\n"; } // ����塞 ��������� �������� � ������ ��ᢠ������, // �⮡� �������� ���।��������� ��������� LoggerGuard(const LoggerGuard&) = delete; LoggerGuard& operator=(const LoggerGuard&) = delete; }; // �ਬ�� �ᯮ�짮����� (��� ��������樨) int SomeFunction() { return 0; } int SomeOtherFunction() { return 1; } int FinalFunction() { return 2; } int Function() { LoggerGuard logger("Function completed"); int value = 1; try { value = SomeFunction(); if (value == 0) { return value; // ����饭�� ��⮬���᪨ �뢥����� �� ��室� } value = SomeOtherFunction(); if (value == 0) { return value; // ����饭�� ��⮬���᪨ �뢥����� �� ��室� } value = FinalFunction(); // might throw an exception } catch (...) { throw; // ����饭�� ��⮬���᪨ �뢥����� �� ��室� �१ �᪫�祭�� } return value; // ����饭�� ��⮬���᪨ �뢥����� �� ��ଠ�쭮� �����襭�� } // ��������樮���� �㭪�� � �᪫�祭��� int FunctionWithException() { LoggerGuard logger("FunctionWithException completed"); throw std::runtime_error("Something went wrong!"); } int task_3() { std::cout << "=== ���� ��ଠ�쭮�� �믮������ ===" << '\n'; Function(); std::cout << "\n=== ���� � �᪫�祭��� ===" << '\n'; try { FunctionWithException(); } catch (const std::exception& e) { std::cout << "������� �᪫�祭��: " << e.what() << '\n'; } return 0; }