Here this is very...

Why does the function func throws exceptions whiles it is not supposed to.?. PLS explain...

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//exceptions specifications
#include <iostream>

using namespace std;

int func(char& a) throw()
{
    try
    {
        try
        {
            if(a>='a' && a<='z'){
                try
                {
                    throw a;
                }
                catch(char e)
                {
                    cout<<"Char catch/handler caught "<<e;
                }
            }
            else
                throw a;
        }

        catch(...)
        {
            cout<<"Passing over to the external handler...\n\n";
            throw;
        }
    }
    catch(char e)
    {
        cout<<"Caught "<<e;
    }
    catch(...)
    {
        cout<<"Default handler caught the throw.";
    }
}


int main()
{
    char x;

    cout<<"Enter a character: ";
    cin>>x;
    func(x);
    cin.ignore();
    cin.get();
}
What exception does it throw that you think it shouldn't?
Last edited on
Why do you have two try statements at the top of the function?
The only way I see that func() could be throwing an exception is if your char e catch block was throwing (so that cout statement). But that is extremely unlikely to happen.

+1 Moschops. What makes you think that function is throwing an exception?


EDIT:

Also, @Shock Bolt: he has two exception handlers, hence the 2 try blocks. The first one catches and rethrows to the 2nd one.
Last edited on
@Moschops

all exceptions
Last edited on
closed account (zb0S216C)
The function shouldn't throw anything. Also, the throw() specifier is redundant, since the compiler cannot ensure the function will not throw an exception during run-time. In C++11, there's the noexcept() specifier which attends to this issue.

Wazzak
Thanks Framework.
So can I use it in my code?
closed account (zb0S216C)
Use what? throw()? You can, but it would be pointless. If you're talking about noexcept(), then I recommend that you do.

Wazzak
Thanks - noexcept();
Last edited on
Topic archived. No new replies allowed.