Click here to Skip to main content
15,898,036 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralWhich is faster... Pin
NeverFall20-May-05 15:35
NeverFall20-May-05 15:35 
GeneralRe: Which is faster... Pin
mkuhac20-May-05 16:19
mkuhac20-May-05 16:19 
GeneralRe: Which is faster... Pin
rocky_pulley20-May-05 16:51
rocky_pulley20-May-05 16:51 
GeneralRe: Which is faster... Pin
NeverFall20-May-05 18:31
NeverFall20-May-05 18:31 
GeneralRe: Which is faster... Pin
peterchen21-May-05 11:27
peterchen21-May-05 11:27 
GeneralRe: Which is faster... Pin
Alexander M.,21-May-05 12:59
Alexander M.,21-May-05 12:59 
GeneralRe: Which is faster... Pin
Tom Archer20-May-05 16:54
Tom Archer20-May-05 16:54 
GeneralUncaught Exceptions Pin
shawnf2220-May-05 12:23
shawnf2220-May-05 12:23 
I was reading a chapter in the book C++ Primer Plus and ran across an interesting statement:

".... which can happen if there is no containing try block or no matching catch block, the exception is branded an uncaught exception, and, by default, it causes the program to abort"

So I started thinking. There are many system functions that can throw exceptions. For instance, when I use new to allocate memory for an object a bad_alloc exception could be thrown. Hopefully this will never happen, but what if it does? I've seen a lot of programs, professional and amateur which do not use try... catch blocks to enclose memory allocation. So in that case, if new results in a bad_alloc exception, is my program going to immediately terminate? A lot of times, I have seen code like this:

---

//Somewhere in the program, the pointer is initialized
TheClassType* theClassObj = NULL;
theClassObj = new TheClassType(arg1, arg2, ...);

//Somewhere else in the program, if pointer is not null, use it
if(theClassObj != NULL)
{
//put some code here to use it for something
}

---

So here is my question. The author of the book seems to imply that if the new operation throws an exception, my program will abort right there because there is no try and catch blocks to handle the exception. Is that really true? That seems to defeat the purpose of manually checking the pointer value every time I use it. Why bother?

My argument has always been that if the system can't allocate memory, there isn't much I can do about it at the moment it occurs anyway. So why bother with a try and catch? What's the point? Just let the program crash and then study to code to see what is going on because obviously there is some out of control code segment that is allocating and not releasing resources.

Does anyone have any thoughts on the matter? If so, I'm willing to read them. I'm still struggling to understand why and when I'd ever want to use the std C++ exception handling features. The project I am currently working on seems to be using it to some degree, but in the past I have never used it and I am having a hard time seeing the benefit of it.

Regards,
Shawn Fox

GeneralRe: Uncaught Exceptions Pin
Kevin McFarlane20-May-05 12:59
Kevin McFarlane20-May-05 12:59 
GeneralRe: Uncaught Exceptions Pin
liquid_20-May-05 21:24
liquid_20-May-05 21:24 
GeneralRe: Uncaught Exceptions Pin
Tim Smith22-May-05 3:58
Tim Smith22-May-05 3:58 
GeneralRe: Uncaught Exceptions Pin
S. Senthil Kumar21-May-05 0:40
S. Senthil Kumar21-May-05 0:40 
QuestionHow to Add Data to a dataset and write to an xml file Pin
Mikan2320-May-05 11:02
Mikan2320-May-05 11:02 
Generaldll problem for mIRC.. Pin
da^hype20-May-05 10:56
da^hype20-May-05 10:56 
GeneralChanging Display Options for Windows Taskbar Pin
wes21420-May-05 8:26
wes21420-May-05 8:26 
GeneralVS2005 - Disabling warning (project-wide) Pin
dandy7220-May-05 7:45
dandy7220-May-05 7:45 
GeneralRe: VS2005 - Disabling warning (project-wide) Pin
PJ Arends20-May-05 8:09
professionalPJ Arends20-May-05 8:09 
GeneralRe: VS2005 - Disabling warning (project-wide) Pin
dandy7220-May-05 8:35
dandy7220-May-05 8:35 
GeneralRe: VS2005 - Disabling warning (project-wide) Pin
David Crow20-May-05 8:35
David Crow20-May-05 8:35 
GeneralRe: VS2005 - Disabling warning (project-wide) Pin
Bob Stanneveld20-May-05 10:01
Bob Stanneveld20-May-05 10:01 
GeneralRe: VS2005 - Disabling warning (project-wide) Pin
dandy7220-May-05 10:17
dandy7220-May-05 10:17 
GeneralRunning Multiple Instances of a Static Library Pin
Islamax20-May-05 6:30
Islamax20-May-05 6:30 
GeneralRe: Running Multiple Instances of a Static Library Pin
Chris Losinger20-May-05 8:44
professionalChris Losinger20-May-05 8:44 
GeneralRe: Running Multiple Instances of a Static Library Pin
Islamax20-May-05 17:40
Islamax20-May-05 17:40 
GeneralRe: Running Multiple Instances of a Static Library Pin
doublebug21-May-05 1:43
doublebug21-May-05 1:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.