Click to See Complete Forum and Search --> : Exception vs errors
Vedam Shashank
March 2nd, 2006, 03:23 AM
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 ??
7stud
March 2nd, 2006, 03:29 AM
To worry about such trivialities when you are beginning C++ is a waste of time.
laserlight
March 2nd, 2006, 03:34 AM
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.
Vedam Shashank
March 2nd, 2006, 03:39 AM
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++ ??
7stud
March 2nd, 2006, 03:59 AM
...because every beginning C++ book has a chapter on exceptions?
SuperKoko
March 2nd, 2006, 04:08 AM
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:
#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".
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.
jayender.vs
March 2nd, 2006, 04:43 AM
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.
Vedam Shashank
March 2nd, 2006, 04:53 AM
But I guess that you learnt C++ except exceptions.
:thumb::thumb:
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 :D:D - from one book that i saw - David Vandevoorde, Nicolai M. Josuttis - C++ Templates)
exterminator
March 2nd, 2006, 05:04 AM
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.
Graham
March 2nd, 2006, 06:22 AM
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.
auriumsoft
March 2nd, 2006, 07:52 AM
...
Hey SuperKoko, your reply is perfect. Thanks!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.