# stdexcept

***

**1. Exception Handling in Array Indexing**

```cpp
int main() {
  // Create an array of size 5
  int arr[] = {1, 2, 3, 4, 5};

  try {
    // Attempt to access an index out of bounds
    int value = arr[5];
    std::cout << "Value at index 5: " << value << std::endl;
  } catch (std::out_of_range& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**2. Exception Handling in File Operations**

```cpp
int main() {
  try {
    // Open a file that does not exist
    std::ifstream file("non_existing_file.txt");
  } catch (std::ifstream::failure& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**3. Exception Handling in Pointer Dereferencing**

```cpp
int main() {
  int* ptr = nullptr;

  try {
    // Attempt to dereference a null pointer
    int value = *ptr;
  } catch (std::runtime_error& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**4. Exception Handling in Function Calls**

```cpp
int divide(int a, int b) {
  if (b == 0) {
    throw std::invalid_argument("Division by zero");
  }

  return a / b;
}

int main() {
  try {
    int result = divide(10, 0);
    std::cout << "Result: " << result << std::endl;
  } catch (std::invalid_argument& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**5. Exception Handling in Class Constructors**

```cpp
class MyClass {
public:
  MyClass() {
    // Initialize member variables
    if (condition) {
      throw std::runtime_error("Initialization error");
    }
  }
};

int main() {
  try {
    MyClass object;
  } catch (std::runtime_error& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**6. Exception Handling in Destructors**

```cpp
class MyClass {
public:
  ~MyClass() {
    // Release resources
    if (condition) {
      throw std::runtime_error("Destruction error");
    }
  }
};

int main() {
  try {
    // Create an object and let it go out of scope
    {
      MyClass object;
    }
  } catch (std::runtime_error& e) {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
```

**7. Exception Handling in operator overloading**

```cpp
class MyClass {
public:
  MyClass& operator=(const MyClass& other) {
    if (condition) {
      throw std::runtime_error("Assignment error");
    }

    // Assignment implementation

    return *this;
  }
};
```

**8. Exception Handling in Copy Constructors**

```cpp
class MyClass {
public:
  MyClass(const MyClass& other) {
    if (condition) {
      throw std::runtime_error("Copy constructor error");
    }

    // Copy constructor implementation
  }
};
```

**9. Exception Handling in Default Constructors**

```cpp
class MyClass {
public:
  MyClass() {
    if (condition) {
      throw std::runtime_error("Default constructor error");
    }

    // Default constructor implementation
  }
};
```

**10. Exception Handling in Move Constructors**

```cpp
class MyClass {
public:
  MyClass(MyClass&& other) {
    if (condition) {
      throw std::runtime_error("Move constructor error");
    }

    // Move constructor implementation
  }
};
```

**11. Exception Handling in Iterators**

```cpp
class MyIterator {
public:
  MyIterator() {
    if (condition) {
      throw std::runtime_error("Iterator error");
    }

    // Iterator initialization
  }
};
```

**12. Exception Handling in Function Templates**

```cpp
template <typename T>
T divide(T a, T b) {
  if (b == 0) {
    throw std::invalid_argument("Division by zero");
  }

  return a / b;
}
```

**13. Exception Handling in Class Templates**

```cpp
template <typename T>
class MyClass {
public:
  MyClass() {
    if (T() == T()) {
      throw std::runtime_error("Class template error");
    }
  }
};
```

**14. Exception Handling in operator Overloading Templates**

```cpp
template <typename T>
T operator+(const T& a, const T& b) {
  if (condition) {
    throw std::runtime_error("Operator overloading error");
  }

  return a + b;
}
```

**15. Exception Handling in Test Fixtures**

```cpp
#include <gtest/gtest.h>

class MyClassTest : public ::testing::Test {
protected:
  void SetUp() override {
    if (condition) {
      throw std::runtime_error("Test fixture error");
    }
  }
};
```

**16. Exception Handling in Assertion Macros**

```cpp
#include <gtest/gtest.h>

TEST(MyClassTest, MyFunction) {
  ASSERT_NO_THROW(myFunction());
}
```

**17. Exception Handling in Catch Blocks**

```cpp
try {
  // Code that may throw an exception
} catch (std::exception& e) {
  std::cout << "Exception caught: " << e.what() << std::endl;
} catch(...) {
  std::cout << "Unknown exception caught" << std::endl;
}
```

**18. Exception Handling in Nested Catch Blocks**

```cpp
try {
  // Code that may throw an exception
} catch (std::invalid_argument& e) {
  // Handle invalid argument exception
} catch (std::runtime_error& e) {
  // Handle runtime error exception

```
