Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
NOTE:: Sorry it is very basic question if you reply would be very nice

what i am trying to do is while reading text from file. splitting the text with ( : ). But if some line in the file has no : than want to throw error. and in the catch section display msg wrong text format.
sample text in file is
Text part 1 : Text Part 2 //line 1
text part 1 : text part 2 //line 2
text part 1 : text part 2 //line 3
sample text without colon //line 4 line without :

  try{
    QStringList Textfields = TextLine.split(":");
    TextFirstPart = Textfields [0];
    TextSecPart = Textfields [1];
throw;
}
catch(...)
{
cout <<"Unidntified Input data format"<<endl;
}

trying to throw error msg only for line 4. How to do this??

What I have tried:

i tried what i mentioned above but it is not working....
Posted
Updated 6-Mar-16 22:19pm
v2

Just check if splitting was possible. If there was no ':' character, the string is not splitted and the list contains only one element (the original string):
C++
if (TextFields.count() == 1)
    throw;

But in your case there is no need to use exceptions because you are detecting and handling the error inside your function. Instead you might use an error indicator and return that from your function:
C++
int error = 0;
while (!error && !EndOfFile())
{
    QString TextLine = ReadLine();
    QStringList Textfields = TextLine.split(":");
    if (TextFields.count() == 1)
    {
        // No ':' character
        error = 1;
    }
    else if (TextFields.count() > 1)
    {
        // Multiple ':' characters
        error = 2;
    }
    // Check for other errors here
    else
    {
        // Process line
    }
}
if (error)
{
    // Show error message
}
// Return false when an error occured
return 0 == error;
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 7-Mar-16 4:41am    
Good one, Jochen! 5ed.
In order to make it working, you should remove the throw; statement. In any case you might avoid using exceptions, in such a simple scenario: using the QStringList's size method, you are able to detect the 'unidentified inpup data format' line.
 
Share this answer
 
Comments
KhalBuz 7-Mar-16 3:55am    
I am using exception. because there is one more scenario in which i need to handle
e.g
string text : string text // line 1
some digit : string text //both should be strings if its digit than also throw error
I want to throw error if any unexpected text line will come. there are many different possibilities so rather than going for complex if else if else i am trying to throw error msg of "unexpected data input". this is my understanding your suggestions are welcome
Philippe Mori 7-Mar-16 13:17pm    
Usually, you don't want to do that... It is a misuse of exceptions to used them as a replacement of conditionà statements. Exception should only be used for exceptionnal error condition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900