?
Solved

Is there a command to trap error ?

Posted on 1998-08-04
5
Medium Priority
?
222 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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

840 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