Solved

How to handle errors in C++

Posted on 2008-10-03
5
205 Views
Last Modified: 2013-11-17
I have a timer which loads a picture using ThePIctureName->LoadFromFile every x seconds. The only problem is , that the file i am trying to load is dynamically changing by another application( can be deleted in 1s, and be reconstructed the next sec) Therefore,at certain moments, my application presents error message that it cannot load the picture.

What I want is to create an event like this: WhenErrorOccures --> To ignore that error, and try to load the file again. How do i do this? Thanks.
0
Comment
Question by:Chrysaor
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 500 total points
ID: 22633776
Check first if(FileExists(FileName)) to see if the filename exists...
IF exists check the size - must be > 0 - and then try LoadFromFile...
Alternative:
try
{
  ThePIctureName->LoadFromFile()
  //your code for success here
}
catch(...)
{
 //Your code for failure
}

George Tokas.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 22634448

I'd go with George's second suggestion (try/catch).  If the host file is changing quite often, just checking for the file may not be robust enough as the file can be deleted (or created) or its size change between checking for the file and actually trying to read it.


Good Luck,
Kent
0
 

Author Comment

by:Chrysaor
ID: 22644295
The try/catch didn't work.. An error did pop up again..
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 22645238
I don't have a habbit to stop monitoring ANY Q I participate...
I'm and will be here Chrysaor...
What kind of error you have??

George Tokas.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 22649092
>> The try/catch didn't work.. An error did pop up again..

Check your project settings.  It may be that this error is being handled by the default handlers.


Kent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

863 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

20 Experts available now in Live!

Get 1:1 Help Now