CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Exception vs errors

    What is the difference between exception and errors ??

    I Googled this... and here is what i came across so far...

    Compile time --> errors
    Runtime --> exceptions

    error... expected situation in an application
    exception... runtime error resulting in unexpected results

    Not clear about the difference.
    Can someone explain the difference ??
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  2. #2
    Join Date
    Oct 2004
    Posts
    296

    Re: Exception vs errors

    To worry about such trivialities when you are beginning C++ is a waste of time.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Exception vs errors

    From what I understand, exceptions are a language feature for handling errors such that the detection and reporting of the errors are separated. I'd say that "error" here is a rather generic term.

  4. #4
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Exception vs errors

    To worry about such trivialities when you are beginning C++ is a waste of time.
    What gave u that "wonderful" idea that I was beginning with C++ ??
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  5. #5
    Join Date
    Oct 2004
    Posts
    296

    Re: Exception vs errors

    ...because every beginning C++ book has a chapter on exceptions?

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Exception vs errors

    Quote Originally Posted by Vedam Shashank
    What gave u that "wonderful" idea that I was beginning with C++ ??
    Your question...
    But I guess that you learnt C++ except exceptions.

    Well... error is a very generic term.
    But we'll assume that error means "compile-time error".
    In that case, an error is very different from an exception.

    An error is the thing which makes your compiler stop and say "that line of code is bad because... there is a syntax error.... type mismatch... etc.)
    Compile-time errors are here because:
    • Some pieces of codes really have no meaning, and a compiler cannot understand what the programmer wanted to do... and maybe even a human cannot understand:
      For example:
      Code:
      #include <lskdjf>
      
      hello{var w; 42}(
      int printf;
      hello=double;
      )
      How could the compiler (or anybody) understand what it is?
    • Some other pieces of code are wrong because they reveal something that the programmer probably did not intended, and which is very probably wrong...
      For example "type checking".
      Code:
      struct T1;
      struct T2;
      void f(T1*);
      void g()
      {
      T2 obj;
      f(&obj); // the compiler "could" interpret it has an implicit reinterpret_cast from one pointer type to another, but that is probably wrong...
      }


    Compile-time errors are friendly...
    They are here for necessity or for convenience.
    If there is one or more Compile-time errors, there is no executable program produced.

    The exception mechanism is very different.
    exceptions are explicitly thrown by the program (and are thrown at runtime) when the program sees that something is wrong.
    For example, one may explicitly throw an exception if an OS function fails... a sound file cannot be open... or anything that should work but may not work because the system has not enough resources/does not work perfectly/has missing optional features.
    An exception may also be thrown if the programmer detects a buggy thing in the program (internal errors), that should theorically never happen, for example the programmer may write a stack class which throw an exception when pop is called when the stack is empty... or a bound-checked array.
    Once an exception is thrown, the program calls all the destructors of all automatic objects of functions currently in execution, and get up "into the call stack" up to reaching a try/catch block, which often, gracefully solves the problem or ignore the problem, avoiding a crash... For example it may just ignore playing the sound if the sound file cannot be open... It may stop the current operation is there is a stack underflow, without stopping other operations in a program that makes more than one operation at a time.

    Thus, exceptions are here to recover gracefully for runtime problems... It is a system of "problem tolerance".
    It is "tolerant" with OS function calls that may fail.
    It is "tolerant" with internal bugs (but in that case, it will probably write into a log file and report the bug to the programmer).
    That second usage of exceptions is often replace by assertions...
    Assertions are also runtime things, but unrecoverable... It outputs an error message and abort the program.
    Assertions are good for small projects, but a runtime exception may be preferable (for a user point of view) if the program is large and contains many bugs... It is better to say to the user "your last operation failed due to an internal bug" than "internal bug... All your work is lost... hahaha ! You should have saved your work more often, you stupid user.."

    Exceptions are not thrown when anything goes wrong...
    The standard library throws very few exceptions... There is the well known bad_alloc of new/new[], and some runtime exceptions of the STL (array bound checking with std::vector::at).
    But throwing and handling exceptions is the task of the programmer.

    Exceptions are meant to replace advantageously error codes yielded by old C functions.
    Note : even if exceptions may be used to control the execution flow of the program, they are not meant to be used like that. It is really a bad programming practice to do that.
    They are here to handle "exceptional cases".

    You can read C++ books (or online documents) to learn more about exceptions.
    For compile-time errors... there is not much to learn, except maybe the C++ standard, which says which are the "well-formed programs" and "ill-formed programs", knowing that an "ill-formed program" generate compile-time errors.
    Last edited by SuperKoko; March 2nd, 2006 at 05:14 AM.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Re: Exception vs errors

    what is the difference between an 'Exception' and an 'Error'?

    Error is an expected situation in an application. For example, think of a situation where you encounter a divided by zero or NaN in your logic, and the system would throw an error and stop. So you need to handle this situation wherever you logic expects that.

    Where as exception is a runtime error that the program might encounter during the execution of the program. For example you are trying to populate a table where the connection is closed, or you are trying to write a file, where hard disk is full. These are exceptions which are unpredictable errors during runtime.
    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  8. #8
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Exception vs errors

    But I guess that you learnt C++ except exceptions.

    Quite right.
    There are bits and pieces i have to touch upon.
    Exceptions and templates are a couple left... before i move into design patterns.(Templates seem to be quite vast, so left it for the end - from one book that i saw - David Vandevoorde, Nicolai M. Josuttis - C++ Templates)
    Last edited by Vedam Shashank; March 2nd, 2006 at 05:58 AM.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Exception vs errors

    Humans are not perfect.. but they are getting better...

    They make mistakes ... or make things that make mistakes .. things that are not perfect.

    From among those things - human can predict where some things can go wrong... they are errors.. they can be sorted out through logic or whatever...

    But there are things that humans don't have control over - they may work - they may not work - they are exceptions...

    This is all I have to say... And its a personal opinion so please don't consider it to be the ultimate truth of life or God's words and worry about it. I may be wrong in my perception. Hope this helps. Regards.

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Exception vs errors

    Basically, an exception is an error or condition that the immediate code is unequipped to deal with. You throw an exception if, at the point the condition/error arises, you can only throw your hands in the air and cry "What the &%$! do I do now?". Throw an exception and pass the buck to someone who can recover the situation. Exceptions become more common, the more general-purpose a bit of code is.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  11. #11
    Join Date
    Mar 2006
    Posts
    4

    Re: Exception vs errors

    Quote Originally Posted by SuperKoko
    ...
    Hey SuperKoko, your reply is perfect. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured