this post is probably a bit different from the usual questions about exceptions in c++. I hope i'm not wasting people's time with my seemingly obvious questions but i'm struggling to understand what i'm about to ask when reaching out to the obvious sources of knowledge before one would directly ask for help in understanding a concept.

My question is this, in c++, how does someone go about handling user input errors, by this specifically i mean when a user is being prompted for an integer and they enter a float or a string/char or vice versa. You know like someone entering their name when being prompted for their age or something.

I'm basically talking about c++'s equivilent to what would be in python something like:

try:
[code to prompting user for an integer.]
exception ValueError:
[code to run if exception is thrown.]

Now i know i'm probably going to get alot of face-palming because this seems like such a basic concept of programming but i'm having trouble understanding how to go about this in c++.

If one of you awesome guys has the spare time to explain this to me in a way a beginner would be able to understand it would be hugely appreciated guys. As this is something i'm getting frustrated with, it should be very simply i would have thought, perhaps my researching skills need work.

Thankyou in advance people and once again i'm very sorry for any time i've wasted with my noob-ass question. But i just want to learn, and would mean alot if someone could help me out here.

Thanks.

share|improve this question
the question is quite okay but could have been more precise and a lil less clumsy. Try asking the exact point if you can. avoid large amounts of detailing. try Avoiding unnecessary things apart from question (Last 3 para of your question and maybe the 1st para too) – Rohit Mar 21 at 7:01
What do you want to do if an input error occurs? It might be easier to just keep looping until you get what you want rather than using exceptions. – Jesse Good Mar 21 at 8:18
feedback

4 Answers

You can yourself inherit from base Exception class to have your own Exception type or use prepared Exception class like those my friends has bring them up there. About your case you should probably need to provide an Exception based on base exception class to evaluate input first and throw the exception you write exception if did not satisfy condition your want. like this:

class myException:Exception {
...
}

int evaluateInput(input) {
 if
 ...
 else
  throw myException;
}

// Get input from user

try:
 cin>>input;
 evaluateInput(input);
catch(myException)
 doWhatYouWant;

share|improve this answer
feedback

Exceptions are handled in C++ using a try-catch block. Exception handling is one improved feature of C++ over C. Exception handling prevents programs from crashing with unknown errors.

 try
    {
         //code to be tried
    }
    catch(Exception& e)
    {
          //make user understand the problem
      throw [expression]
    }

you can read through the MSDN page Or for a basic understanding you can read this

share|improve this answer
1  
Exception handling prevents programs from crashing with unknown errors & hiding such scenarios through exception handling rather than solving them is a very bad practice. – Als Mar 21 at 7:09
Holy shit this website is amazing, thankyou to every single one of you for the help! Definitely had my question answers and then some. Thanks guys appreciate the time spent by each one of you. =) – user1260830 Mar 21 at 7:09
@Als:exceptions caught are to be thrown too. is that worth special mentioning? ;-) – Rohit Mar 21 at 7:12
exceptions caught are to be thrown too Pray tell Why? Are you on a throwing frenzy? If you can handle an exception Once you catchit why do you want to throw it again? – Als Mar 21 at 7:15
oh no. was in a little hurry. When an exceptional circumstance arises within try block, an exception is thrown that transfers the control to the exception handler. is that alright – Rohit Mar 21 at 7:23
show 4 more comments
feedback

The basic example for try catch is something like this:

try
{
    // code that throws an exception
}
catch(...)
{
    // exception handling
}

Note that the three dots are perfectly valid for catching all exceptions, though you do not know "what" you catch. This is why you should prefer specifying the type in the parentheses.

The exception to be caught can be any type beginning from int and ending with a pointer to an object that derives from a exception class. This concept is very flexible, however you must know what exception may occur.

This may be a more concrete example, using std::excpetion: Note the catch by reference.

try
{
    throw std::exception();
}
catch (std::exception& e)
{
    // ...
}

Next example assumes you write C++ with MFC libraries. It clarifies that CException catch is executed because CFileException derives from CException. The CException object deletes itself if it is not needed anymore. Unless your exception derives from CException you should not throw a pointer and stick to the example above.

try
{
    throw new CFileException();
}
catch (CException* e)
{
    // CFileException is caught
}

Last but not least this is also important: You may define several catch blocks to catch different exceptions:

try
{
    throw new CFileException();
}
catch (CMemoryException* e)
{
    // ignore e
}
catch (CFileException* e)
{
    // rethrow exception so it gets handeled else where
    throw;
}
catch (CException* e)
{
    // note that catching the base class should be the last catch
}
share|improve this answer
3  
You should always, throw by value and catch by reference. – Als Mar 21 at 7:00
to be more precise catch by const reference(usually). – Als Mar 21 at 7:17
you are right, I updated my post calrifying that this should be done for MFC CException objects only. – Vash Mar 21 at 7:20
Every time I discover something new about MFC (which I never used), it's facepalming. Seriously, how can it be so wrong ? – Matthieu M. Mar 21 at 7:54
@MatthieuM. I do not want to make any excuses but provide information: If this is correct stackoverflow.com/a/2023134/426662 MFC supported sort of try/catch before it was standardized. Somewhere in 1994/95 with MFC 3.0 they changed it to use C++ try catch but also wanted compatability with their old method which apparantely used heap allocated objects. Would be nice if they had dropped it, lots of programmers may think differently because they do not need to update their code. – Vash Mar 21 at 9:33
feedback

You can use simple if..else loop for simple user input validations. You can use exceptions in pretty similar way

e.g.

try {
      ...code that can generate the error...
   } catch (some_exception& e) {
     ...code that handles the error...
   }
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.