Unit 5
Unit 5
Unit 5
Runtime Error
When the code compiles without any error, there is still chance that the code will fail at run time.
The errors only occur at run time are call run time errors. Run time errors are those that passed
compiler’s checking, but fail when the code gets executed. These errors are unchecked.
The following are some common runtime errors:
Divide by zero exception
Array index out or range exception
StackOverflow exception
Dereferencing of an invalid pointer
etc……….
So, runtime errors are those which are generally can’t be handled and usually refers catastrophic failure.
Exception
An exception is a run-time error. Proper handling of exceptions is an importantprogramming issue.
This is because exceptions can and do happen in practice andprograms are generally expected to
behave gracefully in face of such exceptions.
Unless an exception is properly handled, it is likely to result in abnormal programtermination and
potential loss of work. For example, an undetected division by zeroor dereferencing of an invalid
pointer will almost certainly terminate the programabruptly.
Types
1
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
1. Synchronous: Exception that are caused by events that can be control of program is
called synchronous exception. Example, array index out or range exception.
2. Asynchronous: Exception that are caused by events beyond the control of program is
called synchronous exception. Example, Exception generated by hardware
malfunction.
Exceptions
Exceptions are run time anomalies or unusual condition that a program may encounter
during execution.
Examples:
Division by zero
Access to an array outside of its bounds
Running out of memory
Running out of disk space
An exception is an object. It is sent from the part of the program where an error occurs to
the part of the program that is going to control the error
2
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
try
catch
throw
The keyword try is used to preface a block of statements which may generate exceptions.
Syntax of try statement:
try
{
statement 1;
statement 2;
}
When an exception is detected, it is thrown using a throw statement in the try block.
throw (excep);
throw excep;
throw; // re-throwing of an exception
A catch block defined by the keyword ‘catch’ catches the exception and handles it
appropriately. The catch block that catches an exception must immediately follow the try
block that throws the exception
Syntax of catch statement:
try
{
Statement 1;
Statement 2;
}
catch ( argument)
{
statement 3; // Action to be taken
}
When an exception is found, the catch block is executed. The catch statement
contains an argument of exception type, and it is optional.
3
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
The C++ exception-handling mechanism provides three keywords; they are try, throw,
and catch. The keyword try is used at the starting of the exception. The throw block is present
inside the try block. Immediately after the try block, the catch block is present. Figure shows the
try, catch, and throw statements.
As soon as an exception is found, the throw statement inside the try block throws an
exception (a message for the catch block that an error has occurred in the try block statements).
Only errors occurring inside the try block are used to throw exceptions. The catch block receives
the exception that is sent by the throw block. The general form of the statement is as per the
following figure.
4
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout<<"Enter the values of a and b"<<endl;
cin>>a>>b;
try{
if(b!=0)
cout<<a/b;
else
throw b;
}
catch(int i)
{
cout<<"Division by zero: "<<i<<endl;
}
return 0;
}
Output:
Enter the values of a and b
2
0
Division by zero: 0
5
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
#include <iostream>
using namespace std;
int main() {
int a[5]={1,2,3,4,5},i;
try{
i=0;
while(1){
if(i!=5)
{
cout<<a[i]<<endl; i+
+;
}
else
throw i;
}
}
catch(int i)
{
cout<<"Array Index out of Bounds Exception: "<<i<<endl;
}
return 0;
}
Output:
1
2
3
4
5
Array Index out of Bounds Exception: 5
6
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
#include<iostream>
using namespace std;
void sqr()
{
int s;
cout<<"\n Enter a number:";
cin>>s;
if (s>0)
{
cout<<"Square="<<s*s;
}
else
{
throw (s);
}
}
int main()
{
try
{
sqr();
}
catch (int j)
{
cout<<"\n Caught the exception \n";
}
return 0;
Ouput:
Enter a number:10
Square=100
Enter a number:-1
Caught the exception
7
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
Here is an example of how you can create a user-defined exception to validate the age limit for
voting:
#include <iostream>
#include <exception>
public:
};
throw AgeLimitException();
else {
int main() {
int age;
8
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
try {
validateAge(age);
catch (AgeLimitException& e) {
return 0;
void test(int x) {
try {
if (x > 0)
throw x;
else
throw 'x';
} catch (int x) {
cout << "Catch a integer and that integer is:" << x;
} catch (char x) {
cout << "Catch a character and that character is:" << x;
}
}
9
Ramesh Prasad Bhatta, FWU
Exception Handling, Strings
void main() {
clrscr();
cout << "Testing multiple catches\n:";
test(10);
test(0);
getch();
}
Sample Output
10
Ramesh Prasad Bhatta, FWU