Solved

Is there a command to trap error ?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now