exceptions in c++

This is a discussion on exceptions in c++ within the C++ Programming forums, part of the General Programming Boards category; after a few years of c++, I'm taking my first class in Java - data structures and algorithms 3 to ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    exceptions in c++

    after a few years of c++, I'm taking my first class in Java - data structures and algorithms 3 to be exact. Anyways, right at the begining we are learning about excpetions. I find it to be a really great concept, and I know that it exists in c++, but I have never used it thus far.

    any ideas, opinions as to why c++ exceptions are almost never used and certainly not tought in school?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    IMO: exceptions allow you to recover from mistakes, but don't have anything to do with the flow of the program itself. you can write a perfectly functional program without use of exceptions with the assumption that all input will be perfect. exceptions make the program more robust, but not more functional.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well, thats the exact point of exceptions - to handle errors on the user side

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    True enough, but they don't help me as a programmer solve a problem. I can write the program to solve the problem first and then go back and add exceptions where appropriate if I want to. For me, and many other folks apparently, that's good enough. If I ever wanted to release my work to the public or write code professionally, then I would try to make the program as user friendly as possible, and exceptions would be much more important; but exceptions won't help me solve the problem in the first place. It's sort of like writing writing help files--useful, but not very glamorous.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well, thats precisely it...in the "real world" of programming, the program itself is the samllest part of the whole; the GUI comes next, and the biggest part is the error handling...but this conversation still does not answer my orignal question.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    In Java, exceptions are everywhere. The standard library makes very extensive use of them and the compiler enforces their correct handling.

    In C++, they're far more rare. Some language constructs, namely new and dynamic_cast, can throw exceptions. new throws in an out-of-memory situation (very rare nowadays) and dynamic_cast when you try an invalid cast on a reference (rare too). So there's no NEED to learn exceptions.
    Further along, std::bitset and I think std::locale throw under some circumstances. You can also tell the streams to throw exceptions.

    Why your school didn't teach you about them I don't know. Probably because there are still quite a few C++ programmers who dislike exceptions, for various reasons.

    One reason why they're not often used is that there still are compilers out there which don't properly support them or impose a very high performance overhead for their use (or actually support, which means you have to give the compiler a flag to gain advantage from not using them).

    But I've seen exceptions being used quite a bit.
    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

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by elad
    IMO: exceptions allow you to recover from mistakes, but don't have anything to do with the flow of the program itself.
    exceptions can have alot to do with the flow of a program... if you're trying to open a file and it doesn't open, you can throw an exception and skip the rest of the code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>...if you're trying to open a file and it doesn't open, you can throw an exception and skip the rest of the code...

    true enough, but I assume you skip the code and go back to try to open the file again, as the point of the program at the time the exception was thrown was to try to open the file and you really want to do something in the code you skipped. The exception only allows you to try to recover from the error encountered rather than terminating the program outright or skipping the code permanently, not in any way solve the problem the program is designed to solve.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I think exceptional handling is pretty important in C++. I'm not exactly sure why it isn't taught in some schools, but I'll take a guess. Usually homework assignments are not massive projects. So it's not really important to have good error handling. However, I can tell you from personal experience, when you get into the professional world of programming and aren't dealing with small projects you definitely need some sort of error handling, whether that be exception handling or something else. I personally use structured exceptional handling on the project I am currently working on and it's saved me tons of time catching access violations.

    Also to add to your original question, I'm not totally sure, I thought it was taught in a lot of schools. I know the school I want to went over it pretty thoroughly.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    As CornedBee alluded to, exceptions are not really required in C++ for beginning programs, so I can see how they are less likely to be taught than if a language requires their use in more situations. In my school, I wasn't even taught C++ (this was several years ago and I was told to read a book after my C programming class before using C++ in my data structures class), so who knows what the schools are thinking. I would guess that since many places still teach C++ as an extension of C, exceptions could easily be left out.

    To say that in general exceptions are almost never used is not quite accurate in my opinion. As MrWizard stated, in the professional world with large projects, exceptions are often used in C++ to handle error conditions. Assuming your implementation is not horrible, and you use exceptions for exceptional situations (as opposed to common and expected errors), I think they make good design a lot easier.

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    I too did not really learn exceptions until I learned Java.

    Many of you are only talking about beginning programs, but the difference between using exceptions in C++ and Java programs is not only in beginning programs, but also in advanced programs.

    C and C++ programmers grew up not knowing exceptions, and therefore they have found other ways to do the things that an exception would normally do. For example:

    if ( !fileopen ( filename ) )
    {
    cout << "File does not exist!!!";
    ....do something...
    ...maybe loop around or something like that...
    ...or maybe call an exit(1)....
    }

    You see that time and time again in C and C++ code. That is how we are used to doing error handling.

    Java is different. The same code in Java would do something like this:

    try
    {
    ....try opening a file....
    }
    catch FILE_NOT_FOUND_EXCEPTION
    {
    System.out.println ( "Oh no! Your file was not found!" );
    ...do something...
    }

    Personally I prefer the 1st way of doing things. That's what I have been doing for years. Exceptions dont really provide anything new or innovative in my opinion. Just another way of doing things...
    My Website

    "Circular logic is good because it is."

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    In that small example, exceptions might not provide anything new or innovative, but they certainly can make life better in more complex environments.

  13. #13
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    C++ is not as rigorous as Java with regard to exception handling, in that is does not force you to use them. Personally, I think this is a mistake.

    When I first started writing Windows code, I only used catch blocks to bury exceptions as I thought it would be a bad idea for the user to see them. And I wondered why my programs had so many enigmatic errors.

    Now I throw exceptions whenever I think my code could go wrong. I have very few errors of the type I did, because when they occur I know about them straight away.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HttpWebResponse Exceptions
    By Boomba in forum C# Programming
    Replies: 1
    Last Post: 01-03-2008, 11:43 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Exceptions that should terminate. Really.
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-26-2007, 10:56 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21