Thread: Throwing an exception in int main()

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Throwing an exception in int main()

    I noticed that I don't have to explicitly catch an exception thrown in the main() function. I just throw it, and what was thrown is outputted to screen, and the program is automatically terminated prematurely.

    Why is this?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An uncaught exception is required by the standard to result in stack unwinding (e.g. all local variables or objects are cleaned up for all call functions) and program termination.

    Presumably your implementation (compiler, library, etc) also causes relevant output to occur. The standard does not require that, so such behaviour is not guaranteed with other implementations.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by grumpy View Post
    An uncaught exception is required by the standard to result in stack unwinding and program termination.
    Yes to termination, but whether stack unwinding happens is implementation-defined. (IIRC, GCC will unwind, VC++ won't.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    Yes to termination, but whether stack unwinding happens is implementation-defined. (IIRC, GCC will unwind, VC++ won't.)
    Not true. The standard requires stack unwinding (or, more precisely, an equivalent observable effect of invoking destructors for all auto variables in sequence). If VC++ does not do that, then VC++ is not standard-compliant.
    Last edited by grumpy; 04-02-2011 at 04:20 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    From [except.handle]p9:
    If no matching handler is found, the function std::terminate() is called; whether or not the stack is unwound before this call to std::terminate() is implementation-defined (15.5.1).
    I don't make claims about the standard without first looking things up.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Programmer_P View Post
    I noticed that I don't have to explicitly catch an exception thrown in the main() function. I just throw it, and what was thrown is outputted to screen, and the program is automatically terminated prematurely.

    Why is this?
    Because C++ is not like Java. There is never a requirement in C++ to catch exceptions, if you don't catch it, your program terminates.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thanks guys. I appreciate the info.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Mozza314 View Post
    There is never a requirement in C++ to catch exceptions, if you don't catch it, your program terminates.
    Maybe your programs do, mine crash if i don't catch a thrown exception!
    Devoted my life to programming...

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Crashing is just one form of terminating.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by CornedBee View Post
    Crashing is just one form of terminating.
    ... Oh, ok. I just figured that "termination" means "user-friendly program exit".
    Devoted my life to programming...

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    All my main() functions look the same:
    Code:
    int main(int argc, char** argv) 
    {
    	using namespace std;
    	try {
    		return main_program(argc,argv);
    	}
    	catch (runtime_error& e) {
    		cerr << RED << "Run-time error : " << e.what() << NORMAL << endl;
    	}
    	catch (bad_alloc&) {
    		statusFailed();
    		cerr << RED << "Out of memory." << NORMAL << endl;
    	}
    	catch (exception& e) {
    		cerr << RED << "Exception : " << e.what() << NORMAL << endl;
    	}
    	catch (...) {
    		cerr << RED << "Unknown error" << NORMAL << endl;
    	}
    	return 1;
    }
    Then main_program takes the role of the main function.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Issue?
    By matthewlane in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 05:55 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM