CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: exception handling

  1. #1
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    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.

    Ok,
    * 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; January 12th, 2009 at 04:01 AM.
    "What comes around, goes around"

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: exception handling

    * 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.

    B - AFAIK, stack unwinding occured during exception has thrown and stack memory was released too but not heap.

    D - To ensure polymorphic behaviors too
    E - Correct, To catch a dividebyzero exception, you need to write a dividebyzero class.

    Hope help.
    Thanks for your help.

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: exception handling

    Quote Originally Posted by coletek View Post
    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.

    Ok,
    * 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.
    A : Assuming hierarchy refers to inheritance of exception classes, then it is correct, you can catch specifically, or generally.
    B : Incorrect, the compiler unwinds the stack, objects in the stack frame(s) being popped are responsible for releasing any resources (including dynamic allocations) they own when their destructors are called.
    C : Incorrect, if the exception has been thrown dynamically, then it is the responsibility of the catcher to release the memory allocation - but they probably don't know that. When you catch by pointer, then you (unless you have good documentation) don't know whether the exception has been dynamically allocated (in which case you should delete it, otherwise you have a resource leak), or if the exception is merely the address of a global stack object (in which case attempting to delete it will most likely yeild undefined behaviour). What a dilemma! The thrower never has to worry about tidying up the exception since the thrower is destroyed before the catch statement is reached.
    D : Correct
    E : Incorrect, you can write an exception handler to catch any exception using catch(...). If you follow good guidelines, then any exception classes you write will ultimately inherit from std::exception, in which case, without knowing the name of your class, your exception can be caught with catch(std::exception& e).

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: exception handling

    Quote Originally Posted by coletek View Post
    * D is correct, because it you just have its value you can't free its reference.
    You should never attempt to delete/free any object passed to you by reference, for one, you don't know whether it has been malloced, new'ed or is a stack object. Even a class object can be constructed in space allocated by malloc (using the placement new operator). Secondly, attempting to delete/free a reference is completely wrong - doing so will most likely end in undefined behaviour!

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: exception handling

    D is correct because catching by value may invoke a copy constructor, and that could throw an exception itself, leading to a call to terminate().
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)