Solved

How to handle errors in C++

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
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…

809 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