MODULE 5
Pointers, Arrays and Exception Handling
2. Explain pointer to class?
Pointer to Class
3. Explain pointer to object?
Classname Pointername;
Eg. Demo *P;
class employee
{
int id;
float salary;
public:
void accept()
{
cout<<”Enter details of employee”;
cin>>id>>salary;
}
void display()
{
cout<<”Employee’s id :”<<id;
cout<<”Employee’s salary :”<<salary;
}
};
Demo O;
Demo *P=&O;
P->accept();
4. Explain arrays in c++?
int arr[] = { 10, 20, 30, 40 } |
5. Explain the keywords try , throw and catch?
● C++ supports exception handling mechanism with the help of keywords : try, throw, catch.
Table: Exception handling blocks
Exception Handling | ||
try | catch | throw |
The statements that may generate exception are written within try block. | This block is exception handler. Exception thrown from try block are handled by catch block. | It is used to throw exception, when exception is detected in try block. |
● The general format of exception handling is as follows :
try
{
//statements that may generate errors
}
catch(data_type arg)
{
// statements to handle exceptions
}
● catch block must immediately follow try block.
● If exception is raised in try block the control automatically goes to catch block.
void main()
{
cout<<”start of program\n”;
try
{
throw 100; // exception is raised
cout<<”Hello world\n”; // this statement will not be executed.
}
catch(int i)
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
}
● Exception is thrown using “throw” keyword. It takes following form :
throw exception;
Example
throw 100;
throw x;
● Throw must be executed from within try block or from any function called from within try block.
try
{
--
throw x; //within try block
--
}catch(int i)
{
--
}
void data()
{
throw x; //called from function within try block
}
:
:
try
{
--
data();
--
}
catch(int i)
{
--
}
6. Write a program to handle "divide by zero" exception ?
#include<iostream>
using namespace std;
void data(int x, int y);
int main()
{
int a,b,c;
cout<<“Enter values of a and b”;
cin>>a>>b;
data(a,b);
}
void data(int x, int y)
{
try
{
if(y==0)
throw y
else
c=x/y;
cout<<c;
}
catch(int i)
{
cout<<“divide by zero exception”;
cout<<“Enter number greater than ”<<i;
}
return 0 ;
}
7. Explain exception classes?
● Exception class is designed specifically to be thrown as an exception.
● We can specify exception class within a class.
class Example
{
int a,b;
public:
class Demo // exception class
{
}
:
:
};
● Here the body of exception class is empty. However we can write statements in it. This class connects a throw statement with catch block. The statement,
throw Demo();
invokes implicit constructor of Demo class and creates an object of it.
throw Demo();
passes control to exception handler catch. It is written as follows :
Exception class
catch(Example::Demo)
{
Exception Handling code
}
Using this exception class we can return the description of problem encountered in program
8. Explain exception handling techniques?
void main()
{
cout<<”start of program\n”;
try
{
throw 100; // exception is raised
cout<<”Hello world\n”; // this statement will not be executed.
}
catch(int i)
{
cout<<”Exception occurs\n”;
cout<<”Value is”<<i<<endl;
}
}
#include<iostream> using namespace std;
void data(int x) { try { if(x) throw x; else throw ‘x’; } catch(int x) { cout<<”Caught integer exception”; } catch(char x) { cout<<”Caught character exception”; } } int main() { data(100); data(0); data(200); return 0 ; } |
//try block
// Step 1
// Step 2
// Step 3 Catch block to handle integer exception
// Step.4 Catch block to handle character exception |
9. Explain stack unwinding?
10. Explain types of exceptions?
● C++ provides standard exceptions defined in <exception>.
● These are as follows
1. std:: exception
It is parent class of all exceptions.
2. std::range_error
It occurs when we try to store a value out of range.
3. std::underflow_error
It occurs when arithmetic underflow occurs.
4. std::overflow_error
It occurs when arithmetic overflow occurs.
5. std::invalid_argument
It occurs due to invalid arguments.