1.
Simple try-catch with division by zero check
cpp
Copy
#include <iostream>
int main() {
try {
int a = 10, b = 0;
if (b == 0) throw "Division by zero error!";
std::cout << a / b << "\n";
catch (const char* msg) {
std::cout << "Exception: " << msg << "\n";
2. Catching std::exception
cpp
Copy
#include <iostream>
#include <exception>
int main() {
try {
throw std::runtime_error("Standard runtime error");
catch (const std::exception &e) {
std::cout << "Caught exception: " << e.what() << "\n";
}
3. Throw and catch integer exception
cpp
Copy
#include <iostream>
int main() {
try {
throw 404;
catch (int e) {
std::cout << "Exception code: " << e << "\n";
4. User-defined exception class
cpp
Copy
#include <iostream>
#include <string>
class MyException {
std::string message;
public:
MyException(std::string msg) : message(msg) {}
std::string what() { return message; }
};
int main() {
try {
throw MyException("Something went wrong!");
catch (MyException &e) {
std::cout << "Caught exception: " << e.what() << "\n";
5. User-defined exception class inheriting from std::exception
cpp
Copy
#include <iostream>
#include <exception>
class MyException : public std::exception {
public:
const char* what() const noexcept override {
return "My custom exception";
};
int main() {
try {
throw MyException();
}
catch (const std::exception &e) {
std::cout << "Exception caught: " << e.what() << "\n";
6. Exception in function and handling in main
cpp
Copy
#include <iostream>
#include <stdexcept>
void checkAge(int age) {
if (age < 18) throw std::invalid_argument("Age must be at least 18");
int main() {
try {
checkAge(15);
catch (const std::invalid_argument &e) {
std::cout << "Invalid argument: " << e.what() << "\n";
7. Multiple catch blocks
cpp
Copy
#include <iostream>
#include <exception>
int main() {
try {
throw 3.14;
catch (int e) {
std::cout << "Caught int: " << e << "\n";
catch (double e) {
std::cout << "Caught double: " << e << "\n";
catch (...) {
std::cout << "Caught unknown exception\n";
8. Throwing and catching std::out_of_range
cpp
Copy
#include <iostream>
#include <vector>
#include <stdexcept>
int main() {
std::vector<int> v = {1, 2, 3};
try {
std::cout << v.at(5) << "\n";
}
catch (const std::out_of_range &e) {
std::cout << "Out of range error: " << e.what() << "\n";
9. Custom exception with additional data
cpp
Copy
#include <iostream>
#include <string>
class DetailedException {
std::string message;
int code;
public:
DetailedException(std::string msg, int c) : message(msg), code(c) {}
std::string what() { return message; }
int getCode() { return code; }
};
int main() {
try {
throw DetailedException("Error occurred", 123);
catch (DetailedException &e) {
std::cout << "Exception: " << e.what() << ", Code: " << e.getCode() << "\n";
}
}
10. Exception rethrowing
cpp
Copy
#include <iostream>
void func() {
try {
throw "Error inside func";
catch (...) {
std::cout << "Caught inside func, rethrowing...\n";
throw;
int main() {
try {
func();
catch (const char* msg) {
std::cout << "Caught in main: " << msg << "\n";