How to use try\catch\finally\throws.

Mar 16, 2012 at 8:57pm
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..
Mar 16, 2012 at 10:39pm
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 Mar 20, 2012 at 10:43pm
Mar 16, 2012 at 11:04pm
Thank you I understand it now. sorry about the "finally". I'm learning Java at the same time as c\c++.
Mar 16, 2012 at 11:05pm
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 Mar 16, 2012 at 11:10pm
Topic archived. No new replies allowed.