C++ Module 5
C++ Module 5
Run Time Errors - They are also known as exceptions. An exception caught during run time creates
serious issues.
Errors hinder normal execution of program. Exception handling is the process of handling errors and
exceptions in such a way that they do not hinder normal execution of the system. For example, User
divides a number by zero, this will compile successfully but an exception or run time error will occur due
to which our applications will be crashed. In order to avoid this exception handling techniques is used in
code.
syntax
try
{
//code
throw parameter;
}
catch(exceptionname ex)
{
//code to handle exception
}
try block
The code which can throw any exception is kept inside(or enclosed in) atry block. Then, when the code
will lead to any error, that error/exception will get caught inside the catch block.
catch block
catch block is intended to catch the error and handle the exception condition. We can have multiple
catch blocks to handle different types of exception and perform different actions when the exceptions
occur. For example, we can display descriptive messages to explain why any particular exception
occurred.
throw statement
It is used to throw exceptions to exception handler i.e. it is used to communicate information about
error. A throw expression accepts one parameter and that parameter is passed to handler.
throw statement is used when we explicitly want an exception to occur, then we can use throw
statement to throw or generate that exception.
#include <iostream>#include<conio.h>
using namespace std;
int main()
{
int a=10,b=0,c;
c=a/b;
return 0;
}
The above program will not run, and will show runtime error on screen, because we are trying to divide
a number with 0, which is not possible.
In the code above, we are checking the divisor, if it is zero, we are throwing an exception message, then
the catch block catches that exception and prints the message.
Doing so, the user will never know that our program failed at runtime, he/she will only see the message
"Division by zero not possible".
Using Multiple catch blocks
Below program contains multiple catch blocks to handle different types of exception in different way.
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3] = {-1,2};
for(int i=0; i<2; i++)
{
int ex = x[i];
try
{
if (ex > 0)
// throwing numeric value as exception
throw ex;
else
// throwing a character as exception
throw 'ex';
}
catch (int ex) // to catch numeric exceptions
{
cout << "Integer exception\n";
}
catch (char ex) // to catch character/string exceptions
{
cout << "Character exception\n";
}
}
}
In the above program, if the value of integer in the array x is less than 0, we are throwing a numeric
value as exception and if the value is greater than 0, then we are throwing a character value as
exception. And we have two different catch blocks to catch those exceptions.
Important questions
C. Catch
D. None of the above
Explanation: Catch keyword is used to handle the exception
A. Only i, iv, v
B. Only i, ii, iii
C. Only i, iv
D. Only i, ii
Ans : C
Explanation: Only i, iv statements are true about Catch handler.
int main()
{
try
{
throw 'b';
}
catch (int param)
{
cout << "Int Exception";
}
catch (...)
{
cout << "Default Exception";
}
cout << "After Exception";
return 0;
}
A. Default Exception After Exception
B. Int Exception After Exception
C. Int Exception
D. Default Exception
Ans : A
Explanation: The block catch(...) is used for catch all, when a data type of a thrown exception doesn't
match with any other catch block, the code inside catch(...) is executed. Note that the implicit type
conversion doesn't happen when exceptions are caught. The character 'a' is not automatically converted
to int.
int main()
{
int P = -1;
try {
cout << "Inside try";
if (P < 0)
{
throw P;
cout << "After throw";
}
}
catch (int P ) {
cout << "Exception Caught";
}
cout << "After catch";
return 0;
}
A. Inside try Exception Caught After throw After catch
B. Inside try Exception Caught After catch
C. Inside try Exception Caught
D. Inside try After throw After catch
Ans : B
Explanation: When an exception is thrown, lines of try block after the throw statement are not
executed. When exception is caught, the code after catch block is executed. Catch blocks are generally
written at the end through.
#include <iostream>
using namespace std;
int main( )
{
try
{
string strg1("Test");
string strg2("ing");
strg1.append(strg2, 4, 2);
cout << strg1 << endl;
}
catch (exception &LFC)
{
cout << "Caught: " << LFC.what() << endl;
cout << "Type: " << typeid(LFC).name() << endl;
};
return 0;
}
A. out of range
B. bad type_id
C. bad allocation
D. none of the mentioned
Ans : A
Explanation: As we are using out of bound value on strings, So it arising an exception.
10. Which illustrate predefined exceptions
A. Memory allocation error
B. I/O error
C. Both A and B
D. None of the above
Ans : C
Explanation: Both A and B illustrate predefined exceptions.