Solved

How to handle errors in C++

Posted on 2008-10-03
5
203 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 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

22 Experts available now in Live!

Get 1:1 Help Now