flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > c++, terminate called after throwing an instance of

Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 09 Jul 2018, 14:08
Something strange with exceptions. I can catch exception if it is raised in the same object, but can't if it's raised in a different object.

main.cpp

Code:
#include <iostream>

#include <string>

#include <stdexcept>


#include "highscore.h"

using namespace std;

Highscore gHighscore;

int main(int argc, char *argv[]){

    try {
        gHighscore = Highscore();

        // throw runtime_error(string("BLABLA"));

    } catch (const runtime_error &exc) {
        // cout << exc.what();
        cout << "Runtime_error caught\n";
        return 1;
    } catch (...) {
        cout << "Unknown exception caught\n";
    }

}    


highscore.h

Code:
#ifndef __HIGHSCORE_H__
#define __HIGHSCORE_H__

#include <string>
#include <stdexcept>

using namespace std;

class Highscore
{
 private:
    string Name;

 public:
    Highscore();
    string getName();
    void setName(string theName);
    //getHighscore
    //getStats
    //sendSessionResult
};

#endif // __HIGHSCORE_H__
    


highscore.cpp

Code:
#include "highscore.h"

Highscore::Highscore()
{
    throw runtime_error(string("LETS TRY THIS"));
}

string Highscore::getName() {
    return "LATER";
}

void Highscore::setName(string theName) {

}    


error:

Quote:

terminate called after throwing an instance of 'std::runtime_error'
what(): LETS TRY THIS
3 [main] byki_i_korovy 3592 cygwin_exception::open_stackdumpfile: Dumping
stack trace to byki_i_korovy.exe.stackdump
Post 09 Jul 2018, 14:08
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 09 Jul 2018, 14:36
i get it.

Highscore::Highscore() gets called before main even starts, since there is no new.
Post 09 Jul 2018, 14:36
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 09 Jul 2018, 14:41
hm, this doesn't exactly works...

Highscore* gHighscore;

*gHighscore = new Highscore();
Post 09 Jul 2018, 14:41
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 671
vivik 09 Jul 2018, 15:42
Highscore* gHighscore = NULL;

gHighscore = new Highscore();

perfect.

sorry for creating those stupid threads, I thought I was going to stuck on this. I'll reuse this one for next questions.
Post 09 Jul 2018, 15:42
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.