Thread: Exceptions

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Exceptions

    Hi~

    I understand that there is an exception throwing mechansim in C++. I was experimenting with it the other day - but can I use it to check for random exceptions, i.e. "An unexpected error has occured."? Because otherwise I'd have to have an idea of every error that could ever happen...

    As I cannot create a random error, I'm asking you guys about the theory of it. What does it do?

    Thanks ~

    Chris Howarth

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Segmentation faults? I'm not sure, it probably depends on your compiler.

    In-program exceptions? Yes. The catch(...) construct catches all exceptions.
    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.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    88
    If you want my opinion, your ultimate goal should be to never write a program that prints an error message like "An unexpected error has occurred." Be specific. In the battle of debugging, error messages make all the difference. To answer your question: yes, this is possible if you employ some good design patterns (there is no such thing as a "random exception"). I hope someone at Microsoft is reading this forum...

    >Segmentation faults? I'm not sure, it probably depends on your compiler.
    Segfaults aren't random, either. If you use good programming constructs, use bounds checking when possible, and check all new allocations, you can eliminate almost all segfaults.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    5
    That said, C++'s exception mechanism kind of sucks. I mean, it works, and if you know how to use it right, it can do some very good things for your code, but there are a few things that you have to keep in mind.

    In C++, anything can be thrown as an exception. Strings, ints, enums, you name it, you can throw it. What this means, though, is that it's not possible to catch all exceptions and get any information out of them. The only way to catch all possible exceptions is to use the catch(...) construct, and since you can't do anything with the "..." in there, the best you can do is output "an exception occurred in the function," which really isn't all that useful. One way around this is to create your own Exception class and insist that every person on your team use it exclusively.

    The other problem is that C++ doesn't require you to declare whether your function throws an exception or not. This means that the only way to know for sure if a function throws exceptions or not is to scour through every dependency and search for the "throw" keyword. The way around this problem is to either a) not work on any particularly big projects, or b) put a comment at the top of every method that throws an exception, including ones simply call methods without catching their exceptions.

    Add this to the inherent issues with exceptions (mainly code being skipped in seemingly arbitrary locations), and you have a pretty savage beast.

    My point isn't, "don't use exceptions," (I mean, the project I'm on uses them all over the place), but rather, if you're going to use them, make sure everyone on your project knows pretty well what they're doing.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I suggest to have a look at this link to get an idea about the different opinions about exceptions (there are 2 parties: pro & con), booth with good arguments

    http://www.scottcollins.net/articles/exceptions.html

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    5
    Good article. It does a good job explaining why exception-based programming is good (and does not actually cause cancer), but it doesn't really address my issues with C++'s implementation in particular.

    My main problems are a) no base exception class, and b) no required "throws" keyword in function definitions. These are two things that Java gets right, IMO, and really make exception-based programming a lot easier.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by froobly View Post
    no required "throws" keyword in function definitions. These are two things that Java gets right, IMO, and really make exception-based programming a lot easier.
    I don't know java very well. why does "throw" make things easier? you see the throw and you are able to handle all exceptions because you know ... erm ... hm ... oh ... nothing? you have to read the docu to know what to catch and how to get information out of the thing you catched? like as in c++? and code without docu can even be written from scratch (imo)
    please tell me if I'm wrong.
    Last edited by pheres; 05-15-2007 at 01:34 AM.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    5
    Quote Originally Posted by pheres View Post
    I don't know java very well. why does "throw" make things easier? you see the throw and you are able to handle all exceptions because you know ... erm ... hm ... oh ... nothing? you have to read the docu to know what to catch and how to get information out of the thing you catched? like as in c++? and code without docu can even be written from scratch (imo)
    please tell me if I'm wrong.
    What I like about the mandatory "throws" keyword isn't that it gives me specific information about how to handle an exception, but that it protects against stealth exceptions -- exceptions that are being thrown a few function calls down, but that you have no indication they exist. The "throws" keyword doesn't tell you how to handle the exceptions, but it does tell you 100% of the time whether or not they exist.

    What Java does do that helps you handle exceptions when you have no idea what you're doing, is provide a base Exception class, that all exceptions derive from. This class contains a string, so at the very least, if all else fails, you can tell the user what went wrong. There's no such thing as an unknown exception, because every exception knows at some level what it is.

    Anyway, sorry to have turned this into a bit of a holy war. I don't mean to be some kind of Java zealot. It's just that I really don't like the way C++ does exceptions.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    ok, I got what you mean. I hope one of the c++ gurus in here is going to answer your question (and tell us why the world is much better without exception base classes and throw keyword )

    The only war I'm in is the war of learning

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I like about the mandatory "throws" keyword isn't that it gives me specific information about how to handle an exception, but that it protects against stealth exceptions -- exceptions that are being thrown a few function calls down, but that you have no indication they exist.
    As noted, C++ has the catch (...) construct for such "catch-all" exception handling. C++ simply does not have checked exceptions.

    The "throws" keyword doesn't tell you how to handle the exceptions, but it does tell you 100% of the time whether or not they exist.
    Considering the existence of RuntimeException in Java, I doubt if that really is the case. Its existence would certainly tell you if an exception may be thrown, but its absence would not tell you that an exception cannot be thrown.

    What Java does do that helps you handle exceptions when you have no idea what you're doing, is provide a base Exception class, that all exceptions derive from. This class contains a string, so at the very least, if all else fails, you can tell the user what went wrong. There's no such thing as an unknown exception, because every exception knows at some level what it is.
    std::exception would be the C++ near equivalent. All exceptions throwable in the standard library are derived from it. If I do need to write my own exception classes, I subclass an appropriate class in the std::exception hierarchy. The what() member function of std::exception returns a string that should describe what went wrong.
    Last edited by laserlight; 05-15-2007 at 03:47 AM.
    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

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Always document in prose the exception specification of a function.

    Always document also the exception guarantee of a function:
    http://www.boost.org/more/generic_exception_safety.html
    The article does not only apply to generic components.
    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

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    OK, thanks.
    your ultimate goal should be to never write a program that prints an error message like "An unexpected error has occurred."
    Of course, yes, but it would be better to have an error message alerting the user if an error did occur, rather than the program crashing.

    Thankyou! ~
    Chrishowarth

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by UMR_Student View Post
    If you want my opinion, your ultimate goal should be to never write a program that prints an error message like "An unexpected error has occurred." Be specific. In the battle of debugging, error messages make all the difference. To answer your question: yes, this is possible if you employ some good design patterns (there is no such thing as a "random exception"). I hope someone at Microsoft is reading this forum...
    So what if you're writing an app that loads external functionality from plug-ins? What if a badly-written plug-in throws some weird exception that you didn't plan for? Sure, you can CATCH the exception without knowing its type, but what are you going to DO with it? The most informative message you could issue would be "The plug-in XYZ has caused an error."

    In the real world code works in conjunction with other code you have little or no control over.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The plug-in XYZ has caused an error.
    Which is still better than "An unexpected error has occurred." At least the end user knows who to blame.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are Exceptions effective?
    By Petike in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2008, 12:23 AM
  2. Intercepting Fortran exceptions
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2008, 09:13 AM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  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