Solved

Is there a command to trap error ?

Posted on 1998-08-04
5
213 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 50 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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