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 &quo

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

Hack gmail password

Phishing method  Phishing is an old method of e-mail fraud that is used to gather personal and financial information from the recipients. phishing is the act of attempting to acquire information such as usernames, passwords, and credit card details (and sometimes, indirectly, money) by masquerading as a trustworthy entity in an electronic communication.Phishing is an example of a social engineering technique. ***Step by step guide to hack Gmail account using phishing *** step:1.)  first of all Go to the Gmail.com step:2)   and then right click on the blank area, you will see the option view source page,simply click on that.(see bellow picture for better understanding )                                           step:3)  now a pop up window  will be open which contain a source code of Gmail  page, And Copy  all the code using ctrl+c  step:4)  Now open the notepad and past all code here which copy in step 3  step:5)  now press CTRL+F,and type ACTION.(see bellow picture) step