C++ : Exception Handling Part 2

1. Explain Exception Handling.

2. Explain multiple catch blocks.

3. Explain re-throwing exception.

4. Explain catch all exception that is catch(…).

5. Example showing the use of multiple catch and re-throwing exception in C++.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Exception Handling-Part 2.
*/

#include

using namespace std;

void testException1()
{
try
{
//throw(100);

//throw(‘a’);

//throw(10.56);
}
catch(int val)
{
cout<<“\n——-Integer Exception occured——-“;
}
catch(char val)
{
cout<<“\n——-Character Exception occured——-“;
}
catch(…)
{
cout<<“\n——-All Exceptions handling——-“;
}
}

void testException2()
{
int val;

cout<>val;

if(val<=0)
{
throw(val);
}
}

int main()
{
testException1();

try
{
testException2();
}
catch(int value)
{
cout<<“\n——-Rethrow Exception occured——-“;
}
return 0;
}

Advertisements

Leave a comment

Filed under C++, Exception Handling

Facebook in Mobile Business

Don’t get shocked if you find a mobile phone by “Facebook” yes facebook is planning to launch a mobile phone mainly more concentrated towards to facebook application base. Hardware and OS wise not much surprise mostly probably it HTC + Android. Yes definitely it will he having modified Android as per the companies need.

photo_1364666529_temp

Read More Details

1 Comment

Filed under Android, General

C++ : Exception Handling Part 1

1. Explain Exception Handling & its use.

2. Explain the [try,catch & throw]

3. Example showing the use of Exception handling.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Exception Handling-Part 1.
*/

#include

using namespace std;

void testException()
{
int rel;

cout<>rel;

try
{
if(rel<=0)
{
throw(rel);
}
else
{
cout<<“\nThe value is:”<<rel;
}
}
catch(int val)
{
cout<<“\n——-Exception occured——-“;
}
}

int main()
{
testException();

return 0;
}

Leave a comment

Filed under C++, Exception Handling

C++ : File I/O Part 6

1. Explain the use of File I/O.
2. List major Classes.
3. List major methods.
4. List major File opening modes.
5. List special methods for exception handling in C++ File I/O.
6. Explained using Mind Map.

Leave a comment

Filed under C++, File I/O

C++ : File I/O Part 5 Operations using Operator

1. Explain the use of File I/O.

2. Major File I/O Operations:

1. Write Object using <> Operator.

3. Example showing the use of Operator overloading for read & write objects in File.

2 Comments

Filed under C++, File I/O

C++ : File I/O Part 4

1. Explain the use of File I/O.

2. Major File I/O Operations:

1. Write Object.

2. Display All Objects.

3. Update Object base on ID.

4. Delete Object base on ID.

5. Search Object base on ID.

3. Example showing the use of seekg(),seekp(),tellg() & tellp() operaiton on Files.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
C++ File I/O - Part 4.
*/

#include
#include
#include
#include

using namespace std;

//Employee Class
class Employee
{
int id;
char name[20];
int salary;

public:

Employee()
{ }

~Employee()
{ }

void setID(int tid)
{
id=tid;
}
int getID()
{
return(id);
}
void setData()
{
cout<>name;
cout<>salary;
}
void display()
{
cout<<id;
cout<<“\t”<<name;
cout<<“\t”<<salary;
}
char* getName()
{
return name;
}
};

//Methods related to Employee
void appendEmployee();
void displayAllEmployee();
void modifyEmployee();
void deleteEmployee();
void searchEmployee();
int getSizeofFile();
int getID();

char fname[]={“one.dat”};
char fid[]={“fid.dat”};

//Main Method
int main()
{
int ch;

while(1)
{
printf(“========================================================\n\n”);

printf(“\t\t Welcome Employee Management System\n\n”);

printf(“========================================================\n\n”);

printf(“1. Add New Employee\n\n”);
printf(“2. Modify Existing Employee Details\n\n”);
printf(“3. Delete Employee\n\n”);
printf(“4. Search Employee by Name\n\n”);
printf(“5. Display All Employee Details\n\n”);

printf(“0. Exit\n\n”);

printf(“========================================================\n\n”);

printf(“\nPlease enter your Choice:”);
scanf(“%d”,&ch);

switch(ch)
{
case 1: appendEmployee();
break;

case 2: modifyEmployee();
break;

case 3: deleteEmployee();
break;

case 4: searchEmployee();
break;

case 5: displayAllEmployee();
break;

case 0: exit(0);
}

}

return 0;
}

