I'm looking for a way to catch exceptions due to software or hardware errors and prevent my solution from breaking brutally, but I can't find anything complete.
I have read the two main ways of handling exceptions, C ++ and SEH, but it seems to me that both do not include all cases that can happen.
Can anyone help me ?
Greg Utas, thanks for article: i read it as soon as possible.
DevParty: ok, all cases is too much, but most of the cases?
I've tried both ways (exception C++ and SEH), but, for example, they catch division by zero, but not a string copy error.
In C/C++ you can copy a buffer to another, often a char array but not always, and unintentionally write past the end of the target. Less often but still possible you can write past the beginning as well.
So for example if you have a char and you write 6 bytes to that you have a problem.
The impact of this depends on where the buffer actually lives in the memory space and often what processing has been happening up to that point.
Of course, but not an exception. Which is why you are always reminded to use the strxxx_s versions which include bounds checking. But you have to work quite hard to write past the beginning of a buffer.
Compiling does not mean your code is right!
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
returnvalue * value;
Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE.... 1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION. 2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER.
You've failed to explain exactly what the problem is. How are we, therefore, to know what your program is supposed to do?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
cout<<"BOOK LOAN SYSTEM"<<endl;
cout<<"Enter the number of books : ";
cout<<"\nEnter the days of the loan : ";
cout<<"Days overdue : ";
cout<<"\nFine : RM "<<(l-b)*(f)*n;
If you mean to convert everything to C, you also may need to remove the const qualifiers depending on the exact C compiler (and version) you are using.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application.
Here's the critical portion of my code: (Unnecessary detail has been removed.)
Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun.
It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written."
I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.
m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); // All of these "Type" classes are derived from CGsTypeBase
CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount]; // Is this the correct way to create an array of pointers?
m_Members = m_s_b1;
m_Members = m_s_b2; // This line has the warning about the buffer overrun.
m_Members = m_s_b3;
m_Members = m_s_b4;
m_Members = m_s_w1;
m_Members = m_s_w2;
m_Members = m_S_addr;
The difficult we do right away...
...the impossible takes slightly longer.
Last Visit: 31-Dec-99 18:00 Last Update: 30-Sep-23 6:00