Skip to main content

How to generate exceptions in virtual constructors

It is possible that exceptions might raise in a constructor or destructors. If an exception is raised in a constructor, memory might be allocated to some data members and might not be allocated for others. This might lead to memory leakage problem as the program stops and the memory for data members stays alive in the RAM.

 

Similarly, when an exception is raised in a destructor, memory might not be deallocated which may again lead to memory leakage problem. So, it is better to provide exception handling within the constructor and destructor to avoid such problems. Following program demonstrates handling exceptions in a constructor and destructor:

#include<iostream>

using namespace std;

class Divide

{

private:

int *x;

int *y;

public:

Divide()

{

x = new int();

y = new int();

cout<<“Enter two numbers: “;

cin>>*x>>*y;

try

{

if(*y == 0)

{

throw *x;

}

}

catch(int)

{

delete x;

delete y;

cout<<“Second number cannot be zero!”<<endl;

throw;

}

}

~Divide()

{

try

{

delete x;

delete y;

}

catch(...)

{

cout<<“Error while deallocating memory”<<endl;

}

}

float division()

{

return (float)*x / *y;

}

};

int main()

{

try

{

Divide d;

float res = d.division();

cout<<“Result of division is: “<<res;

}

catch(...)

{

cout<<“Unkown exception!”<<endl;

}

return 0;

}

Input and output for the above program is asfollows:

Enter two numbers: 5 0

Second number cannot be zero!

Unkown exception!

 

Advantages of Exception Handling

 

Following are the advantages of exception handling:

Exception handling helps programmers to create reliable systems.Exception handling separates the exception handling code from the main logic of program.Exceptions can be handled outside of the regular code by throwing the exceptions from a function definition or by re-throwing an exception.Functions can handle only the exceptions they choose i.e., a function can throw many exceptions, but may choose handle only some of them.

A program with exception handling will not stop abruptly. It terminates gracefully by giving appropriate message.

Take you time to comment on this article.

Comments

Popular posts from this blog

Hack WiFi password (rooted or non-rooted device)

Hello guyzz!!! Today I am here to share a trick how to hack WiFi password in an android mobile.It works on rooted as well as non-rooted device depending upon you device capability.. How to install: Method One: in order to skip license verification first you must be disconnected from internet when you run this app , it will automaticly connects you or turns your wifi on Method Two: or use lucky patcher for preventing it from license verification check these options in lucky patcher auto mode, other patches, apply patch to delvick cache, back up apk file for reinstaller This app has NO advertisements Instructions: This app has NO advertisements CONTACT ME BEFORE GIVING BAD REVIEW, SO I CAN HELP YOU. The application supports two types of test: - Dictionary test - it tries passwords from predefined list one by one. Please don't be disappointed if the password willnot be found, it simply means that it was not in the dictionary. However, if somebody set his key to ...

Hack a memory card password within minutes

possible ways to break micro sd memory card password hye friends today's topic is very importent .some time we secrate the data in memory card with password but unfortunatly we forget that password which protect our dta from hackers or your friends or any other you guess. so i am here to give you best solution of breaking memory card paasword in only 5 minutes. there are lot off methode publish on internate about how to break the memory card password but many of my reader advise me to write a tutorial about breaking memory card password .becuase they believe that i am write that tutorial very friendly so they can understand very ifficiently. so now i am going to start the tutorial namly how to break memory card password.here i am publish very friendly methode break password so i give only those methode which easily understandable for everyone .so dont wasting your valuable time lets start.follow step by step procedure. solution no. 1:-- BREAKING MEMORY CARD PASSWORD FOR...

Lucky patcher(some basics)

Though this app has many more feature for rooted android, Non-rooted android users can only enjoy following features:- 1)Create modified.apk This feature allows you to modify app. You can create app with custom patch applied (if available), app with ads removed and app with changed permissions and activities and the app will be stored in sd card. Then you have to install this modified app, before this make sure you have checked Unknown sources in your device???s setting. 2)Backup App. You can backup app only(means no data as like in Titanium backup) 3)Hack in app purchases. With this app you can hack only offline games Step 01 : Download Lucky patcher for non-rooted devices from here. Step 02 : Enable the installation from unknown sources by going to settings > security and tap on Unknown sources check-box. Step 03 : Now navigate to folder where you have placed/downloaded the luckypatcher.apk file and install it. Step 04 : Now open lucky patcher app and tap on the ...