C++ examples for Statement:try catch
Throwing and catching exceptions in main function
#include <iostream> int main(){/*from w w w . j ava 2s . c o m*/ for (int i {}; i < 7; ++i){ try{ if (i < 3) throw i; std::cout << " i not thrown - value is " << i << std::endl; if (i > 5) throw "Here is another!"; std::cout << " End of the try block." << std::endl; } catch (int i) { // Catch exceptions of type int std::cout << " i caught - value is " << i << std::endl; } catch (const char* message) { // Catch exceptions of type char* std::cout << " \"" << message << "\" caught" << std::endl; } std::cout << "End of the for loop body (after the catch blocks) - i is " << i << std::endl; } }