# exception

***

**Exception Handling in C++**

**1. Simple Error Handling with try-catch**

```cpp
try {
  // Code that may throw an exception
} catch (std::exception& e) {
  // Handle the exception
}
```

**2. Catch Specific Exceptions by Type**

```cpp
try {
  // Code that may throw an exception
} catch (std::invalid_argument& e) {
  // Handle invalid argument exception
} catch (std::out_of_range& e) {
  // Handle out-of-range exception
}
```

**3. Use Nested try-catch Blocks**

```cpp
try {
  // Outer try block
  try {
    // Inner try block
  } catch (std::exception& e) {
    // Handle inner exception
  }
} catch (std::exception& e) {
  // Handle outer exception
}
```

**4. Exception with Custom Error Message**

```cpp
class MyException : public std::exception {
public:
  MyException(const std::string& message) : std::exception(message) {}
};

try {
  // Code that may throw a MyException
} catch (MyException& e) {
  std::cout << "MyException: " << e.what() << std::endl;
}
```

**5. Rethrow Exception**

```cpp
try {
  // Code that may throw an exception
} catch (std::exception& e) {
  // Log the exception
  std::cerr << "Exception: " << e.what() << std::endl;
  throw; // Rethrow the exception
}
```

**6. Use Exception Pointer**

```cpp
try {
  // Code that may throw an exception
} catch (...) {
  std::exception_ptr e = std::current_exception();
  try {
    std::rethrow_exception(e);
  } catch (std::exception& exc) {
    // Handle the exception
  }
}
```

**7. Catch All Exceptions**

```cpp
try {
  // Code that may throw an exception
} catch (...) {
  // Handle any exception
}
```

**8. Use noexcept Specifier**

```cpp
int function() noexcept {
  // Code that cannot throw an exception
}
```

**9. Handle Exceptions in Constructors**

```cpp
class MyClass {
public:
  MyClass() {
    // Code that may throw an exception
  }
};

try {
  MyClass obj;
} catch (std::exception& e) {
  // Handle the exception
}
```

**10. Handle Exceptions in Destructors**

```cpp
class MyClass {
public:
  virtual ~MyClass() {
    // Code that may throw an exception
  }
};

try {
  MyClass* obj = new MyClass();
  delete obj;
} catch (std::exception& e) {
  // Handle the exception
}
```

**11. Exception in Class Member Function**

```cpp
class MyClass {
public:
  void function() {
    // Code that may throw an exception
  }
};

try {
  MyClass obj;
  obj.function();
} catch (std::exception& e) {
  // Handle the exception
}
```

**12. Exception in Friend Function**

```cpp
class MyClass {
public:
  friend void friendFunction();
};

void friendFunction() {
  // Code that may throw an exception
}

try {
  friendFunction();
} catch (std::exception& e) {
  // Handle the exception
}
```

**13. Exception in Operator Overloading**

```cpp
class MyClass {
public:
  MyClass operator+(const MyClass& other) {
    // Code that may throw an exception
  }
};

try {
  MyClass obj1, obj2;
  MyClass obj3 = obj1 + obj2;
} catch (std::exception& e) {
  // Handle the exception
}
```

**14. Exception in Template Function**

```cpp
template<typename T>
T function(T arg) {
  // Code that may throw an exception
}

try {
  int i = function(10);
} catch (std::exception& e) {
  // Handle the exception
}
```

**15. Exception in Lambda Function**

```cpp
auto lambda = [](int arg) {
  // Code that may throw an exception
};

try {
  lambda(10);
} catch (std::exception& e) {
  // Handle the exception
}
```

**16. Exception in Thread**

```cpp
std::thread thread([]() {
  // Code that may throw an exception
});

try {
  thread.join();
} catch (std::exception& e) {
  // Handle the exception
}
```

**17. Exception in Exception Class**

```cpp
class MyException : public std::exception {
public:
  MyException(const std::string& message) : std::exception(message) {}
};

try {
  throw MyException("My exception message");
} catch (std::exception& e) {
  // Handle the exception
}
```

**18. Exception in Dynamic Allocation**

