By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Exception handling

P: 62
Hi,

I tried the next simple code for catch exception:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void fn(int a) throw(double){
  5.     cout << "function call" << endl;
  6. }
  7.  
  8. void main(void){
  9.         double d=3.97
  10.     try{
  11.         fn(d);
  12.     }
  13.     catch(double){
  14.         cout << "wrong argument" << endl;
  15.     }
  16.  
  17.     cin.get();
  18. }
But I have not got the message "wrong argument" on output.I am using VS 2008 Express Edition. Could someone explain what would be wrong?

Thanks

Arepi
Sep 24 '10 #1

✓ answered by hype261

Your problem lies here...

Expand|Select|Wrap|Line Numbers
  1. void fn(int a) throw(double) 
Basically what this is telling the compiler that this function can only throw exceptions with type double. If an exception is generated that doesn't match this type then it is converted to unexpected I believe.

What you are looking for is something like this

Expand|Select|Wrap|Line Numbers
  1. void fn(double a) throw(double) <--- the parameter needs to be double not int
  2. {
  3.  throw a;
  4. }

Share this Question
Share on Google+
1 Reply


100+
P: 207
Your problem lies here...

Expand|Select|Wrap|Line Numbers
  1. void fn(int a) throw(double) 
Basically what this is telling the compiler that this function can only throw exceptions with type double. If an exception is generated that doesn't match this type then it is converted to unexpected I believe.

What you are looking for is something like this

Expand|Select|Wrap|Line Numbers
  1. void fn(double a) throw(double) <--- the parameter needs to be double not int
  2. {
  3.  throw a;
  4. }
Sep 24 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.