Throw catch question

This is a discussion on Throw catch question within the C++ Programming forums, part of the General Programming Boards category; When an exception is thrown, how does the program know where to go? I mean, if the catch block catching ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Throw catch question

    When an exception is thrown, how does the program know where to go? I mean, if the catch block catching the exception would have been in the same function, the throw would have been just like using a goto to the right catch block. But when there is no catch block in the same function, the compiler can't know which catch block that will catch the exception (if any at all), so the program has to figure it out in run time. But how does it do that? Does it store somewhere which type (an id) has been thrown, and looks for a catch block which catches that type?

    Also, I've heard that when scopes are left due to that an exception has been thrown, all local variables are destructed. This means that new code is executed, before reaching the catch block. But what if a new exception is thrown in the mean time?

    And one final question, when throwing exceptions, are there some standard exception classes you should use, or do you have to create your own?
    Come on, you can do it! b( ~_')

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by TriKri View Post
    When an exception is thrown, how does the program know where to go? I mean, if the catch block catching the exception would have been in the same function, the throw would have been just like using a goto to the right catch block. But when there is no catch block in the same function, the compiler can't know which catch block that will catch the exception (if any at all), so the program has to figure it out in run time. But how does it do that? Does it store somewhere which type (an id) has been thrown, and looks for a catch block which catches that type?
    It's called Stack Unwinding. It keeps moving up the stack until it finds an appropriate catch block.

    Quote Originally Posted by TriKri View Post
    Also, I've heard that when scopes are left due to that an exception has been thrown, all local variables are destructed. This means that new code is executed, before reaching the catch block. But what if a new exception is thrown in the mean time?
    If 2 exceptions are thrown, std::terminate() is immediately called and the program dies.
    I believe this can only happen if a destructor throws an exception (which is why destructors should never throw exceptions).

    Quote Originally Posted by TriKri View Post
    And one final question, when throwing exceptions, are there some standard exception classes you should use, or do you have to create your own?
    There are standard exceptions, such as the ones in the <stdexcept> header, but you are free to create your own if none of them suite your needs.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's called Stack Unwinding. It keeps moving up the stack until it finds an appropriate catch block.
    But if there is no handler for the exception, it might not unwind anything and just call terminate?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unhandled exceptions (throw with no try)
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2006, 07:31 AM
  2. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 10:41 PM
  3. Parent/child object design
    By lyx in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2003, 09:46 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 03:40 PM

Tags for this Thread


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