Thread: Need advice: catch exceptions or call methods to check bits?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Wink Need advice: catch exceptions or call methods to check bits?

    Howdy,
    I am wondering what is a better approach to check whether or not stream operations succeeded:

    Check exceptions:

    Code:
    file.exceptions(ios_base::eofbit||ios_base::failbit);
    
    try
    {
       do operations on stream
    }
    catch (ios_base::eofbit)
    {
       handle this exception
    }
    catch (ios_base::failbit)
    {
       handle this exception
    }
    or just use the methods provided with streams:

    Code:
    do operations on stream
    
    if (file.eof())
    {
       handle eof
    }
    
    if (file.fail())
    {
       handle failure
    }

    Normally I'd use exceptions but I'm wondering why the methods eof(), etc., were provided if exception-checking is better. I imagine the exceptions handling is much faster and creates less error-prone code....

    Thanks for any info. Just curious.

  2. #2
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, I don't know for that case, but I can tell you that the classic error checking method with if/else statements is usually faster than the exception handling method. This is because exception handling needs to pass objects through function scope whereas if/else test only check for a variable.
    At least, it's what I think. But, usually, the more complex the abstraction is, the slower it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  2. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  3. Hacked OST2 methods (not hacking)--need advice
    By jverkoey in forum Windows Programming
    Replies: 3
    Last Post: 07-23-2004, 11:03 PM
  4. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM