---
title: "How to handle an \"invalid input\" Exception, like entering a character instead of an integer?"  
description: "How to handle an \"invalid input\" Exception, like entering a character instead of an integer?"  
author: "Revati S Misra"  
published: 2023-08-16  
updated: 2023-08-17  
canonical: https://www.mindstick.com/forum/159539/how-to-handle-an-invalid-input-exception-like-entering-a-character-instead-of-an-integer  
category: "visual c++"  
tags: ["exception handling", "exception", "c++"]  
reading_time: 2 minutes  

---

# How to handle an "invalid input" Exception, like entering a character instead of an integer?

How to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) an "invalid [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java)" [Exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling), like entering a [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character) [instead of](https://www.mindstick.com/articles/330/triggers-in-sql-server) an [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer)?

## Replies

### Reply by Aryan Kumar

To handle an invalid input exception like entering a character instead of an integer in C++, you can use the following steps:

1. Use a try-catch block. A try-catch block is a way to handle errors in a controlled way. The `try` block contains the code that you are trying to execute. If an error occurs, the `catch` block will be executed.
2. Use the `std::invalid_argument` exception. The `std::invalid_argument` exception is thrown when an invalid argument is passed to a function.
3. Handle the exception gracefully. This could involve logging the error, displaying a message to the user, or taking some other action.

Here is an example of how to handle an invalid input exception in C++:

C++

```plaintext
#include <iostream>
#include <stdexcept>

using namespace std;

int main() {
  int number;

  try {
    // This will throw an invalid_argument exception if the user enters a character
    cin >> number;
  } catch (invalid_argument e) {
    // Handle the exception gracefully
    cout << "Invalid input: " << e.what() << endl;
  }

  return 0;
}
```

In this example, the `try` block contains the code that tries to read an integer from the user. If the user enters a character instead of an integer, the `invalid_argument` exception will be thrown and the `catch` block will be executed. In the `catch` block, we simply print a message to the user indicating that the input is invalid.

It is important to note that not all invalid input exceptions are thrown as `invalid_argument` exceptions. For example, if the user enters a string that is not an integer, a `std::runtime_error` exception will be thrown. If you need to handle all invalid input exceptions, you can use the `std::exception` exception.


---

Original Source: https://www.mindstick.com/forum/159539/how-to-handle-an-invalid-input-exception-like-entering-a-character-instead-of-an-integer

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
