Solved

Is there a command to trap error ?

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now