0

All code : https://pastebin.com/k7TZzQEY

/*If i remove try catch runtime error appear.*/ 
try
    {
        SnowAdventure.DodajUplatu(nisvet, trening_1, uplata_1, "trening");
        SnowAdventure.DodajUplatu(nisvet, trening_2, uplata_2, "trening");
        SnowAdventure.DodajUplatu(nisvet, trening_3, uplata_3, "trening");
        SnowAdventure.DodajUplatu(nisvet, trening_1, oprema_nm, "oprema");
        SnowAdventure.DodajUplatu(nisvet, trening_3, oprema_NM, "oprema");

    }
    catch (const std::exception& obj)
    {
        obj.what();

    }
/*Function*/
void DodajUplatu(Ucenik & ucenik, Trening & trening, Uplata & uplata, string svrha) {
        for (int i = 0; i < _treninzi.size(); i++)
        {
            for (int j = 0; j < _treninzi[i].getUcenici().size(); j++)
            {
                if (*_treninzi[i].getUcenici()[j] == ucenik) {
                    if (svrha == "oprema") {
                        int size = _treninzi[i].getUcenici()[j]->getIznajmljenaOprema()->size();
                        bool uplaceno = false;
                        for (int k = 0; k < size; k++)
                        {
                            Oprema * oprema = &(*_treninzi[i].getUcenici()[j]->getIznajmljenaOprema())[k];
                            if (oprema->getCijena() >= uplata.getIznos()) {
                                _treninzi[i].getUcenici()[j]->DodajUplatu(uplata, svrha);
                                uplaceno = true;
                                return;
                            }
                        }
                        if (!uplaceno) {
                            throw exception("Niste uplatili!");
                            return;
                        }
                    }
                    else
                    {
                        if (trening.getTipTreninga() == Pocetnicki) {
                            if (uplata.getIznos() == trening.getBrojCasova() * 10) {
                                _treninzi[i].getUcenici()[j]->DodajUplatu(uplata, svrha);
                                return;
                            }
                            else {
                                throw exception("Niste uplatili dovoljno novca!");
                            }
                        }
                        else if(trening.getTipTreninga()==Rekreativni)
                        {
                            if (uplata.getIznos() == trening.getBrojCasova() * 15) {
                                _treninzi[i].getUcenici()[j]->DodajUplatu(uplata, svrha);
                                return;
                            }
                            else {
                                throw exception("Niste uplatili dovoljno novca!");
                            }
                        }
                        else
                        {
                            if (uplata.getIznos() == trening.getBrojCasova() * 20) {
                                _treninzi[i].getUcenici()[j]->DodajUplatu(uplata, svrha);
                                return;
                            }
                            else {
                                throw exception("Niste uplatili dovoljno novca!");
                            }
                        }
                    }
                }
                throw exception("Nepoznat ucenik koji je zaprimljen kao parametar!");
            }
        }

    }
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Please **[edit]** your question with an [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Aug 22 '17 at 14:16
  • What's the error? – Cid Aug 22 '17 at 14:21
  • Briefly, What error you get? – Raindrop7 Aug 22 '17 at 14:23
  • abort() has been called – Arnel Maric Aug 22 '17 at 14:24
  • when i remove try catch block from code,with try catch it works perfectly – Arnel Maric Aug 22 '17 at 14:25
  • Sounds like you need to print `obj.what();` so that you know what the exception is so you can fix it. Just doing `catch (const std::exception& obj){ obj.what(); }` throws out thew exception. – NathanOliver Aug 22 '17 at 14:27
  • `std::cerr << obj.what() << '\n';` would seem a pretty obvious thing to do. What you have right now seems rather pointless unless the intention was to (a) catch the exception, (b) purposely *avoid* knowing what it was, and (c) throw it out; i.e. ignore it. as you're *throwing* all those exceptions throughout that code, it stands to reason you have interest in knowing what was thrown. So why ignore it? – WhozCraig Aug 22 '17 at 14:42

1 Answers1

2

cppreference.com says following:

Once the exception object is constructed, the control flow works backwards (up the call stack) until it reaches the start of a try block, at which point the parameters of the associated catch blocks are compared with the thrown expression to find a match. If no match is found, the control flow continues to unwind the stack until the next try block, and so on. If a match is found, the control flow jumps to the matching catch block (the exception handler), which executes normally.

As the control flow moves up the call stack, destructors are invoked for all objects with automatic storage duration constructed since the corresponding try-block was entered, in reverse order of construction. If an exception is thrown from a constructor, destructors are called for all fully-constructed non-static non-variant members and base classes. This process is called stack unwinding.

Explanation

abort() has been called when i remove try catch block from code

It means if your code throws an exception (function DodajUplatu does), and this exception isnt handled in try/catch block (it isnt if you remove it), an abort() - Runtime error is called.

with try catch it works perfectly

Yes because this exception was caught in catch block.


You should read about exceptions to get extended info. Or better read some C++ book.

kocica
  • 6,412
  • 2
  • 14
  • 35