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

This is a discussion on Need advice: catch exceptions or call methods to check bits? within the C++ Programming forums, part of the General Programming Boards category; Howdy, I am wondering what is a better approach to check whether or not stream operations succeeded: Check exceptions: Code: ...

  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
    lyx
    lyx is offline
    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, 02:54 PM
  2. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM
  3. Hacked OST2 methods (not hacking)--need advice
    By jverkoey in forum Windows Programming
    Replies: 3
    Last Post: 07-24-2004, 12:03 AM
  4. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 08: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, 07:57 AM

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