• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Last post wins!

include <exception> // or #include <stdexcept>
#include <iostream>

struct QuitNow: public std::exception
{
QuitNow() { }
virtual ~QuitNow() throw() { }
virtual const char* what() throw()
{
return "QuitNow: request normal termination of this thread.\n"
"(You should not see this message. Please report it if you do.)";
}
};

using namespace std;

void loopy( unsigned count )
{
if (count < 20)
{
cout << "count = " << count << endl;
loopy( count +1 );
}
else
{
cout << "Throwing QuitNow.\n";
throw QuitNow();
}
}

int main()
{
try
{
cout << "Starting.\n";
loopy( 0 );
cout << "You shouldn't see this.\n";
}
catch (QuitNow)
{
cout << "Caught it!\n";
}
catch (...)
{
cout << "Something went wrong...I WIN!\n";
}
cout << "Good-bye.\n";
return 0;
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones