0% found this document useful (0 votes)
2 views

C++_Programming_Unit-4[1]

This document covers exception handling in programming, including concepts such as try, catch, throw, and user-defined exceptions. It also discusses generic programming with templates, providing examples of function and class templates. Additionally, it illustrates the use of UML diagrams for dynamic modeling and includes various coding examples to demonstrate the discussed concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++_Programming_Unit-4[1]

This document covers exception handling in programming, including concepts such as try, catch, throw, and user-defined exceptions. It also discusses generic programming with templates, providing examples of function and class templates. Additionally, it illustrates the use of UML diagrams for dynamic modeling and includes various coding examples to demonstrate the discussed concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT – 4

 Exception Handling – Try, Catch, Throw


 Multiple Exception, Finally

SRM IST, DELHI-NCR CAMPUS


 User-Defined Exception
 Generic Programming with Templates
 Templates – Function Template and Class Template
 Dynamic Modeling:
• UML Package Diagram
• UML Component Diagram
• UML Deployment Diagram

DR. BAPUJI RAO, Department of CSE(CORE)

HANDLING MODEL
3

 It uses THREE BLOCKS: try, throw, and catch.


 The relationship of these three exception handling constructs called the

SRM IST, DELHI-NCR CAMPUS


exception handling model which is shown diagrammatically as:

try BLOCK It detects the exception.


Detects Exception and
reaches to throw block

throw BLOCK Throws all exceptions if occurs.


Throw Exception
If occurs Catches all exceptions thrown
catch BLOCK from with in the TRY BLOCK
or by function invoked with in
a TRY BLOCK.
DR. BAPUJI RAO, Department of CSE(CORE)
SYNTAX
4

 Syntax of try BLOCK:


try
{

SRM IST, DELHI-NCR CAMPUS


Statement(s);
}
 Syntax of catch BLOCK:
catch (dataType variable_Name)
{
Statement(s);
}
 Syntax of throw BLOCK:
throw Constant / Variable / Expression;
DR. BAPUJI RAO, Department of CSE(CORE)

EXAMPLE-1
5

Read two numbers. Find the division.


#include<iostream>

SRM IST, DELHI-NCR CAMPUS


using namespace std;
int main( )
1st RUN
{
catch(float a)
float num1, num2;
{
try
cout<<num1<<"/"<<num2<<endl;
{
2nd RUN cout<<"Division by zero";
cout<<"Enter 1st Number :";
}
cin>>num1;
return 0;
cout<<"Enter 2nd Number :";
}
cin>>num2;
if(num2==0) throw num2;
float res = num1/num2;
cout<<num1<<"/"<<num2<<"="<<res;
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE-2
6

Read two numbers. Find the division (using function).


#include<iostream> catch (float a)
using namespace std; {

SRM IST, DELHI-NCR CAMPUS


float Divide (float a, float b) cout<<num1<<"/"<<num2<<endl;
{ int main() cout<<"Division by zero";
if (b==0) throw b; { }
return a/b; float num1, num2; return 0;
} try }
{
cout<<"Enter 1st Number :";
1st RUN cin>>num1; 2nd RUN
cout<<"Enter 2nd Number :";
cin>>num2;
float res = Divide(num1,num2);
cout<<num1<<"/"<<num2<<"="<<res
;
}
DR. BAPUJI RAO, Department of CSE(CORE)

Catch All Exceptions [ catch(…) ]


7

 It catches any kind of exception by the exception handler.


 The exception handler has one argument i.e. ELLIPSIS(...) which

SRM IST, DELHI-NCR CAMPUS


can receive any kind of data type.
 Syntax: Capable of receiving any kind of data thrown by the throw statement.
catch( … )
{
Statement(s);
}

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE
Finding factorial of a number using catch all exception method. 8

int main( )
{
#include<iostream> int num;
try

SRM IST, DELHI-NCR CAMPUS


using namespace std;
long Fact(int a) {
{ cout<<"Enter a Number for factorial :";
if(a<0) throw a; cin>>num;
long res = Fact(num);
long res =1; cout<<"Number = "<<num<<endl<<"Factorial ="<<res;
for(int i=1; i<=a; i++) }
catch(...) 1st RUN
res = res*i;
return res; {
} cout<<"Number = "<<num<<endl;
cout<<"No Factorial";
2nd RUN
}
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)

MULTIPLE EXCEPTION
9
 Syntax:
try
{
statement(s);
}

SRM IST, DELHI-NCR CAMPUS


 If a program has more than one exception then it is catch (data_type1 variable)
{
necessary to have that many exception handlers.
statement(s);
 One exception handler is used for solving one kind of }
catch (data_type2 variable)
exception.
{
 All the exception handlers are defined after the try statement(s);
}
block.
………………………………
catch (data_typeN variable)
{
statement(s);
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
10
Check a number is positive or negative or zero using multiple exception.
#include<iostream>
using namespace std; catch(char a) 1st RUN
void Check(float num) {

SRM IST, DELHI-NCR CAMPUS


{ cout<<"Positive"; 
if(num>0) throw 'a'; }
else if(num<0) throw 67;
else throw num; catch(int a) 2nd RUN
} {
int main() cout<<"Negative"; 
{ }
float num;
try catch(float a) 3rd RUN
{ {

cout<<"Enter a Number :"; cout<<"Zero";
cin>>num; }
Check(num); return 0;
} }
DR. BAPUJI RAO, Department of CSE(CORE)

RETHROWING EXCEPTION
11
void Function( )
 The statement throw is used to rethrow an exception. {
try
 It allows an exception without any argument. {
if(condition) throw value;

SRM IST, DELHI-NCR CAMPUS


It causes the current exception to be thrown to the }
catch(Data_Type Variable)
next enclosing try/catch block.
{
 Syntax: Statement(s);
throw; throw; void main( )
} {
} try
{
Function( );
}
catch(Data_Type Variable)
{
Statement(s);
}
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE
12

Check a number is prime or not using rethrow exception method.

#include<iostream>

SRM IST, DELHI-NCR CAMPUS


using namespace std;
int prime(int n)
{
int c=0;
try
catch(int e)
{
{
if(n<=0) throw n;
cout<<"Exception in function prime( )\n";
for(int i=1; i<=n; i++)
cout<<"The number = "<<e<<endl;
if(n%i == 0) c++;
throw;
return c; }
} }

DR. BAPUJI RAO, Department of CSE(CORE)

Continue…
13

int main( )
{
int num; 1st RUN
try

SRM IST, DELHI-NCR CAMPUS


{
cout<<"Enter a number ";
cin>>num;
int r = prime(num);
if(r==2) cout<<"Prime"; 2nd RUN
else cout<<"Not Prime";
}
catch(int a)
{ 3rd RUN
cout<<"Exception in Main\n";
cout<<"It is "<<a<<" for which no prime is possible";
}
return 0;
} DR. BAPUJI RAO, Department of CSE(CORE)
USER-DEFINED EXCEPTION
14

 The user-defined exception can be created with the help of a class type.

 Syntax:

SRM IST, DELHI-NCR CAMPUS


class Class_Name
{

};
try
{
if(Condition(s)) throw Class_Name( );
}
catch(Class_Name Object_Name)
{
// Statement(s);
}
DR. BAPUJI RAO, Department of CSE(CORE)

EXAMPLE
15

#include<iostream> try catch(Negative n)


using namespace std; { {
class Positive if(num>0) throw Positive( ); cout<<" Entered Number is Negative";
{ if(num<0) throw Negative( ); }

SRM IST, DELHI-NCR CAMPUS


}; if(num==0) throw Zero( ); catch(Zero z)
class Negative } {
{ catch(Positive p) cout<<" Entered Number is Zero";
}; { }
class Zero cout<<" Entered Number is Positive"; cout<<endl<<endl;
{ } return 0;
}; }
int main( ) 1st RUN 2nd RUN
{
int num;
cout<<"\n Enter a Number: ";
3rd RUN
cin>>num;

DR. BAPUJI RAO, Department of CSE(CORE)


GENERIC PROGRAMMING WITH TEMPLATES
16

 The template supports generic programming, which allows the development of


reusable software components such as functions, classes, etc, which support different

SRM IST, DELHI-NCR CAMPUS


data types in a single framework.
 It allows a single template to deal with a GENERIC DATA TYPE.
 The templates declared for functions are called FUNCTION TEMPLATES.
 The templates declared for classes are called CLASS TEMPLATES.
 They perform appropriate operations depending on the data type of the parameters
passed to them.

DR. BAPUJI RAO, Department of CSE(CORE)

FUNCTION TEMPLATE
17

 A FUNCTION TEMPLATE or GENERIC FUNction specifies how an individual


function can be constructed.

SRM IST, DELHI-NCR CAMPUS


 At least one argument of function template must be a TEMPLATE TYPE.

 Syntax:
template <class/typename template_name-1, template_name-2, ………...>
return_type Function_Name(template_name-1 variable1,…….)
{
// Statement(s);
}

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE-1
18

#include<iostream.h>
template<class T> // template<typename T>
void Show(T a)

SRM IST, DELHI-NCR CAMPUS


{ OUTPUT
cout<<a<<endl;
67
}
14.37
void main( ) A
{ SRM
Show(67);
Show(14.37);
Show('A');
Show("SRM");
}

DR. BAPUJI RAO, Department of CSE(CORE)

EXAMPLE-2
19

#include<iostream.h>
template<class T>
// 1st argument is primitive and the 2nd argument is a

SRM IST, DELHI-NCR CAMPUS


// template kind.
void Show(char *p, T a) OUTPUT
{ The Integer = 67
cout<<p<<a<<endl; The Float = 14.37
} The Character = A
void main( ) The String = Smiley
{
Show("The Integer = ", 67);
Show("The Float = ", 14.37);
Show("The Character =", 'A');
Show("The String = ", "SRM");
}
DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE-3
20

#include<iostream.h>
template <typename A, typename B>
void Show(A a, B b)

SRM IST, DELHI-NCR CAMPUS


{
cout<<a<<" "<<b<<endl; OUTPUT
}
SRM A
int main( )
BRP 67
{ 1 7
Show("SRM", 'A'); 1437 66.67
Show("BRP", 67); A B
Show(1, 7);
Show(1437, 66.67);
Show('A', 'B');
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)

EXAMPLE-4
21

Sum of Array of Integers and Array of Floating numbers using Function Template.
#include<iostream.h>
template<class T>

SRM IST, DELHI-NCR CAMPUS


T sum(T *a, int n) // T sum(T a[ ], int n)
{
T s = 0;
for (int i = 0; i < n; i ++)
s = s + a[i];
return s; template<class T>
} void show(T *a, int n) // void show(T a[ ], int n)
{
for (int i = 0; i < n; i ++)
cout<<a[i]<<" ";
cout<<endl;
}
DR. BAPUJI RAO, Department of CSE(CORE)
Continue…
22

int main( )
{
int a[4] = {1, 4, 3, 7}, res1;

SRM IST, DELHI-NCR CAMPUS


float b[5] = {1.5, 2.6, 3.7, 6.7, 6.6}, res2; OUTPUT
res1 = sum(a, 4); Integer Array Elements
1 4 3 7
res2 = sum(b, 5); Sum = 15
cout<<"Integer Array Elements\n";
show(a, 4); Floating Array Elements
cout <<"Sum = "<< res1 << endl; 1.5 2.6 3.7 6.7 6.6
Sum = 21.1
cout<<"\nFloating Array Elements\n";
show(b, 5);
cout <<"Sum = "<<res2;
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)

CLASS TEMPLATE
23

 Classes can also be declared to operate on different data types.


 Such classes are called CLASS TEMPLATES.

SRM IST, DELHI-NCR CAMPUS


 Definition-1:
A class template specifies how individual classes can be constructed.
 Defintion-2:
A class template provides a specification for generating classes based on parameters.

DR. BAPUJI RAO, Department of CSE(CORE)


SYNTAX
24

template<class/typename template_name, …….. >


class class_name

SRM IST, DELHI-NCR CAMPUS


{
private:
template_name var1, var2,……;

public:
// Member Function Template Definition
return_type function_name(template_name var-1, template_name var-2)
{
// Statement(s);
}

};
DR. BAPUJI RAO, Department of CSE(CORE)

TEMPLATE CLASS
25

 A class template is instantiated by passing a given set of data types to it as template


arguments.

SRM IST, DELHI-NCR CAMPUS


 Syntax:
void main( )
{
class_name<template_name, ……> object_name1,……;
}

DR. BAPUJI RAO, Department of CSE(CORE)


EXAMPLE-1
int main( ) 26
{
#include<iostream.h> ARS<int> IObj; // template class
template <class T> ARS<float> FObj; // template class
class ARS // class template
ARS<char> CObj; // template class
{

SRM IST, DELHI-NCR CAMPUS


IObj.Assign(1437); OUTPUT
private:
T a; FObj.Assign(66.67); Integer Object
public: a=1437
CObj.Assign('A'); Float Object
void Assign(T b)
{ cout << "Integer\n"; a=66.67
a = b; IObj.Show( ); Character Object
} a=A
void Show( ) cout <<"Float\n";
{ FObj.Show( );
cout<<"a="<<a<<endl; cout <<"Character\n";
}
}; CObj.Show( );
return 0;
}
DR. BAPUJI RAO, Department of CSE(CORE)

EXAMPLE-2
Sum and product of two numbers using class template. 27

OUTPUT
template <class T> void main( )
Enter two Integers : 7 6
class SP {
Enter two Floats : 1.4 3.7
{ Integers:
SP<int> obj1;
T a, b; a =7 b=6 SP<float> obj2;

SRM IST, DELHI-NCR CAMPUS


public : Sum = 13 cout<<"Enter two Integers :";
void Get( ) Product = 42 obj1.Get( );
{ Floats: cout<<"Enter two Floats :";
cin>>a>>b; a = 1.4 b = 3.7 obj2.Get( );
} Sum = 5.1 cout <<"Integers:\n";
Product = 5.18
void Show( ) obj1.Show( );
{ void Sum( ) obj1.Sum( );
cout<<"a = "<<a<<" b = "<<b<<endl; { obj1.Product( );
} T r = a+b; cout <<"Floats:\n";
cout<<"Sum = "<<r<<endl; obj2.Show( );
} obj2.Sum( );
void Product( ) obj2.Product( );
{ }
T r = a*b;
cout<<"Product = "<<r<<endl;
}
}; DR. BAPUJI RAO, Department of CSE(CORE)
EXAMPLE-3
28

#include<iostream.h> OUTPUT
template<class T> void main( ) 67
class SRM { 67

SRM IST, DELHI-NCR CAMPUS


{ SRM<int> A; 67
T a; SRM<char> B; 67
public: SRM<float> C; 67
void Assign(T b) @
{ A.Assign(67); @
a = b; B.Assign(‘@’); @
} C.Assign(6.7); @
void Display(int n) @
{ A.Display(5); @
for(int i=1; i<=n; i++) B.Display(7); @
cout<<a<<endl; C.Display(2); 6.7
} } 6.7
};

DR. BAPUJI RAO, Department of CSE(CORE)

OVERLOADED FUNCTION TEMPLATE


29

 A function template can also be overloaded by defining more than one with same name
and different signature.

#include<iostream.h>

SRM IST, DELHI-NCR CAMPUS


OUTPUT
template < class T>
// function template with template argument 1347
void print(T a) int main( ) 31.47
{ { 67
cout <<a <<endl; print(1347); 67
} print(31.47); 67
template <class T> SRM
// function template with template and primitive argument print(67, 3); SRM
void print(T a, int n) print("SRM", 5); SRM
{ SRM
for (int i=1; i<=n; i++) print('A', 2); SRM
cout << a << endl; return 0; A
} } A
DR. BAPUJI RAO, Department of CSE(CORE)
PACKAGE DIAGRAMS
30

 It is mainly used to represent the organization and the structure of a system in the form
of PACKAGES.
 A SET OF CLASSES are grouped into PACKAGES and packages may reside in other

SRM IST, DELHI-NCR CAMPUS


packages.

 BASIC ELEMENTS OF PACKAGE DIAGRAM


• Package
• Namespace
• Package Merge
• Package Import
• Dependency / Package Access
• Constraint

DR. BAPUJI RAO, Department of CSE(CORE)

PACKAGE DIAGRAMS
31

 Package
• It is a container for organizing different diagram elements such as classes,
interfaces, etc.

SRM IST, DELHI-NCR CAMPUS


• It is a folder-like icon with its name.
• Symbol: Package Name

 Namespace
• It represents the name of the package.
• It generally appears on top of the package symbol.
• Example:
Student

DR. BAPUJI RAO, Department of CSE(CORE)


PACKAGE DIAGRAMS
32

 Package Merge
• It is a relationship that signifies how a package can be merged or combined.

SRM IST, DELHI-NCR CAMPUS


It is represented as a dashed arrow and named as <<merge>> is written over the
line.
• Example:

Package-1 Package-2
<<Merge>>

DR. BAPUJI RAO, Department of CSE(CORE)

PACKAGE DIAGRAMS
33

 Package Import
• It indicates that functionality has been imported from one package to another.
• It is represented as a dashed arrow and named as <<import>> is written over the line.

SRM IST, DELHI-NCR CAMPUS


• Examples: <<Import>> 2DShape

java Shopping Cart


Shapes 2DShape

3DShape

<<Import>>
<<Import>> <<Import>>
<<Import>> <<Import>>
Shapes
util.Vector Inventory
<<Import>> <<Import>>

Square Rectangle Circle Triangle

DR. BAPUJI RAO, Department of CSE(CORE)


PACKAGE DIAGRAMS
34

 Package Access / Dependency


• It indicates that one package requires assistance from the functions of another package.
• It is represented as a dashed arrow.

SRM IST, DELHI-NCR CAMPUS


• Examples: Fish Water Online Payment Internet
<<Access>> <<Access>>

Payment Shopping Cart


<<Access>>

 Constraint
• It is like a condition or requirement set related to a package.
• It is represented by curly braces.
DR. BAPUJI RAO, Department of CSE(CORE)

PACKAGE DIAGRAMS
35

Example: Package diagram is used to Example: Nested and Hierarchical Package.


represent the composition of business.

SRM IST, DELHI-NCR CAMPUS


java
Business
util

Date
Account Ordering Shipping

DR. BAPUJI RAO, Department of CSE(CORE)


PACKAGE DIAGRAMS
36
Example: Track Order Service for an online shopping store.

UI Framework Track Order Shipping

SRM IST, DELHI-NCR CAMPUS


<<Access>> <<Import>>

<<Access>>
<<Access>>

Order Processing
<<Import>>

DR. BAPUJI RAO, Department of CSE(CORE)

Example: MVC Structure.

Presentation Layer 37

Application Views
Java AWT

SRM IST, DELHI-NCR CAMPUS


Application Logic

Control Objects Business Logic

Application Logic

DBMS Query Engine File Management

DR. BAPUJI RAO, Department of CSE(CORE)


PACKAGE DIAGRAMS
Example: Package Merge Relationship. 38

Cash Payment Online Payment Card Payment Cheque Payment

SRM IST, DELHI-NCR CAMPUS


<<
<<Merge>> <<Merge>>
Payment

<<Merge>> <<Merge>>

DR. BAPUJI RAO, Department of CSE(CORE)

COMPONENT DIAGRAMS
39

 Components are SOFTWARE ENTITIES that satisfy certain functional


requirements specified by INTERFACES.

SRM IST, DELHI-NCR CAMPUS


 It is used to show CODE MODULES of a system.
 It is generally used for modeling subsystems.
 It represents how each and every component acts during execution and running of a
System Program.
 The word COMPONENT simply means modules of a class that usually represents
an Independent Subsystem.

DR. BAPUJI RAO, Department of CSE(CORE)


COMPONENT DIAGRAMS SYMBOLS
40

SYMBOL NAME PURPOSE

A component provides and consumes behaviour through

SRM IST, DELHI-NCR CAMPUS


COMPONENT
interfaces, as well as through other components.

It represents hardware or software objects, which are of a


NODE
higher level than components.

NOTES Allows developers to write notes.

DR. BAPUJI RAO, Department of CSE(CORE)

COMPONENT DIAGRAMS SYMBOLS


41

SYMBOL NAME PURPOSE

DEPENDENCY One part of the system depends on another.

SRM IST, DELHI-NCR CAMPUS


It represents the interfaces where a component produces
PROVIDED
information used by the required interface of another
INTERFACE component.

It represents the interfaces where a component requires


REQUIRED
OR information in order to perform its proper function.
INTERFACE

It specifies a separate interaction point between the


PORT component and the environment.

DR. BAPUJI RAO, Department of CSE(CORE)


LIBRARY MANAGEMENT SYSTEM
42

DATABASE BOOK

SRM IST, DELHI-NCR CAMPUS


<<depends>>

<<depends>>

Transaction
SYSTEM <<depends>> LIBRARY
SEARCH MEMBER

<<depends>>

FEES
DR. BAPUJI RAO, Department of CSE(CORE)

COMPONENT DIAGRAM OF AN ATM SYSTEM


43

Account
Information
ATM
Transaction
ATM <<Depends>> BANK EMPLOYEE
Machine Database Station

SRM IST, DELHI-NCR CAMPUS


<<Depends>>

CARD <<Depends>>
CUSTOMER CLIENT
Station READER DESKTOP

Web
Merchant <<Depends>>
Transaction CLIENT
DESKTOP
Transaction
WEB PAGE ONLINE
Transaction

DR. BAPUJI RAO, Department of CSE(CORE)


DEPLOYMENT DIAGRAM
44

 It shows the physical deployment of software components on hardware nodes.


 It illustrates the mapping of software components onto the physical resources of a system, such as
SERVERS, PROCESSORS, STORAGE DEVICES, and NETWORK INFRASTRUCTURE.

SRM IST, DELHI-NCR CAMPUS


 BASIC ELEMENTS OF DEPLOYMENT DIAGRAM
• Nodes
• Components
• Artifacts
• Interface
• Dependencies
• Association
• Communication Paths
DR. BAPUJI RAO, Department of CSE(CORE)

DEPLOYMENT DIAGRAM
45

SYMBOL NAME PURPOSE

It represents a physical or computational resource.


NODE Examples: Hardware Device, Server, Workstation, and

SRM IST, DELHI-NCR CAMPUS


Computing Resource.
It represents a point of interaction between different
INTERFACE
components or subsystems.

It represents software modules that are deployed onto nodes.


COMPONENT Examples: Executable Files, Libraries, Databases, and
Configuration Files.

The Physical files are deployed onto nodes. So that the actual
ARTIFACTS implementation of software components, such as executables,
scripts, databases, etc will be taken place.
DR. BAPUJI RAO, Department of CSE(CORE)
DEPLOYMENT DIAGRAM
46

SYMBOL NAME PURPOSE

It represents communication between two device


STRAIGHT LINE

SRM IST, DELHI-NCR CAMPUS


nodes.
It represents the relationships or dependencies
DASHED LINE between elements, indicating that one element is
related to or dependent on another.

DR. BAPUJI RAO, Department of CSE(CORE)

ONLINE EXAMINATION REGISTRATION SYSTEM


47

Online Examination Registration

SRM IST, DELHI-NCR CAMPUS


Login Test

Registration Select Exam Manage Profile and Status View Result

View Profile Report

Payment
DR. BAPUJI RAO, Department of CSE(CORE)
MOBILE BANKING ANDROID SERVICES
48

Application Data

SRM IST, DELHI-NCR CAMPUS


Server Storage

Account
Statement Account
Preparation Details

Client Fund
Web Transfer
Android
Services
Device
Cheque
Transfer Customer
Details

Other
Services

DR. BAPUJI RAO, Department of CSE(CORE)

You might also like