Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

saving to/reading from text file using TStringList's SaveToFile/ReadToFile and exception handling/raising

Posted on 2005-05-01
5
Medium Priority
?
1,299 Views
Last Modified: 2013-11-17
Hi,
I develop small application (actually, a school project, but this in one os numerous other things, so I don' think I'm breaking the asking the question rules). I use SaveToFile and ReadFromFile methods of TStringList class to save/load data.

Now, when these methods fail to open the file for write/read, will they generate an exception, and what type of exception? All I need is some tutorial on exceptions aand handling them, an how to generate them - e.g. i would like to generate exception when the data file has wrong format and the function that iterates thru the StringList encounters invalid sequence, causing immediate jump to exception handling (try{}) block.

Though I've read topics on exceptions in Help file, i didn't figue how to implement it in the function.

Thnx, jares  
0
Comment
Question by:jares
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 200 total points
ID: 13905372
ok, say you just want to catch the exception thrown by TStringList

you could do:

try{
       open file;
}

catch( ...)
{
   caught the exeception.
}

the above catch catches everything, check the help file for what exception is thrown;
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 13905378
say you want to generate your own exceptions: you can do the following


  if(my file not open)
    throw 3;

catch( int E)
{
  switch E
    case 3: cout << file not open
  default:
   foo
}

as you can see you use the keyword throw to throw exceptions then you speicfy a value you want to throw, in my case I threw back a integer. the item thrown can be anything from basic types to your own classes.
Also it is possible to rethow exceptions by just typing throw; this will rethrow your eception and keep unwinding the stack until another catch block is found


0
 
LVL 25

Accepted Solution

by:
kode99 earned 1400 total points
ID: 13906992
Since you are using the VCL you may want to use of the VCL Exception class.  So in the case of a  basic example of loading the string list,

  TStringList *test = new TStringList();

  try
  {
      test->LoadFromFile("<file>");
  }
  catch(Exception& e)
  {
    AnsiString temp = e.Message;
  }

You will get a usefull message in the Message property of the exception - like "File does not exist" etc.  The other property is the a help context ID number.

Also to simplify throwing an exception you can do it as follows,

 if (!DirectoryExists("c:\\temp"))
  {
    if (!CreateDir("C:\\temp"))
      throw Exception("Cannot create c:\\temp directory.");
  }

You can also include a ID numbers etc. in the constructor for the exception.  Take a look at the help for the Exception class constructor for all the options.  Since you can combine a useful text message and a reference number it can simplify the catch portion somewhat.

So for example you could catch exceptions based on the file activity but you could further throw your own exception for things like maybe a empty file was loaded and the string list has no actual strings even though the file action completed without error.

  if(TestList->Count == 0)
    throw Exception("No strings loaded - empty file");

Or in your case a message about the invalid format etc.

0
 
LVL 1

Author Comment

by:jares
ID: 13910767
thanks for quick responses, I'm going to test this tomorrow...
0
 
LVL 1

Author Comment

by:jares
ID: 13911114
hi, it works, of course. This was my fault - I set the cath block to catch EFOpenError, but i threw EFCreateError exceptions. it's OK now.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

571 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