Professional Documents
Culture Documents
Name Spaces
Name Spaces
Namespaces
Allow to group entities like classes, objects and functions
under a name.
This way the global scope can be divided in "sub-scopes",
each one with its own name.
The format of namespaces is:
Namespace identifier
{
entities
}
int main ()
#include <iostream>
namespace first
return 0;
int var = 5;
}
namespace second
{
double var = 3.1416;
}
using
The keyword using is used to introduce a name from a namespace into the
current declarative region.
// using
#include <iostream>
int main ()
{
using first::x;
namespace first
using second::y;
int y = 10;
namespace second
{
}
double x = 3.1416;
double y = 2.7183;
return 0;
int main ()
#include <iostream>
namespace first
int y = 10;
return 0;
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main ()
{
{
namespace first
int x = 5;
namespace second
double x = 3.1416;
return 0;
}
Exception
An exception is a unusual, often
unpredictable event, detectable by
software or hardware, that requires
special processing occurring at
runtime
In C++, a variable or class object that
represents an exceptional event.
Handling Exception
If without handling,
Program crashes
Falls into unknown state
An exception handler is a section of
program code that is designed to execute
when a particular exception occurs
Resolve the exception
Lead to known state, such as exiting
the program
Standard Exceptions
new
throw Expression
Execution of try-catch
A
statement throws
an exception
Control moves
directly to exception
handler
No
statements throw
an exception
Exception
Handler
Statement
following entire try-catch
statement
Throwing an Exception to be
Caught by the Calling Code
void Func3()
{
try
{
Func4();
}
catch ( ErrType )
{
}
}
void Func4()
{
Function
call
Normal
return
if ( error )
throw ErrType();
}
Returnfrom
thrown
exception
// The numerator
// The denominator
{
if (denom != 0)
return numer / denom;
else
//What to do?? do sth. to avoid program
//crash
}
A Solution
int Quotient(int numer,
int denom )
// The numerator
// The denominator
{
if (denom == 0)
throw DivByZero();
//throw exception of class DivByZero
return numer / denom;
}
A Solution
//quotient.cpp--Quotientprogram
#include<iostream.h>
#include<string.h>
intQuotient(int,int);
classDivByZero{};//Exceptionclass
intmain()
{
intnumer;//Numerator
intdenom;//Denominator
//readinnumerator
anddenominator
while(cin)
{
try
{
cout<<"Theirquotient:"
<<Quotient(numer,denom)<<endl;
}
catch(DivByZero)//exceptionhandler
{
cout<<Denominatorcan'tbe0"<<endl;
}
//readinnumeratoranddenominator
}
return0;
}
#include <iostream.h>
int main()
{
char * buf;
try
{
buf = new char [51200000000];
if(buf ==0)
throw "Memory Allocation failure!";
cout<<"Successful";
}
catch (char *str)
{
cout<<"Exception raised:"<< str<<"\n";
}
return 0;
}
#include <iostream.h>
class number
{
private:
int num;
public:
void read()
{
cin>>num;
}
void main()
{
number n1,n2;
int result=0;
cout<<"Enter number1-";
n1.read();
cout<<"Enter number2-";
n2.read();
try
{
cout<<"\nTrying the division...";
result=n1.div(n2);
cout<<"\nsuccessful\n";
}
catch(char *p)
{
cout<<p<<"\nfailed\n";
cout<<"Exception :divide by 0\n";
}
cout<<"num1/num2 "<<result;
}