|
To catch an error you have to throw an exeption by calling throw. In debug mode certain exeptions are handled by the debuger, but that's not the case in your release version.
If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called.
This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is:
void terminate();
int a = 0;
int b;
try
{
b = 10 / a;
if( a == 0 ) throw "Error";
printf("b = %d a = %d\n", b, a);
}
catch (...)
{
printf("catch it");
return -1;
}
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
Toni78 wrote:
...because there is no catch statement with a matching type...
But there is: He has written catch(...) to catch ALL exceptions!
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
jhwurmbach wrote:
But there is: He has written catch(...) to catch ALL exceptions!
These are the definitions for try, catch, and throw:
"The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type.
We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch."
You have to throw an exception because you can't just simply expect the compiler to throw every kind of exception that there is out there.
Of course you can argue and say that division by zero is a standard exception and I agree with you on that, but catch(...) doesn't catch ALL the ecxeptions unless you throw some of them. Otherwise, programs would never crash.
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
Pal, sorry. I still not very clear about it.
Does it mean we can't catch a "divide by zero" exception at all? We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? Any code to solve my example?
|
|
|
|
|
novachen wrote:
Does it mean we can't catch a "divide by zero" exception at all?
I am not sure how the VC6 compiler handles this particular case so take a look at CException. If you are not using MFC then you have to write your own code.
novachen wrote:
We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0?
If you want to handle a divide by zero exception in your own way (not the compilers way) yes you have to check for every number before you divide, and you have to throw the error.
novachen wrote:
Any code to solve my example?
You could check my first reply to your message.
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
For me, it works in Debug as well as Release build.
The message is "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero."
Did you disable exceptions in the Project settings under the 'C++ Language'-tab?
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
yes, Exception is enable in 'C++ Language' as default.
I won't like a "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." message. That's just i get in release version.
What i want is a console output "catch it" and end the program like the behavior in debug version.
|
|
|
|
|
VC6?
There is a bug in the optimizer with VC6 that causes simple try/catch blocks from working. What happens is that the compiler thinks there can't be an exception so it optimizes the try/catch block away.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
i have test the code under vc7,vc7.1
debug version do it well, but it seemed that release version has no exception handler code.
if use cout replace printf ,then work fine .
i dont know whether this is a compiler bug .
|
|
|
|
|
Hi all,
I have a problem with setting of print page size programatically.
I try use DEVMODE and its values PaperSize, PaperWidth and PaperLength. Right after setting I determinate size of Paper by GetDeviceCap (not sure with function name).
Modification from A4 to A3 paper size works well
but
modification from A4 to A5 paper size doesn't.
Can anybody help me with this problem?
PetrP.
|
|
|
|
|
I'm having similar problems - can't set custom paper size, printer resets to A4, but the paper size can be set to non standard in the drivers properties.
Have you found any solutions yet?
|
|
|
|
|
What is the equvalent of CStringArray in c++?I want to store some strings in a array.
Mazy
No sig. available now.
|
|
|
|
|
std::vector<std::string> .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
See you after long time Joaquin.
Thanks.
Mazy
No sig. available now.
|
|
|
|
|
anyone know it? thank you?
|
|
|
|
|
Good people,
How do I set the “Comment” attribute of a file or folder using VB script ?
For example, the following code lists “Comment” attributes for files under C:\ directory:
set shell = CreateObject("Shell.Application")
set folder = shell.namespace("C:\")
for each folderitem in folder.items
list = list & _
folder.GetDetailsOf(folderitem , 0) & ", " & _
folder.GetDetailsOf(0, 5) & ": " & _
folder.GetDetailsOf(folderitem, 5) & vbCr
next
MsgBox list
Thanks,
Serge
|
|
|
|
|
I also do not understand what does macro guard do in header
file. What do we need to include #ifndef, # define and #endif.
And why do they capitialize the COUNTER_H, is COUNTER_H the
file name? It seems to be. Why is it called macro guard?
----------------------------
// File: Counter.h
// Counter class definition
#ifndef COUNTER_H // used to avoid multiple definitions
#define COUNTER_H // not part of the class
class counter
{
public:
// Member Functions
// Constructors
private:
// Data members (attributes)
};
#endif // COUNTER_H
|
|
|
|
|
It ensures that a declaration can only be included once.
Put simply, if you didn't have it in, you could end up with two versions of the same thing linked into your code. Try it. You won't get a compiler error but you will get a linker error
|
|
|
|
|
You can have compiler errors too. like the “type redefinition” one.
|
|
|
|
|
Regarding the symbol name, it is just a convention to use something that resembles the file name, for readability. It is always capitalized to avoid name collisions - language keywords are never all capitals, and #defines and typedefs are never named after files.
--Mike--
Latest blog entry: *drool* (Alyson) [May 10]
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
"You have Erica on the brain" - Jon Sagara to me
|
|
|
|
|
Capitalisation does not avoid name collisions. Example:
_DEBUG
The compiler defines _DEBUG when you specify the /MTd or /Mdd option. These options specify debug versions of the C runtime library.
Use GUID or MS-specific #pragma once.
|
|
|
|
|
1.) If Classes in C++ can do anything Structs do plus
more, why do we still have Structs in C++? Is there
something Classes can't do but Structs? Do you
prefer Classes over Structs?
2.) I understand the difference between Classes and
Structs is the default asscess of the Structs is public,
and Classes is private. But I don't understand why do we
use private. I know that the private keyword specifies
that those members are accessible only from member functions
and friends of the class. But why do we use it? Any specific
example? Why do we want to limit those asscess?
|
|
|
|
|
VW_Red_Jetta wrote:
why do we still have Structs in C++?
The reason is to maximise the compatibility with C. C has structs, and the default access level is public, so C++ retains the same system.
VW_Red_Jetta wrote:
But I don't understand why do we use private.
C++ additionally allows classes, and specifies that good encapsulation / good OOP the default access should be private. This ensures that anyone who needs to get/set the properties of a private variable will have to use an accessor or a mutator method. In general, you need to modify the properties of an object via getXXX() and setXXX() methods.
As a rule of thumb, use structs for POD, Plain Old Data (anything without pointers, that doesn't need initialisation or special procudures for copying). Use classes when you need to have your class DO something, rather than just store information.
|
|
|
|
|
[quote] As a rule of thumb, use structs for POD, Plain Old Data (anything without pointers, that doesn't need initialisation or special procudures for copying). Use classes when you need to have your class DO something, rather than just store information. [/quote]
Okay, so can we simply just use classes through all of the C++ programs?
Do the real world programmers use structs and classes together? Since
the existence of structs is for the backward compatibility of C language,
and Classes can do anything Structs do, so why not just use classes?
I second this question: Do the real world programmers use structs and classes together?
Thanks.
|
|
|
|
|
VW_Red_Jetta wrote:
I second this question: Do the real world programmers use structs and classes together?
Yes, look an any real API and you'll find some structs. Generally I try to avoid them, unless you're sending a very strong message about the complexity of the object.
DirectX for example.
In DirectX struct is used to signify that objects (especially the vertices of polygons) can be memcpy()'d directly. This is used almost to speed up the process of adding to the vertex buffer.
Not that I'd generally cite DirectX as a API for demonstrating good programming practices, but it's appropriate in this situation.
|
|
|
|