Exception catching not displayed?

Hi all,

I have 2 methods with one method 1 throwing exception and method 2 that is catching the exception thrown by method 1. So my exception types are 2 empty classes for testing purposes. Somehow, nothing is being printed out even though i called the method2 in the int main(). Supposedly, method2 should catch exception of class H() type due to my condition, but no output message.

Am i not catching the exception right?

1
2
3
4
5
void method1(int g){
if(g<10) throw L();
if(g>10) throw H();
return;
}


1
2
3
4
5
6
7
8
9
void method2() {

try{
method1(100);

}catch(L c){ cout<< "catch L()" << endl;}

catch(H c){ cout<< "catch H()" << endl;}
}

Last edited on
Code as pasted works just fine for me. Are you sure method2 is being called?
Yes, i did call method2() in the main(), but it doesn't throw anything. That's the problem.
Can you post a minimal compilable example of this? Like I say I tried the pasted portion in a quickie program and it worked just fine, so the problem must be elsewhere.

EDIT:

here's a minimal test I made:
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
#include <iostream>
using namespace std;

class L { };
class H { };

void method1(int g)
{
    if(g<10) throw L();
    if(g>10) throw H();
    return;
}

void method2()
{
    try
    {
        method1(100);
    }
    catch(L c)
    { cout<< "catch L()" << endl; }
    catch(H c)
    { cout<< "catch H()" << endl; }
}

int main()
{
    cout << "program start\n";
    method2();
    cout << "program end\n";

    // wait for keypress
    int blah;
    cin >> blah;

    return 0;
}


As expected, the output is:
program start
catch H()
program end
Last edited on
Thanks, i think it works now!
Last edited on
Topic archived. No new replies allowed.