473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VC++ Exception Handling

11 New Member
Hi all

I am working in VC++ domain. i need one help regarding vc++ exception handling. how to use exception handling in vc++? how to get the error message? how to avoid the runtime error. i used some exception in vc++ but in that i cant get the error message...

help me..
Regards
Sam
May 24 '07 #1
6 3888
sicarie
4,677 Recognized Expert Moderator Specialist
I can't give you one with authority - I don't use VC++, but check these out:

http://www.google.com/search?hl=en&q...=Google+Search
May 24 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You use the standard try/catch/throw of C++.

Do you have a code sample?
May 24 '07 #3
sathiyamurthy
11 New Member
You use the standard try/catch/throw of C++.

Do you have a code sample?

try{
int a=10;
int b;
b=a/0;
}
catch(CException* e)
{
some codes
}

here i want to catch the error.. but i cant get it...
if you know help me
May 25 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
I don't believe a/0 throws an exception. You need to call a function and have that function throw the exception you want to catch.

Your code should look like this:

Expand|Select|Wrap|Line Numbers
  1. try{
  2. int a=10;
  3. int b;
  4. b =     MyFunction(a,0);
  5. }
  6. catch(CException* e)
  7. {
  8. some codes
  9. }
  10.  
  11. //Where:
  12.  
  13. int MyFunction(int first, int second)
  14. {
  15.     if (second == 0) throw new CException;
  16.  
  17.     return  first/ second;
  18. }
  19.  
May 25 '07 #5
sathiyamurthy
11 New Member
Thanks for your reply,

i worked with your code. but in that i am not getting any messages.
i used GETErrorMessage() and ReportError() function.
i cant get it. help me...

And also i dont know how to use exception like CMemoryException,
waiting for your valuable reply.
May 28 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
OK, I put this in _tmain():

Expand|Select|Wrap|Line Numbers
  1.  int result = ATest(5,0);
  2.  
  3.     cout << "Result is: " << result << endl;
  4.  
The ATest function looks like my cocd from my last reply except I have got it to compile and execute:

Expand|Select|Wrap|Line Numbers
  1. int ATest(int a, int b)
  2. {
  3.    try
  4.    {
  5.      int* pb = &b;
  6.      *pb =    MyFunction(a,b);
  7.     }
  8.     catch(CException* e)
  9.     {
  10.         cout << "Exception!!" << endl;
  11.         return 0;
  12.     }
  13.     return b;
  14. }
  15.  
  16.  
  17.  
  18. int MyFunction(int first, int second)
  19. {
  20.     if (second == 0) throw new CMemoryException;
  21.  
  22.     return  first/ second;
  23. }
  24.  
  25.  
So, a call is made to ATest(). ATest trys MyFunction() passing in the two integers. If the second interge id 0, MyFunction() throws a MemoryException.

A CMemoryException IS-A CException. Therefore, it is caught using a CException pointer.

Try this code yourself with different values used in ther ATest() call in _tmain().
May 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2834
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
5968
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2736
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
2582
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
2522
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...
44
4161
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
9
1240
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
1
1063
by: sparc | last post by:
Hi, I have a software with the source code for windows. It can't be compiled in vc++ easily. Is it possible to set the break points in the source code using vc++ or atleast create the project file...
1
3089
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
7313
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
7372
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...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.