Thread: Why throw exceptions?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Why throw exceptions?

    We're learning about throwing exceptions in Deitel's book. I get the concept ok, but don't see the value of it.
    The typical example is catching an attempt to divide by zero. Why not just test for that, or whatever, in an if statement or similar. Maybe call another function if need be, without the bother of try, throw and catch? It seems more work than needed. I must be missing something, but what? Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Patience!

    You will find situations later on that would be a little awkward to handle in other ways.

    I'd give you an example, but I suspect it might confuse you!!! Wait until you understand dynamic class constructors, you'll find situations where exception handling will really simplify your coding.

    Stick with understanding the concept - always good advice anyway.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Ok, you write a math library.. it does some division, and it checks for the 0 divide. The programmer made an error obviously, you shouldn't be able to divide by 0. So what do you do? Print an error message to console and quit the program? The purpose of exceptions is to decouple error finding and error handling.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 09:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. Unhandled exceptions (throw with no try)
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2006, 06:31 AM
  5. causing default exceptions to throw additional info
    By lightatdawn in forum C++ Programming
    Replies: 14
    Last Post: 08-06-2003, 06:39 PM