Thread: Why exceptions???

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Why exceptions???

    I´ve been reading a litte about exception handling and if I understand it right it is used when something goes "wrong". For example when a stack is full the stack could throw an exception. But cannot this be accomplished in another way??? Couldn´t the same problem be solved by returning a bool value (false if stack full otherwise true). Also it is more expensive,in terms of CPU-power, to handle expections then return-values. From my point of view return-values are "better", or am I totaly wrong ???

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    With careful programming you can almost eliminate the need for throwing exceptions. Take dynamic memory allocation for example:
    Code:
    int* ptr = new int[ 100 ];
    
    if( ptr == NULL )
    {
         cout << "Memory allocation failed" << endl;
    }
    or

    Code:
    try
    {
         int* ptr = new int[ 100 ];
    }
    catch( bad_alloc ex )
    {
    cout << ex.what( ) << endl;
    }
    Both accomplish the same thing, although I think the standard says that when new fails it should throw a bad_alloc exception. Maybe someone will confirm that for me...
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    They are good for a number of things......firstly, constructors.....as you cant return a value to inform of an error from a constructor, and exception is sometimes the only way for a class to say if the construction failed.

    Also, assume that you are building a library for working with files.....your classes sit between the file system and the programmer....but if there is a problem (say a corrupt or missing file) then its the programmer's responsibility to deal with the error - not your library (Hell if it was your responsibility, who would need programmers )..your library should simply pass the error from the filesystem to the programmer...and rethrowing an exception is a good way of doing it

    Both accomplish the same thing, although I think the standard says that when new fails it should throw a bad_alloc exception. Maybe someone will confirm that for me...
    Yup...but many compilers dont implement this and only allow the NULL return (I think VC++6 is guilty here).....

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Don't know what VC++ does, but new is a C++ keyword which throws an exception on failure. If MS VC++ is not throwing an exception, then VC++ isn't implementing C++ properly.

    From C++ reference:

    ...

    If successful, new returns a pointer to the allocated memory. By default, an allocation failure (such as insufficient or fragmented heap memory) results in the predefined exception bad_alloc being thrown. Your program should always be prepared to catch the bad_alloc exception before trying to access the new object (unless you use a new-handler).

    A request for allocation of 0 bytes returns a non-null pointer. Repeated requests for zero-size allocations return distinct, non-null pointers.

    ...
    Last edited by Davros; 10-15-2002 at 07:24 AM.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Fordy
    Yup...but many compilers dont implement this and only allow the NULL return (I think VC++6 is guilty here).....
    Could be right. I tested it on VC++ 6 and after 2 or 3 go's with 1000000000 or so ints, I got a warning about low virtual memory. Even though it was being deleted
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Davros
    Don't know what VC++ does, but new is a C++ keyword which throws an exception on failure. If MS VC++ is not throwing an exception, then VC++ isn't implementing C++ properly.
    Yup...old news....M$ says that VC++6 was shipped before the standard was final..Its C++ complience can be found a little weak in places....

    But there are many other compilers out there which do a better job for standard C++ & the stl - The 2 I use for this stuff are Codewarrior and Mingw - both of those seem pretty complient....

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    i think it is fixed .net though

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    int* p1 = new int[ MUCH ]; //throws an exception
    
    #include <new>
    int* p2 = new(nothrow) int[ MUCH ];  //returns (int*)0
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Why exceptions???

    Originally posted by ripper079
    Also it is more expensive,in terms of CPU-power, to handle expections then return-values. From my point of view return-values are "better", or am I totaly wrong ???
    It depends.

    I originally wrote my Omicron project using return values/error flags. To get safe error-handling, the code was a mess.
    I decided to convert to exceptions.
    Now the code is very beautiful (well... at least not as bad) and easy to maintain.
    Exception has advantages, speed may not be one of them (although CodeWarrior use the term "zero-oeverhead exceptions")
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    In an OO world, surely exceptions are the better option....
    Couldn't think of anything interesting, cool or funny - sorry.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Originally posted by Fordy

    Yup...but many compilers dont implement this and only allow the NULL return (I think VC++6 is guilty here).....
    Shocking! Always use the best style for the situation. Sometimes easy to maintain code is key (exceptions are easy to maintain between a large group of programmers), other times your goal is lightning fast speed (possibly no exceptions). There are a couple of ways to eliminate the need for exceptions. A solid memory manager will virtually eliminate memory related errors. Initializing all pointers to NULL is also a step in the right direction. The bottom line is care will reduce the possibility of errors.

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