Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <exception> // biblioteka "z wyjątkami"
- using namespace std;
- int fun(int a, int b); // prototyp funkcji
- int main()
- {
- int dzielna;
- int dzielnik;
- cout << "podaj dzielna: "; // wiadomo
- cin >> dzielna;
- cout << "podaj dzielnik: ";
- cin >> dzielnik;
- try
- {
- cout << fun(dzielna, dzielnik) << endl; // wywołujemy funkcję, jeśli dzielnik będzie równy 0 to funkcja "fun" rzuci wyjątkiem
- }
- catch (exception ex) // w tym miejscu przechwytujemy wyjątek (nadajemu mu nazwę "ex")
- {
- cout << "Error: " << ex.what() << endl; // wyświetlamy wyjątek (samo ex.what() wystarczy ale dodałem Error żeby wyglądało very profeszynal)
- }
- system("pause");
- return 0;
- }
- int fun(int a, int b)
- {
- if (b == 0) throw exception("Dzielnik rowny 0!!!"); // na początku działania funkcji sprawdzamy, czy dzielnik jest równy 0, jeśli jest, to "rzucamy wyjątkiem", który informuje nas, że zaszło dzielenie przez 0
- return a / b; // zwracamy wynik dzielenia w wypadku gdy dzielnik nie jest równy 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement