Newbie how to handle an exception caused by an invalid user input type

This post may differ slightly from the usual C ++ exception questions.

In C ++, as someone goes to handle user input errors, I mean when the user is prompted to enter an integer, and they enter a float or a string / char or vice versa. You know how someone enters their name when they are asked their age.

I basically talk about what in C ++ is equivalent to what would be in Python something like this:

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

If one of you wonderful guys has free time to explain it to me in such a way that a beginner can understand that he will be highly appreciated by the guys .

Thanks.

+4
source share
4 answers

The main example for try catch is something like this:

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

Note that the three points are great for detecting all exceptions, although you don't know the β€œwhat” you caught. This is why you should indicate the type in parentheses.

The exception to be caught can be any type starting with an int and ending with a pointer to an object that comes from the exception class. This concept is very flexible, but you should be aware of the exception that may occur.

This could be a more specific example using std :: excpetion: Note the catch by reference.

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

The following example assumes that you are writing C ++ with MFC libraries. It explains that a catch of CException is thrown, since a CFileException comes from CException. The CException object deletes itself if it is no longer needed. If your exception is not derived from CException, you should not throw a pointer and stick to the above example.

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

Last but not least, it is also important: you can 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 } 
+4
source

You can use a simple if..else loop to simply check user input. You can use exceptions in a pretty similar way.

eg.

 try { ...code that can generate the error... } catch (some_exception& e) { ...code that handles the error... } 
+1
source

Exceptions are handled in C ++ using a try-catch block. Exception handling is one improved C ++ function over C. Exception handling prevents program crashes with unknown errors.

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

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

0
source

You can inherit from the base Exception class yourself to have your own type of exception or use a prepared Exception class such as my friends brought them there. In your case, you probably need to provide an exception based on the base class of exceptions, first evaluate the data entry and throw an exception that you write the exception if you do not satisfy the condition that you 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; 

0
source

Source: https://habr.com/ru/post/1402597/


All Articles