Académique Documents
Professionnel Documents
Culture Documents
Advertisements
Previous Page
Next Page
throw: A program throws an exception when a problem shows up. This is done
using a throw keyword.
try: A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
{
// catch block
}catch( ExceptionName e2 )
{
// catch block
}catch( ExceptionName eN )
{
// catch block
}
You can list down multiple catch statements to catch different type of
exceptions in case your try block raises more than one exception in
different situations.
Throwing Exceptions:
Exceptions
can
be
thrown
anywhere
within
a
code
block
using throwstatements. The operand of the throw statements determines a
type for the exception and can be any expression and the type of the result
of the expression determines the type of exception thrown.
Following is an example of throwing an exception when dividing by zero
condition occurs:
double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}
Catching Exceptions:
The catch block following the try block catches any exception. You can
specify what type of exception you want to catch and this is determined by
}
return (a/b);
}
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Description
std::exception
std::bad_alloc
std::bad_cast
std::bad_exception
std::bad_typeid
std::logic_error
std::domain_error
std::invalid_argument
std::length_error
std::out_of_range
std::runtime_error
std::overflow_error
std::range_error
std::underflow_error
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
std::cout << "MyException caught" << std::endl;
std::cout << e.what() << std::endl;
}
catch(std::exception& e)
{
//Other errors
}
}
1 ALGORITHM:
2.
2 PROGRAM:
3.
3 Output:
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float
d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
getch();
}
Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
1 ALGORITHM:
2.
2 PROGRAM:
3.
3 Output:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
template<class t>
:"<<a<<"\t"<<b;
swap(c,d);
cout<<"\nc and d after swaping
:"<<c<<"\t"<<d;
void main()
{
int a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
Output:
Enter A, B values (integer): 10 20
Enter C, D values (float):
2.50 10.80
swapping: 20 10