?
Solved

Is there a command to trap error ?

Posted on 1998-08-04
5
Medium Priority
?
216 Views
Last Modified: 2010-04-02
I'm working with VC 5.0 and looking for a command to trap error.  Is there a command which calls a function when an error is encountered ?

I'm looking for something similar to ON ERROR in Visual Basic.  
0
Comment
Question by:tam031198
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
alex_r earned 200 total points
ID: 1169386
Use try and catch blocks (exception handling).

Example:

CMyDaoRecordset myset;
try { myset.Open(); }
catch ( CDaoExeception* e) { e->Delete(); MessageBox(...); }

See help on each MFC function. You will discover which exception
are thrown on each one of them.
0
 

Author Comment

by:tam031198
ID: 1169387
This my code and I try to handle the devided by 0 exception:
try
{
    double a;
    a = a / 0;
}
catch(...)
{
  int i;
  i = 1;
}

If I understand well,  while executing the line "a = a / 0;", the program should go to the catch procedure and associate value 1 to i.  Why it never goes to the catch procedure ?
What's wrong with my code ?

Thanks
0
 
LVL 1

Expert Comment

by:alex_r
ID: 1169388
Replace:

a = a / b;

with:

if (!b) throw 0;
a = a / b;

0
 
LVL 1

Expert Comment

by:alex_r
ID: 1169389
An exception can be caused only by using throw keyword.
0
 

Author Comment

by:tam031198
ID: 1169390
Thank you very much Alex_r !
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question