Thread: exception handling

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    In a cave on Mars

    exception handling

    Which of the following statements describe correct methods of handling C++ exceptions?

    A. In a hierarchy of exception classes, the order of handling exceptions can be from the most specific class to the most general class.
    B. Once an exception is thrown, the compiler unwinds the heap, freeing any memory dynamically allocated within the block from which the exception was thrown.
    C. If an exception is caught by its address or pointer, it is the responsibility of the thrower to release the memory occupied by the exception.
    D. Catching an exception by reference is preferable to catching it by value.
    E. To write an exception handler, it is essential to know the concrete class of exception to catch.

    * A is correct, because ya can do exception handling anywhere.
    * B is incorrect, becuase you need to free the memory yourself when you do a try/catch
    * C is correct, because of what I said about B
    * D is correct, because it you just have its value you can't free its reference.
    * E - unsure, I don't understand the answer.

    Can someone please confirm/comment? Thx.
    Last edited by coletek; 01-12-2009 at 03:01 AM.
    "What comes around, goes around"

  2. #2
    The larch
    Join Date
    May 2006
    A. Wrong reason. Can you catch more than one exception type at one level?

    try {
    catch (std::bad_alloc&) {
    catch (std::exception& {
    C. Wrong answer and wrong reason. (In practice you probably wouldn't do that anyway.)


    #include <iostream>
    #include <stdexcept>
    int main()
        std::runtime_error err("Oh");
        try {
            throw &err;
        catch (std::exception* e) {
            std::cout << e->what();
    D. The explanation makes no sense. Why would you prefer passing objects to functions by reference? What about hierarchies of exception objects?

    E. Can you write a catch block that catches an exception, if you don't know whether a std::bad_alloc or std::range_error (or any other exception is thrown).
    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).

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    E. To write an exception handler, it is essential to know the concrete class of exception to catch.
    This is basically stating that you can only catch concrete exceptions, not abstract ones.

    What do you think -- is this true?

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  3. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  5. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM