What is an exception? How an exception is different from an error?
Explain how exceptions are handled in C++, with the help of an example program
Ans
Exception is a condition Which is Responsible for Occurrence
of Error Like Divide by Zero is an condition that never be possible So we can
call it an Exception which halts or stops the Execution of Program In java
there is an Exception Class Which is Responsible for producing an Error Message
when an Error has occurred.
when an Exception has occurred then compiler will never reach
to the Code that is placed after the block which contains an error. So
that there is special Mechanism given by java for handling Exceptions
When as Exception has occurred then the Compiler will generate an Exception
Code to an Exception Class and with the help of Exception object.
So if we wants to Execute Statements then we have to handle Exception For
handling Exceptions java provides us special Mechanism which is called as
Exception handling Mechanism Java provide following Methods for handling
Exceptions those are occurred in a program
An exception is a class that takes advantage of language
semantics. As others have stated, exceptions interrupt execution up the stack
until caught. An exception can be used to convey an error, but
more generally is used to convey that something exceptional has occurred.
Errors, on the other hand, can be exceptional or not.
There are several kinds of errors:
- User
error - this should be handled without an exception
- Syntax
error - this shouldn't compile in statically typed languages (in dynamic
languages, they're a little harder to discover)
- Runtime
error - this will either result in an exception, or silently fail (usually
creating unexpected results)
Really, exceptions should be limited to handling runtime
errors, since a user inputting bad data is not "exceptional." To
handle user errors, you should take the following approaches:
- Prevent
bad data from being input (front-end validation)
- Prevent
bad data from being persisted (back-end validation)
Exceptions should be used as a "last line of
defense" for user error. If you're writing a persistence layer, you can
rely on exceptions to ensure that bad data that falls through validation does
not get persisted. You should, however, fix any of these by putting a fix in
the validation that prevents the error from occurring in the first place.
Error: Any departure from the expected behavior of the system
or program, which stops the working of the system is an error.
Exception:Any error or problem which one can handle and
continue to work normally.
Note that in Java a compile time error is normally called an
"error," while a runtime error is called an "exception."
Errors don't have subclasses while exception has two
subclasses, they are compile time exception or checked exception (ClassNotFound
Exception, IOException, SQLException etc.) and runtime or unchecked
exception(ArrayIndexOutOfBounds Exception, NumberFormat Exception).
Exception handling is a construct
designed to handle the occurrence of exceptions, that is special conditions
that changes the normal flow of program execution. Since when designing a
programming task (a class or even a function), one cannot always assume that
application/task will run or be completed correctly (exit with the result it
was intended to). It may be the case that it will be just inappropriate for
that given task to report an error message (return an error code) or just exit.
To handle these types of cases, C++ supports the use of language constructs to
separate error handling and reporting code from ordinary code, that is,
constructs that can deal with these exceptions (errors and
abnormalities) and so we call this global approach that adds uniformity to
program design theexception handling.
An exception is said to be thrown at the
place where some error or abnormal condition is detected. The throwing will
cause the normal program flow to be aborted, in a raised exception.
An exception is thrown programmatic, the programmer specifies the conditions of
a throw.
In handled exceptions, execution of the program
will resume at a designated block of code, called a catch block,
which encloses the point of throwing in terms of program execution. The catch
block can be, and usually is, located in a different function/method than the
point of throwing. In this way, C++ supports non-local error handling.
Along with altering the program flow, throwing of an exception passes an object
to the catch block. This object can provide data that is necessary for the
handling code to decide in which way it should react on the exception.
Consider this next code example of a try and catch block
combination for clarification:
void AFunction()
{
// This function
does not return normally,
// instead
execution will resume at a catch block.
// The thrown
object is in this case of the type char const*,
// i.e. it is a
C-style string. More usually, exception
// objects are of
class type.
throw "This is
an exception!";
}
void AnotherFunction()
{
// To catch
exceptions, you first have to introduce
// a try block via
" try { ... } ". Then multiple catch
// blocks can
follow the try block.
// " try { ...
} catch(type 1) { ... } catch(type 2) { ... }"
try
{
AFunction();
// Because the
function throws an exception,
// the rest of
the code in this block will not
// be executed
}
catch(char const*
pch) // This catch block
// will react on exceptions
// of type char const*
{
// Execution
will resume here.
// You can
handle the exception here.
}
// As
can be seen
catch(...) // The
ellipsis indicates that this
// block
will catch exceptions of any type.
{
// In this
example, this block will not be executed,
// because the
preceding catch block is chosen to
// handle the
exception.
}
}
Unhandled exceptions on the other hand will result in a
function termination and the stack will be unwound (stack allocated objects
will have destructors called) as it looks for an exception handler. If none is
found it will ultimately result in the termination of the program.
From the point of view
of a programmer, raising an exception is a useful way to signal that a routine
could not execute normally. For example, when an input argument is invalid
(e.g. a zero denominator in division) or when a resource it relies on is
unavailable (like a missing file, or a hard disk error). In systems without
exceptions, routines would need to return some special error code. However,
this is sometimes complicated by the semi-predicate problem, in which users of
the routine need to write extra code to distinguish normal return values from
erroneous ones.
No comments:
Post a Comment