Quick question on exception handling

This is a discussion on Quick question on exception handling within the C++ Programming forums, part of the General Programming Boards category; If I have this snippet of code in an implementation file: Code: cout << "Please select from one of the ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Quick question on exception handling

    If I have this snippet of code in an implementation file:
    Code:
        cout << "Please select from one of the following destinations (Enter the first letter only):" << endl;
        cout << "(C)hicago" << endl;
        cout << "(N)ew York" << endl;
        cout << "(D)enver" << endl;
        cout << "(O)rlando" << endl;
        cout << "(L)os Angeles" << endl;
        cout << "(R)eturn to Main Menu" << endl;																																				
        cin >> destination;
    //==============Start of Exception handling=====================
        if (destination != "c" || destination != "C" || destination != "n" || destination != "N"
    	|| destination != "d" || destination != "D" || destination != "o" || destination != "O"
    	|| destination != "l" || destination != "L" || destination != "r" || destination != "R")
    	throw str;
        }//==============end of try=============
        catch(string messageStr)
        {
    	cout << messageStr << endl << endl;
    	system("pause");
    	cin.clear();
    	cin.ignore(10,'\n');
        }//==============end of catch=============
        }//==============end of do===============
    
        cout << endl;
    
        if (destination == 'R' || destination == 'r')
            return;
        //==================Class Choice==================
      do
      {
        try
        {
        cout << "Will the customer be traveling First Class or Economy Class?" << endl;
        cout << "(1) First Class" << endl;
        cout << "(2) Economy Class" << endl;
        cin >> reserveType;
    //=================Start of exception handling===================
        if (reserveType != 1 || reserveType != 2)
    	throw str;
        }//==============end of try=============
        catch(string messageStr)
    Can I use "messageStr" again in this next catch?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, you can use the variable name messageStr. It's scope is limited to the catch block. However, it will be a completely separate variable with no relationship to the first one.

    Are you doing this to learn exceptions? This code is generally not how you'd want to use them unless you are just trying things for learning purposes.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Smile

    Quote Originally Posted by Daved View Post
    Yes, you can use the variable name messageStr. It's scope is limited to the catch block. However, it will be a completely separate variable with no relationship to the first one.

    Are you doing this to learn exceptions? This code is generally not how you'd want to use them unless you are just trying things for learning purposes.
    Yes, actually I am doing this for a final group project program. I saw the tutorial said that it's useful to create classes for exceptions as they occur, but seeing as we're trying to finish it up by midnight, I'm not going to go into too much more detail on this one. Thanks so much for your help!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, actually I am doing this for a final group project program.
    Er .. .I don't think that's what Daved meant by "learning purposes". But seeing as you've got a few hours left to finish, I suppose it's not a good time to scrap the whole component and start over.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code would be noticably easier to read [and write] if your comparisons use "tolower()" or some similar construction when you are looking at the input to see if it's a particular letter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by matsp View Post
    Your code would be noticably easier to read [and write] if your comparisons use "tolower()" or some similar construction when you are looking at the input to see if it's a particular letter.

    --
    Mats
    How about:

    Code:
    if(std::strchr("cndolr", tolower(destination)) == 0)
    {
        throw str;
    }
    Of course, "str" is a weird thing to be throwing...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 10:17 AM
  2. Exception handling
    By targa in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2006, 10:19 PM
  3. Exception Handling Class Woes
    By ventolin in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2005, 07:37 PM
  4. Exception handling !!!
    By Brain Cell in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 05:25 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21