```cpp
int* ptr = new int[10];

try {
  ptr[10] = 10; // Index out of bounds
} catch (std::exception& e) {
  // Handle the exception
}

delete[] ptr;
```

**19. Exception in File I/O**

```cpp
std::ifstream file("filename.txt");

try {
  file >> data; // File not found or invalid
} catch (std::exception& e) {
  // Handle the exception
}

file.close();
```

**20. Exception in Network Operations**

```cpp
std::socket sock;

try {
  sock.connect("127.0.0.1", 80); // Connection refused or timed out
} catch (std::exception& e) {
  // Handle the exception
}

sock.close();
```

**21. Exception in Database Operations**

```cpp
std::sql::Connection conn;

try {
  conn.execute("SELECT * FROM table"); // Invalid query or table not found
} catch (std::exception& e) {
  // Handle the exception
}

conn.close();
```

**22. Exception in GUI Operations**

```cpp
std::window window;

try {
  window.create(100, 100, "Title"); // Invalid window parameters
} catch (std::exception& e) {
  // Handle the exception
}

window.close();
```

**23. Exception in Multithreading**

```cpp
std::mutex mutex;

try {
  mutex.lock();
  mutex.lock(); // Recursive lock exception
} catch (std::exception& e) {
  // Handle the exception
}

mutex.unlock();
```

**24. Exception in Coroutines**

```cpp
auto coro = std::coroutine<void>::from_lambda([]() {
  // Code that may throw an exception
});

try {
  coro();
} catch (std::exception& e) {
  // Handle the exception
}
```

**25. Exception in Asynchronous Operations**

```cpp
std::future<int> fut = std::async([]() {
  // Code that may throw an exception
});

try {
  fut.get();
} catch (std::exception& e) {
  // Handle the exception
}
```

**26. Exception in Memory Management**

```cpp
void* ptr = malloc(1024);

try {
  free(ptr + 1024); // Invalid free
} catch (std::exception& e) {
  // Handle the exception
}

free(ptr);
```

**27. Exception in Resource Acquisition and Initialization (RAII)**

```cpp
class MyResource {
public:
  MyResource() {
    // Code that may throw an exception
  }

  ~MyResource() {
    // Code that may throw an exception
  }
};

try {
  MyResource resource;
} catch (std::exception& e) {
  // Handle the exception
}
```

**28. Exception in Constructor Initialization List**

```cpp
class MyClass {
public:
  MyClass() : member(5) {
    // Code that may throw an exception
  }

private:
  int member;
};

try {
  MyClass obj;
} catch (std::exception& e) {
  // Handle the exception
}
```

**29. Exception in Copy Constructor**

```cpp
class MyClass {
public:
  MyClass(const MyClass& other) {
    // Code that may throw an exception
  }
};

try {
  MyClass obj1;
  MyClass obj2(obj1);
} catch (std::exception& e) {
  // Handle the exception
}
```

**30. Exception in Move Constructor**

```cpp
class MyClass {
public:
  MyClass(MyClass&& other) noexcept {
    // Code that may throw an exception
  }
};

try {
  MyClass obj1;
  MyClass obj2(std::move(obj1));
} catch (std::exception& e) {
  // Handle the exception
}
```

**31. Exception in Assignment Operator**

```cpp
class MyClass {
public:
  MyClass& operator=(const MyClass& other) {
    // Code that may throw an exception
    return *this;
  }
};

try {
  MyClass obj1, obj2;
  obj1 = obj2;
} catch (std::exception& e) {
  // Handle the exception
}
```

**32. Exception in Move Assignment Operator**

```cpp
class MyClass {
public:
  MyClass& operator=(MyClass&& other) noexcept {
    // Code that may throw an exception
    return *this;
  }
};

try {
  MyClass obj1, obj2;
  obj1 = std::move(obj2);
} catch (std::exception& e) {
  // Handle the exception
}
```

**33. Exception in Destructor**

```cpp
class MyClass {
public:
  ~MyClass() {
    // Code that may throw an exception
  }
};

try {
  MyClass obj;
  // obj goes out of scope
} catch (std::exception& e) {
  // Handle the exception
}
```

**34. Exception in Object Disposal**

