Question: In some languages, certain exceptions are known as checked exceptions and those languages require that the exceptions are caught, but all exceptions in C +
In some languages, certain exceptions are known as checked exceptions and those
languages require that the exceptions are caught, but all exceptions in C are known
as unchecked exceptions, which do not require an exception handler. Most unchecked
exceptions, however, cause the program to display an error message which may be
a security issue since it exposes the inner details of your application and stop
execution when not handled. Others, such as problems with inputoutput must be
activated by the programmer before the runtime environment will throw them.
Exception handling provides a way for the program to possibly recover from an error
or at least terminate in a graceful manner.
Consider the following simple program and type it in as LabB cpp:
Compile and run the program, entering an integer between and inclusively.
Hopefully, all worked well and it responded that you entered the number correctly.
The program is requesting a number, but what if the user does not cooperate? Run
the program again and enter "four". The program is unable to store an integer value
into num, so num retains its original value of and the program falls into an infinite
loop.
By default, C does not throw exceptions when something goes wrong with IO We
can, however, activate exceptionthrowing by prepending the following line of code
to the start of your main function:
cin.exceptions ios: :failbit;
This line informs C that you'd like to the program to throw iostream: : failure
exception whenever an IOrelated method fails. Try running the program again with
this line of code present and enter "four". This time, the program should terminate
and generate an error message, rather than fall into an infinite loop.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
