Thread: Are Exceptions effective?

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Are Exceptions effective?

    Hi all,
    I have just learned a lot about "Exceptions" (from Bruce Eckel's book, by the way). But I have read that I should not use them very often because they are very difficult to processing, thus little effective.
    And now, I am confused. It is so nice and clear to use Exceptions in the code BUT they are "time-consuming".
    So should I use them? Or should I use them everywhere, or somewhere, or should I try to avoid them?

    Thanks.
    Petike

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    I am glad you asked because I wanted to know that too. Sorry no answer from me.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I have just learned a lot about "Exceptions" (from Bruce Eckel's book, by the way).
    O_o

    Bruce Eckel
    So... not much at all then...

    But I have read that I should not use them very often because they are very difficult to processing, thus little effective.
    If you need an exception mechanism, then a compiler will likely do a better job then you ever could, and if you can do a better job, you should build a better compiler. You shouldn't use exceptions as part of controlling the flow of a program, but if any particular source benefits from what exceptions provide then you should definitely use exceptions. C++ exceptions are extremely effective at what they do, but they are not "return values".

    It is so nice and clear to use Exceptions in the code BUT they are "time-consuming".
    From a mechanical perspective, they are only expensive when raised. (In practice this depends on the compiler, some compilers are simply better than others.)

    So should I use them?
    Yes.

    Or should I use them everywhere, or somewhere, or should I try to avoid them?
    No. Yes. No.

    *shrug*

    The best thing to do: contrive examples and write test cases where some employ exceptions and some do not. And of course, you can always ask for help with specific cases.

    Soma

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Short answer: It depends. Long answer, that is what they were invented for. They sort of break the way a program flows a bit. Because of this, they typically carry a bit more overhead than using status returns or something similar. Use them when you find they are most effective. Just don't get overly lazy and use them exclusively. They do exist for a reason. At the very least, they can be used during development and removed later. I typically do not write code in this fashion but its another issue of preference. Its equivilent to leaving ASSERT() in a production build.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    At the very least, they can be used during development and removed later. I typically do not write code in this fashion but its another issue of preference. Its equivilent to leaving ASSERT() in a production build.
    There is a thin line between assertions and exceptions of the std::logic_error variety, and so for such exceptions I agree, since at least in theory these exceptions can be avoided. Exceptions of the std::runtime_error variety, on the other hand, are rather different from assertions since the exceptional course of events might well be unavoidable due to external factors.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use MFC for many of my systems, which throws a lot of exceptions.
    (I use the right 'tool' for the job at hand, irregardless of its reputation.)

    I catch exceptions in most methods. In many cases the exception is due to bad data captured from a device. Catching the exception at least gives my app the chance to recover.

    I write exceptions to a log, always including the class::method. This allows easier investigation of issues.

    I rarely process any exceptions, except timeouts (as I may want to attempt the operation again with extended TO).

    Many of my apps (asset protection systems) run in remote/unaccessable/harsh locations and 24/7 , so this aids in locating problem areas in the code (when exposed to real world conditions/data).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  2. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM