Throw string value as exception - C++ Statement

C++ examples for Statement:try catch

Description

Throw string value as exception

Demo Code

#include <iostream>
#include <string>
using namespace std;

void Inner()/*  w ww  . ja va2  s  .c o m*/
{
    throw string("Error!");
}

void Outer()
{
    try
    {
        Inner();
    }
    catch (string excep)
    {
        cout << "Outer caught an exception: ";
        cout << excep << endl;
        throw;
    }
}

int main()
{
    try
    {
        Outer();
    }
    catch (string excep)
    {
        cout << "main caught an exception: ";
        cout << excep << endl;
    }

    return 0;
}

Result


Related Tutorials