c++ try catch code example
Example 1: throw exception c++
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void MyFunc(int c)
{
if (c > numeric_limits< char> ::max())
throw invalid_argument("MyFunc argument too large.");
}
Example 2: C++ try catch
try {
} catch (const std::exception& e) {
std::cout << e.what();
}
Example 3: c++ try
try {
} catch (...){
}
Example 4: try statement in c++
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}