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

Posted on 2005-05-01
Last Modified: 2013-11-17
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  
Question by:jares
    LVL 11

    Assisted Solution

    ok, say you just want to catch the exception thrown by TStringList

    you could do:

           open file;

    catch( ...)
       caught the exeception.

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

    Expert Comment

    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

    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

    LVL 25

    Accepted Solution

    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();

      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.

    LVL 1

    Author Comment

    thanks for quick responses, I'm going to test this tomorrow...
    LVL 1

    Author Comment

    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.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    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…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    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…

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now