Exception Handling Examples
Exception Handling Examples
cpp
Copy
#include <iostream>
int main() {
try {
int a = 10, b = 0;
2. Catching std::exception
cpp
Copy
#include <iostream>
#include <exception>
int main() {
try {
cpp
Copy
#include <iostream>
int main() {
try {
throw 404;
catch (int e) {
cpp
Copy
#include <iostream>
#include <string>
class MyException {
std::string message;
public:
int main() {
try {
cpp
Copy
#include <iostream>
#include <exception>
public:
};
int main() {
try {
throw MyException();
}
catch (const std::exception &e) {
cpp
Copy
#include <iostream>
#include <stdexcept>
int main() {
try {
checkAge(15);
cpp
Copy
#include <iostream>
#include <exception>
int main() {
try {
throw 3.14;
catch (int e) {
catch (double e) {
catch (...) {
cpp
Copy
#include <iostream>
#include <vector>
#include <stdexcept>
int main() {
try {
std::cout << "Out of range error: " << e.what() << "\n";
cpp
Copy
#include <iostream>
#include <string>
class DetailedException {
std::string message;
int code;
public:
};
int main() {
try {
std::cout << "Exception: " << e.what() << ", Code: " << e.getCode() << "\n";
}
}
cpp
Copy
#include <iostream>
void func() {
try {
catch (...) {
throw;
int main() {
try {
func();