---
title: "How do you handle an \"arithmetic exception,\" like division by zero, in C++?"  
description: "How do you handle an \"arithmetic exception,\" like division by zero, in C++?"  
author: "Revati S Misra"  
published: 2023-08-16  
updated: 2023-08-17  
canonical: https://www.mindstick.com/forum/159545/how-do-you-handle-an-arithmetic-exception-like-division-by-zero-in-c-plus-plus  
category: "visual c++"  
tags: ["exception handling", "exception", "c++"]  
reading_time: 2 minutes  

---

# How do you handle an "arithmetic exception," like division by zero, in C++?

How do you [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) an "arithmetic [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling)," like division by [zero](https://www.mindstick.com/forum/1264/add-a-zero-to-the-returned-value-if-there-is-no-zero), in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?

## Replies

### Reply by Aryan Kumar

To handle an arithmetic exception like division by zero 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. **Catch the** `ArithmeticException` **exception.** The `ArithmeticException` exception is thrown when an arithmetic error occurs, such as division by zero.
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 arithmetic exception in C++:

C++

```plaintext
#include <iostream>
#include <exception>

using namespace std;

int main() {
  int num1 = 10;
  int num2 = 0;

  try {
    // This will throw an ArithmeticException
    int result = num1 / num2;
    cout << "The result is " << result << endl;
  } catch (ArithmeticException e) {
    // Handle the exception gracefully
    cout << "Division by zero error" << endl;
  }

  return 0;
}
```

In this example, the `try` block contains the code that attempts to divide `num1` by `num2`. If the division is successful, the `catch` block will not be executed. However, if the division fails because `num2` is zero, the `ArithmeticException` 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 a division by zero error has occurred.

It is important to note that not all arithmetic errors are thrown as `ArithmeticException` exceptions. For example, overflow errors are not thrown as `ArithmeticException` exceptions. If you need to handle all arithmetic errors, you can use the `std::runtime_error` exception.


---

Original Source: https://www.mindstick.com/forum/159545/how-do-you-handle-an-arithmetic-exception-like-division-by-zero-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