//Return the Auto generated ID for Employee
int getID()
{
ifstream fin(fid);
int value=0;

if(fin==NULL)
{
fin.close();
ofstream fout(fid);
fout<>value;
fin.close();

ofstream fout(fid);
fout<<value+1;

fout.close();

return(value+1);
}

//Add New Employee record into the File
void appendEmployee()
{
Employee t1;

fstream fout(fname,ios::app|ios::out);

t1.setID(getID());
t1.setData();

fout.write((char *) & t1,sizeof(t1));
fout.close();
}

//Update existing Employee record details into the File – Base of ID
void modifyEmployee()
{
Employee t;
int size=getSizeofFile();

fstream fout(fname,ios::ate|ios::out|ios::in);
fout.seekg(0);

int id;
int counter=0;

cout<>id;

if(fout==NULL)
{
cout<<“\n Sorry no record found \n”;
}
else
{
for(int i=0;i<size;i++)
{
fout.read((char *) & t,sizeof(t));

if(id==t.getID())
{
int size=fout.tellg();
size=size-sizeof(t);
fout.seekp(size,ios::beg);

Employee newobj=t;
newobj.setData();
fout.write((char *) &newobj,sizeof(newobj));

counter=1;
break;
}
cout<<“\n”;
}
printf(“========================================================\n\n”);
}
fout.close();

if(counter==0)
{
cout<<“\n Sorry no record found”;
}
else
{
cout<<“\n Record Updated”;
}
}

//Delete Employee Details from the File – Base on ID
void deleteEmployee()
{
Employee t;
int size=getSizeofFile();

ifstream fin(fname);
ofstream fout(“temp.dat”);

int id;
int counter=0;

cout<>id;

if(fin==NULL)
{
cout<<“\n Sorry no record found \n”;
}
else
{
for(int i=0;i<size;i++)
{
fin.read((char *) & t,sizeof(t));

if(id==t.getID())
{
counter=1;
}
else
{
fout.write((char *) & t,sizeof(t));
}
}
}
fin.close();
fout.close();

if(counter==0)
{
cout<<“\n Sorry no record found”;
}
else
{
fin.open(“temp.dat”);
fout.open(fname);

for(int i=0;i<size-1;i++)
{
fin.read((char *) & t,sizeof(t));

fout.write((char *) & t,sizeof(t));
}

fin.close();
fout.close();
}
}

//Search Employee Details from File – Base on ID.
void searchEmployee()
{
Employee t;
int size=getSizeofFile();
int id=0;
int counter=0;

cout<>id;

ifstream fin(fname);

printf(“\n========================================================\n\n”);
printf(“\t\t Employee Details\n\n”);
printf(“========================================================\n\n”);

printf(“ID\tName\tSalary\n\n”);

for(int i=0;i<size;i++)
{
fin.read((char *) & t,sizeof(t));

if(id==t.getID())
{
t.display();
counter=1;
}
cout<<“\n”;
}
printf(“========================================================\n\n”);

fin.close();

if(counter==0)
{
cout<<“\n Sorry no record found”;
}
}

//Display all the Employee Details stored in the File
void displayAllEmployee()
{
Employee t;
int size=getSizeofFile();

ifstream fin(fname);

if(fin==NULL)
{
cout<<“\n Sorry no record found \n”;
}
else
{
printf(“\n========================================================\n\n”);
printf(“\t\t All Employee Details\n\n”);
printf(“========================================================\n\n”);

printf(“ID\tName\tSalary\n\n”);

for(int i=0;i<size;i++)
{
fin.read((char *) & t,sizeof(t));
t.display();
cout<<“\n”;
}
printf(“========================================================\n\n”);
}

fin.close();
}

//Return Total Objects stored in the File
int getSizeofFile()
{
int size;
int total;
Employee t1;

ifstream fin(fname);
fin.seekg(0,ios::end);

total=fin.tellg();

fin.close();
size=total/sizeof(t1);
return(size);
}

2 Comments

Filed under C++, File I/O

C++ : File I/O Part 3

1. Explain the use of File I/O.

2. Read and Write Object & Array using:

1. read()

2. write()

3. Explain the use of following method:

1. seekg()

2. tellg()

3. seekp()

4. tellp()

4. Example showing the use of reading & writing data into the File.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
C++ File I/O - Part 3.
*/

#include
#include

using namespace std;

class Employee
{
int id;
int salary;

public:

Employee(){ }
~Employee(){ }

void setData()
{
cout<>id;
cout<>salary;
}

void display()
{
cout<<“\n ID:”<<id;
cout<<“\n Salary:”<<salary;
}
};

int main()
{
int arr[]={10,20,30,40,50};

//Writing Array values.
fstream fobj(“one.dat”,ios::out);

fobj.write((char *)&arr,sizeof(arr));

fobj.close();

//Reading Array values.
fobj.open(“one.dat”,ios::in);

int arr2[5];

fobj.read((char *)&arr2,sizeof(arr2));

for(int i=0;i<5;i++)
{
cout<<arr2[i]<<“\n”;
}

fobj.close();

//Writing Object value.

fobj.open(“one.dat”,ios::out);

Employee e1;

e1.setData();

fobj.write((char *)&e1,sizeof(e1));

fobj.close();

//Reading Object value.

fobj.open(“one.dat”,ios::in);

Employee e2;

fobj.read((char *)&e2,sizeof(e2));

e2.display();

fobj.close();

return 0;
}

1 Comment

Filed under C++, File I/O