```cpp
MyClass* obj = new MyClass();

try {
  delete obj; // Delete with invalid pointer
} catch (std::exception& e) {
  // Handle the exception
}
```

**35. Exception in Member Access Operator**

```cpp
class MyClass {
public:
  int operator[](int index) {
    // Code that may throw an exception
  }
};

try {
  MyClass obj;
  obj[10]; // Index out of bounds
} catch (std::exception& e) {
  // Handle the exception
}
```

**36. Exception in Function Pointer**

```cpp
int function(int arg) {
  // Code that may throw an exception
}

int* func_ptr = &function;

try {
  func_ptr(10);
} catch (std::exception& e) {
  // Handle the exception
}
```

**37. Exception in Pointer Arithmetic**

```cpp
int* ptr = new int[10];

try {
  ptr[-1]; // Pointer arithmetic error
} catch (std::exception& e) {
  // Handle the exception
}

delete[] ptr;
```

**38. Exception in Casting**

```cpp
MyClass* obj = new MyClass();

try {
  OtherClass* other_obj = dynamic_cast<OtherClass*>(obj);
} catch (std::exception& e) {
  // Handle the exception
}

delete obj;
```

**39. Exception in Virtual Function Call**

```cpp
class BaseClass {
public:
  virtual void function() {}
};

class DerivedClass : public BaseClass {
public:
  override void function() {
    // Code that may throw an exception
  }
};

try {
  BaseClass* base_obj = new DerivedClass();
  base_obj->function();
} catch (std::exception& e) {
  // Handle the exception
}

delete base_obj;
```

**40. Exception in Template Specialization**

```cpp
template<typename T>
void function(T arg) {
  // Code that may throw an exception
}

template<>
void function<int>(int arg) {
  // Specialized code that may throw an exception
}

try {
  function(10);
} catch (std::exception& e) {
  // Handle the exception
}
```

**41. Exception in Friend Class**

```cpp
class MyClass {
public:
  friend class FriendClass;
};

class FriendClass {
public:
  void function(MyClass& obj) {
    // Code that may throw an exception
  }
};

try {
  MyClass obj;
  FriendClass friend_obj;
  friend_obj.function(obj);
} catch (std::exception& e) {
  // Handle the exception
}
```

**42. Exception in Inline Function**

```cpp
inline int function(int arg) {
  // Code that may throw an exception
}

try {
  int result = function(10);
} catch (std::exception& e) {
  // Handle the exception
}
```

**43. Exception in Lambda Capture**

```cpp
int var = 10;

try {
  auto lambda = [var]() {
    // Code that may throw an exception
  };
} catch (std::exception& e) {
  // Handle the exception
}
```

**44. Exception in Variadic Templates**

```cpp
template<typename... Args>
void function(Args&&... args) {
  // Code that may throw an exception
}

try {
  function(10, "abc");
} catch (std::exception& e) {
  // Handle the exception
}
```

**45. Exception in Pack Expansion**

```cpp
int values[] = {1, 2, 3};

try {
  function({values});
} catch (std::exception& e) {
  // Handle the exception
}
```

**46. Exception in Class Template**

```cpp
template<typename T>
struct Container {
  // Code that may throw an exception
};

try {
  Container<int> container;
} catch (std::exception& e) {
  // Handle the exception
}
```

**47. Exception in Enum Class**

```cpp
enum class MyEnum : int {
  Value1 = 10,
};

try {
  MyEnum::Value1 = 20; // Invalid assignment to enum class
} catch (std::exception& e) {
  // Handle the exception
}
```

**48. Exception in Input/Output Stream**

```cpp
std::stringstream stream;

try {
  stream << "invalid input";
  int value;
  stream >> value; // Invalid input conversion
} catch (std::exception& e) {
  // Handle the exception
}
```

**49. Exception in std::array**

```cpp
std::array<int, 10> arr;

try {
  arr.at(10); // Index out of bounds
} catch (std::exception& e) {
  // Handle the exception
}
```

**50. Exception in std::vector**

```cpp
std::vector<int> vec;

try {
  vec.at(10); // Index out of bounds
} catch (std::exception& e) {
  // Handle the exception
}
```
