Thread: try & catch

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    19

    try & catch

    I've got this code (it is only part of my code):

    Code:
    int main(void) {
    
       ifstream fin("myfile.txt");
       string line;
       int counter=0;
    
       string nameMainDir; //it contains line " Directory: C:\root"
       //where 'Directory' is word dependent on language version of dir/s
       //and 'root' is the name of main directory in our tree
       string root; //it contains only name 'root'
    
    //...
    
          try
          {
             if (counter==0) //if it is first iteration, it reads nameMainDir
             {
                nameMainDir = line; //it reads first line
                size_t size;
                size=line.size(); //here I check size of the line
                int i=size;
                do { i--; } while (line[i]!=(char)92); //it counts from end of line
                                                    //to first use of symbol char(92)=='\'
                root=line.substr((i+1),(size-i)); //if writes name of root directory                                      
                counter++; //it disables entering this if any more
             }
          }
          catch(FirstLineIsEmpty)
          {cout<<"there was error";}
    
    }
    and I want this 'catch' to be executed if the first line is empty - but I don't know how to do it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you understand how try/catch works?
    Since there is no predefined error to what you want, you will have to make your own and also throw that error on your own.
    Do you understand how to use throw?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's not really the best use of exceptions. Normally you want to use exceptions where the error needs to propagate up to calling functions to be handled later. Here, you're creating the exception and handling it in the same place. A simple if statement should be all you need.

    If you want to use exceptions anyway, then you have to figure out what you want to throw. Normally you would use an existing exception class or write your own. Something as simple as this would work:
    Code:
    class FirstLineIsEmpty { };
    You still need an if, but if the line is empty you throw FirstLineIsEmpty;. The catch should be catch (FirstLineIsEmpty& ).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    I heard that using exceptions is if I've got situation which shouldn't happen and probably won't happen at all - this is why we call it exception. I don't use 'if' because I guess it will never happen. I expect first line to be non-empty line but if it happens that this line is empty, I want to use this exception. This exception should write information "There was an error. Do you want to continue execution of the program?" and write short explanation of the reason of this error (how to avoid it in the input file so that the user can edit .txt file by his/her own). And simply I don't know how to write this catch .

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You got this whole thing backwards.
    Exceptions does not just "happen" if the first line appears to be empty. YOU must check if the first line is empty and then THROW an exception. In other words, an if is required, same as without exceptions.
    Plus exceptions are not mean to simply stop execution and then resume it some line. It means to typically jump out of a function or procedure of code because something went wrong. You would have to re-do whatever you were doing again.

    Plus an exception does not "do" anything - it is simply a tool which contains information.
    I hope you understand what this means.

    EDIT:
    Also to add: Visual Basic have the "Resume" and "Resume Next" statements which basically jumps back to where the error occurred and the latter to the line after where the error occurred. Basically, they can recover from exceptions and continue code execution. C++ is different. It has no resume or resume next functionality. When an exception occurs, everything before the catch keyword is gone - code that will never be executed.
    Last edited by Elysia; 11-03-2008 at 12:45 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I heard that using exceptions is if I've got situation which shouldn't happen and probably won't happen at all - this is why we call it exception. <<

    That is true, but is only part of the equation. Another part is that you shouldn't use exceptions if a simple if statement will suffice. You should just use an if here. Separate the error message into a separate function if you'd like:
    Code:
    if (line.empty())
    {
        outputEmptyLineErrorMessage();
        return 1; // or whatever you want to return
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. What can be done with catch(...)?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2008, 10:27 AM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. file try...catch
    By SuperNewbie in forum C# Programming
    Replies: 1
    Last Post: 07-12-2002, 01:06 AM