-1

I have the following code:

int main()
{
    Client *c = new Client();
    ExitOnlyWhenClientException();
    return 0;
}

So I don't want main to return only when client throws exception. I know I can use condition variable, which signal in client's destructor and wait in main block.

But Is there another method?

I've found this link How would you implement a basic event-loop?

My question here is a little different compared to above link, since it shoud know client crashed.

EDIT 1

I made a mistake to say crash, which I really mean exception.

Community
  • 1
  • 1
Aylwyn Lake
  • 1,919
  • 4
  • 26
  • 36
  • 3
    `Client` seems to be a class. A class doesn't crash. A program crashes. Therefore if the code in the `Client` class causes a crash, the whole program goes down. Unless you are talking about exceptions, which are a different matter. – SirDarius Dec 13 '13 at 11:09
  • Which platform? The concept of an event loop is likely in the 1st chapter of any basic tutorial. @SirDarius: It's pretty obvious he means "when Client causes a crash". – tenfour Dec 13 '13 at 11:14
  • Why no just have a run method on the client, call it and then you'll have to wait til it finished, either via a crash or cleanly – doctorlove Dec 13 '13 at 11:16
  • @SirDarius: Yes, I mean client throwes exception. – Aylwyn Lake Dec 13 '13 at 12:28

1 Answers1

0

Let's replace Client with another class type:

int main()
{
    std::string *s = new std::string();
    ExitOnlyWhenStringException();
    return 0;
}

Now, std::string::at() could throw an exception if you tried to access an out-of-bound element. And in fact, since *s is empty, any index will be out of bounds. However, you are not calling s->at(8) or any other method. So, the only possible exception is a std::bad_alloc from new. You won't need to handle that (Any reasonable implementation will allow you to crate at least one object dynamically).

MSalters
  • 173,980
  • 10
  • 155
  • 350