But mfc appear to encourage the employ of catch-by-pointer;, C/C++ Programming

Assignment Help:

Q: But MFC appear to encourage the employ of catch-by-pointer; should I do the similar?

A: Depends. If you're utilizing MFC and catching one of their exceptions, by all means, do it their way. Similar goes for any framework: while in Rome, do as the Romans. Don't attempt to enforce a framework into your way of thinking, even if "your" way of thinking is "better." If you decide to employ a framework, embrace its way of thinking employ the idioms that its authors expected you to use.

But if you're developing your own framework and/or a piece of the system which does not directly based on MFC, then don't catch by pointer only because MFC does it that way. While you're not in Rome, you don't essentially do as the Romans. In this case, you must not. Libraries such as MFC predated the standardization of exception handling in the C++ language, and some libraries employ a backwards-compatible form of exception handling which requires (or at least encourages) you to catch by pointer.

The difficulty with catching by pointer is that it's not clear who (if anyone) is responsible for deleting the pointed-to object. For instance, consider the following:

MyException x;

void f()                                                                                  

{

MyException y;

try {                                                                                                                                              

switch (rand() % 3) {

case 0: throw new MyException;

case 1: throw &x;

case 2: throw &y;

}

}

catch (MyException* p) {

...  should we delete p here or not???!?

}

}

There are three basic problems here:

 

It might be tough to decide whether to delete p in the catch clause. For instance, if object x is inaccessible to the scope of the catch clause, as while it's buried in the private part of some class or is static in some other compilation unit, it may be tough to figure out what to do.

If you solve out the primary problem by constantly using new in the throw (and thus consistently by delete in the catch), then exceptions always employ the heap that can cause problems while the exception was thrown since the system was running low on memory.

If you solve out the first problem by constantly not using new in the throw (and thus consistently not using delete in the catch), then you probably won't be capable to allocate your exception objects as locals (as then they might get destructed too early), wherein case you'll ought to worry about thread-safety, semaphores, locks etc. (intrinsically static objects are not thread-safe).

It isn't to say it's not possible to work through these issues. The point is this: if you catch by reference instead of by pointer, life is easier. Why make life tough when you don't have to?

The moral: ignore throwing pointer expressions, and ignore catching by pointer, unless you're using an existing library that "wants" you to do so.

 


Related Discussions:- But mfc appear to encourage the employ of catch-by-pointer;

Explain the command line parameters, Explain the Command Line Parameters? ...

Explain the Command Line Parameters? The Command line arguments are parameters supplied to a program from the operating system. These arguments are parameters accepted to main

Luminous jewel - a polishing game, Byteland county is very famous for lumin...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Define array of structures, Define Array of Structures? An Array of Str...

Define Array of Structures? An Array of Structures is an assortment of the same data types which are declared as structures. It is useful to store large and different number of

C#, 1. write a program named InputMethodDemo2 that eliminates the repetitiv...

1. write a program named InputMethodDemo2 that eliminates the repetitive code in the InputMethod() in the InputMethodDemo program. Rewrite the program so the InputMethod()contains

Amie, what happens when the following command is used? chmod u=rwx,go=r-x f...

what happens when the following command is used? chmod u=rwx,go=r-x foo

Bulk listing posting for a site like ebay, Project Description: I posted...

Project Description: I posted listings on a site that is like eBay but for video games. I post the similar listing many times daily, but need something that will let me post

Prepare an ipad application clash of clans like game, Prepare an iPad appli...

Prepare an iPad application Clash of Clans like game I would like to prepare a free city building app with the in app purchase possibility. An example game could be Clash of Cla

Find area, #question.to determine the area of a triangle,rectangle and tra...

#question.to determine the area of a triangle,rectangle and trapezium

Minimum shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd