How to use try\catch\finally\throws.

What exactly are these keywords. and an example of their implimentation? every time I try to use these I get errors. I understand they help in catching errors..
try/catch/throw
1
2
3
4
5
6
7
8
9
10
11

try
{
    //Throw an exception
    throw("A nonsense error!");
}
catch(...)
{
   std::cout << "The is an error" << std::endl;
}


Finally is not a c++ construct.

edit: @LB Whoops! Fixed
Last edited on
Thank you I understand it now. sorry about the "finally". I'm learning Java at the same time as c\c++.
clanmjc: You're throwing a const char* but nowhere do you catch it ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int x = 5;
try
{
    if(x == 0) throw(1ULL);
    else if(x % 2) throw("x must be even");
    x *= 4;
}
catch(unsigned long long e)
{
    std::cerr << e;
}
catch(const char *e)
{
    std::cerr << e;
}
Last edited on
Topic archived. No new replies allowed.