Catching exception

This is a discussion on Catching exception within the C++ Programming forums, part of the General Programming Boards category; I'm throwing exceptions that seem impossible to catch... see this code: Code: #include <iostream> using namespace std; int main() { ...

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    Catching exception

    I'm throwing exceptions that seem impossible to catch... see this code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	cout << "Starting." << endl;
    
    	try
    	{
    		cout << "In a hopeless situation." << endl;
    		throw;
    	}
    	catch(...)
    	{
    		cout << "What a miserable little existance..." << endl;
    	}
    
    	cout << "My life as a program ends now." << endl;
    
    	return 0;
    }
    Gives me:
    Code:
    Starting.
    In a hopeless situation.
    
    abnormal program termination
    Why? I thought catch(...) caught any exception thrown? Why miss a blank one? If I replace my throw; with throw 1; or anything else (provide something to be thrown) it will catch. I'm new to exceptions... but this code is almost directly off the flipcode tutorial. Where have I gone wrong?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,405
    The problem is that a throw without any operand rethrows the exception. As such you can only use that type of throw inside a catch statement
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    throw; doesn't throw a blank exception. It will re-throw the current exception and thus should be used only within a catch block. (Of course, it could be in a function that is called from within a catch block, so the compiler will have a hard time warning you against incorrect usage.)
    Re-throwing a non-existent exception causes terminate() to be called. (See 15.1.7 in the C++ standard.) The default behaviour of terminate() (i.e. when no different handler was installed using set_terminate()) is to call abort() in turn, which immediately kills the program.
    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
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I see... as far as I can the tell, the flipcode article must've been incorrent. Regardless, I understand now. Thanks for the help!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You mean this?
    Empty Throws

    So, we can see now how powerful and useful this "new" method is. A try-catch block can contain another try-catch block, and this block can throw anything, too, which will be caught by the outer catch if there is no corresponding inner catch, or the catch throws something. One thing I have neglected to mention is that throw need not even have anything after it:
    Code:
    try
    {
      throw;
    }
    catch(...)
    {
      cout << "Caught exception!" << endl;
    }
    This could be used in extreme situations where you don't even care what was thrown.
    You're right, it's wrong. (The catch statement's right, though. You actually can have a catch that looks like that, which will catch any exception.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 08:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 12:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21