473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Exception Catching difference between VC++ and C#

Exception Catching difference between VC++ and C#

In C# I have this:
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch
{
Console.WriteLine("oops !");
}

And it nicely catches whatever error happens inside the try block.

But now in C++ I have this:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch(...)
{
cout << "Oops !";
}
}

And it blows. I get the windows dialog that tells me about sending the
error to Microsoft.
Why ?

Feb 19 '07 #1
2 3450

<fi*******@gmail.comwrote in message
news:11********************@h3g2000cwc.googlegroup s.com...
Exception Catching difference between VC++ and C#

In C# I have this:
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch
{
Console.WriteLine("oops !");
}

And it nicely catches whatever error happens inside the try block.

But now in C++ I have this:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch(...)
{
cout << "Oops !";
}
}

And it blows. I get the windows dialog that tells me about sending the
error to Microsoft.
Why ?
You aren't using a throw statement, but catching a hardware (asynchronous)
exception. The default (/EHsc) catches only synchronous (C++ language)
exceptions. Compile with /EHa.
Feb 19 '07 #2
On 19 Feb 2007 13:10:31 -0800, fi*******@gmail.com wrote:
>Exception Catching difference between VC++ and C#

In C# I have this:
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch
{
Console.WriteLine("oops !");
}

And it nicely catches whatever error happens inside the try block.

But now in C++ I have this:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
try
{
int x, y, z;

x = 20;
y = 0;

z = x / y;
}
catch(...)
{
cout << "Oops !";
}
}

And it blows. I get the windows dialog that tells me about sending the
error to Microsoft.
Why ?
In C++, division by zero is undefined, and you should not expect any
particular treatment of the error. In Windows, integer division by zero
causes a structured exception, and VC++ allows you to catch it directly
with __try/__except or translate it into a C++ exception with
_set_se_translator. The latter requires you to compile with /EHa, and then
you will be able to catch it in catch(...). To understand why it's bad to
indiscriminately catch structured exceptions as C++ exceptions, see:

http://members.cox.net/doug_web/eh.htm

The C# language defines division by zero to throw
System.DivideByZeroException, and you can achieve much the same effect in
C++ by using _set_se_translator and turning EXCEPTION_INT_DIVIDE_BY_ZERO
into a C++ exception. Because you will also have to use /EHa, you should
stay away from catch(...) for the reasons given in my article. My
preference is to test for a zero denominator in the rare cases in which it
can occur. (For floating point, however, I might prefer to check the FPU
status word at the end of a calculation.)

--
Doug Harrison
Visual C++ MVP
Feb 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
0
by: Cody Manix | last post by:
is there any difference between try{}catch(Exception){} and try{}catch{} ? if not, why this short syntax? i think it encourages programmers especially beginners to catch all exceptions...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
1
by: archana | last post by:
Hi all, I am having c# application containing one property. What i want is not to allow user to set null value to property. So if in code user tries to set value as null i want to through...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
6
by: Mohan | last post by:
Hi, I am learning the Exception Handling in C++. I wrote a small program using Exception Handling. I am using Vistual Studio 6. It is working fine in Win32 Debug build, but it is not...
12
by: EvilOldGit | last post by:
In Stroustrup he talks of a MathErr class that you can use to catch floating point exceptions. It doesn't exist, at least not in Visual C++ I can catch FP exceptions using catch(...) but am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.